The `google-cloud-profiler` Python agent continuously collects CPU usage and memory-allocation information from Python applications in production. It helps identify resource-intensive parts of the code with low overhead, supporting performance analysis through flame graphs in the Google Cloud console. The library is actively maintained with irregular releases, the latest being v4.1.0, focusing on performance and compatibility with newer Python versions.
pip install google-cloud-profilerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the Google Cloud Profiler agent in a Python application. The `googlecloudprofiler.start()` function should be called as early as possible in your application's lifecycle. It requires a `service` name and optionally a `service_version` to categorize profiling data. The `project_id` is often auto-detected in Google Cloud environments but must be explicitly set if running outside GCP.
Enable the Cloud Profiler API via the Google Cloud Console or `gcloud services enable cloudprofiler.googleapis.com`. Grant the `Cloud Profiler Agent` role to your service account.
Ensure `googlecloudprofiler.start()` is invoked near the entry point of your application, ideally before any significant application logic, and from the main thread.
Set the `project_id` argument in `googlecloudprofiler.start()` and ensure your environment is authenticated (e.g., via `GOOGLE_APPLICATION_CREDENTIALS` environment variable or `gcloud auth application-default login`).
In your Dockerfile, install `build-base` (e.g., `RUN apk add --update --no-cache build-base`) in a builder stage, then copy the compiled profiler wheels to your final stage.
For full CPU profiles, set `lazy-apps = true` in your uWSGI configuration. For Wall profiles, also set `py-call-osafterfork = true` to enable custom signal handling in forked processes.
Ensure your application runs on a supported Python version (e.g., 3.7-3.11 for v4.1.0). Upgrade your Python environment or downgrade the `google-cloud-profiler` library if necessary, referring to the official documentation for compatibility.
Enable the Cloud Profiler API for your Google Cloud project via the Cloud Console or by running `gcloud services enable cloudprofiler.googleapis.com`.
Grant the `Cloud Profiler Agent` (roles/cloudprofiler.agent) IAM role to the service account or user associated with your application on the target Google Cloud project.
Ensure `service` and `service_version` are explicitly provided as string arguments to `googlecloudprofiler.start()`, e.g., `googlecloudprofiler.start(service='my-service', service_version='1.0.0')`.
Install the package using pip: `pip install google-cloud-profiler`. If running on Alpine Linux, ensure `build-base` is installed before installing the profiler package: `apk add --update --no-cache build-base && pip install google-cloud-profiler`.
Ensure your service runs continuously for at least 3 minutes, verify the `Cloud Profiler API` is enabled and the service account has the `Cloud Profiler Agent` role. If running outside of Google Cloud, explicitly pass the `project_id` parameter to `googlecloudprofiler.start()`.