Menu

Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Open source

Python

In March 2023, Grafana Labs acquired Pyroscope, the company behind the eponymous open source continuous profiling project. As a result, the Pyroscope and Grafana Phlare projects will be merged under the new name Grafana Pyroscope. To learn more, read our recent blog post about the news.

The Python module pypprof adds HTTP-based endpoints similar like Go’s net/http/pprof for collecting profiles from a Python application.

Under the hood, it uses zprofile and mprofile to collect CPU and heap profiles with minimal overhead.

How to instrument your application

First of all required Python modules need to be installed:

# Add the modules to the requirements.txt file
cat >> requirements.txt <<EOF
mprofile==0.0.14
protobuf==3.20.3
pypprof==0.0.1
six==1.16.0
zprofile==1.0.12
EOF

# Build the module's wheels locally
pip3 wheel --wheel-dir=/tmp/wheels -r requirements.txt

# Install the modules
pip3 install --no-index --find-links=/tmp/wheels -r requirements.txt

Now the initialization code of your application should be invoking the web server exposing the profiling data:

# import continuous profiling modules
from pypprof.net_http import start_pprof_server
import mprofile

# start memory profiling
mprofile.start(sample_rate=128 * 1024)

# enable pprof http server
start_pprof_server(host='0.0.0.0', port=8081)

To test the handlers you can use the pprof tool:

# Profile the current heap memory usage
pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/heap"

# Profile the cpu for 5 seconds
pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/profile?seconds=5"

How to instrument a Django application

You can use django-pypprof, a wrapper around pypprof to add the endpoints to your Django applications. The following instructions are provided as information, refer to django-pypprof’s documentation for up-to-date instructions.

First, install the required Python modules:

# Add the modules to the requirements.txt file
cat >> requirements.txt <<EOF
--extra-index-url=https://gitlab.com/api/v4/groups/prologin/-/packages/pypi/simple
django-pypprof==1.0.0
EOF

# Build the module's wheels locally
pip3 wheel --wheel-dir=/tmp/wheels -r requirements.txt

# Install the modules
pip3 install --no-index --find-links=/tmp/wheels -r requirements.txt

In your Django settings, add django_pypprof to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django_pypprof',
    ...
]

Add the endpoints to your urls.py:

urlpatterns = [
    ...
    path('debug/pprof/', include('django_pypprof.urls')),
    ...
]

Configuration of django-pypprof

You can configure the sample rate of mprofile by adding the following setting:

PPROF_SAMPLE_RATE = 128 * 1024 # the default

Configuration of scrape targets

Use the following profiling configuration when configuring a Django scrape target:

profiling_config:
  pprof_config:
    block:
      enabled: false
    mutex:
      enabled: false
    memory:
      path: /debug/pprof/heap