Skip to main content

Google Cloud Profiler Python Agent

Project description

Google Cloud Python profiling agent

Python profiling agent for Google Cloud Profiler.

See Google Cloud Profiler profiling Python code for detailed documentation.

Supported OS

Linux. Profiling Python applications is supported for Linux kernels whose standard C library is implemented with glibc or with musl. For configuration information specific to Linux Alpine kernels, see Running on Linux Alpine.

Supported Python Versions

Python >= 3.7 and <= 3.11

Installation & usage

  1. Install the profiler package using PyPI:

    pip3 install google-cloud-profiler
    
  2. Enable the profiler in your application:

    import googlecloudprofiler
    
    def main():
        # Profiler initialization. It starts a daemon thread which continuously
        # collects and uploads profiles. Best done as early as possible.
        try:
            googlecloudprofiler.start(
                service='hello-profiler',
                service_version='1.0.1',
                # verbose is the logging level. 0-error, 1-warning, 2-info,
                # 3-debug. It defaults to 0 (error) if not set.
                verbose=3,
                # project_id must be set if not running on GCP.
                # project_id='my-project-id',
            )
        except (ValueError, NotImplementedError) as exc:
            print(exc)  # Handle errors here
    

Installation on Linux Alpine

The Python profiling agent has a native component. The base Alpine image for Python does not have all dependencies required to build this native component installed. To build the Python profiling agent on Alpine, one must install the package build-base.

To use the Python profiling agent on Alpine without installing additional dependencies on to the final Alpine image, one can use a two-stage build and compile the Python profiling agent in the first stage.

Here is an example of a Docker image that uses a multi-stage build to compile and install the Python profiling agent:

FROM python:3.7-alpine as builder

# Install build-base to allow for compilation of the profiling agent.
RUN apk add --update --no-cache build-base

# Compile the profiling agent, generating wheels for it.
RUN pip3 wheel --wheel-dir=/tmp/wheels google-cloud-profiler


FROM python:3.7-alpine

# Copy over the directory containing wheels for the profiling agent.
COPY --from=builder /tmp/wheels /tmp/wheels

# Install the profiling agent.
RUN pip3 install --no-index --find-links=/tmp/wheels google-cloud-profiler

# Install any other required modules or dependencies, and copy an app which
# enables the profiler as described in "Enable the profiler in your
# application".
COPY ./bench.py .

# Run the application when the docker image is run, using either CMD (as is done
# here) or ENTRYPOINT.
CMD python3 -u bench.py

Troubleshooting

Resource temporarily unavailable errors with Python

If you see the following log entries after enabling the Profiler:

BlockingIOError: [Errno 11] Resource temporarily unavailable
Exception ignored when trying to write to the signal wakeup fd

see https://cloud.google.com/profiler/docs/troubleshooting#python-blocking for the cause and the workaround.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google-cloud-profiler-4.1.0.tar.gz (32.3 kB view details)

Uploaded Source

File details

Details for the file google-cloud-profiler-4.1.0.tar.gz.

File metadata

  • Download URL: google-cloud-profiler-4.1.0.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for google-cloud-profiler-4.1.0.tar.gz
Algorithm Hash digest
SHA256 2d90f9c6d4c075ad6d43752ae39424c3d0bad63e6549a2c761881f9a235067ae
MD5 c64c226a74d8498b239db47221546413
BLAKE2b-256 44befa9b4915315c764a38b42aac1a6355bb65deb5cb8a97c1a01c0de425780d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page