Grafana Phlare operator and user guideGrafana Phlare AgentGrafana Phlare Agent Language SupportPython


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

# 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 continous 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='', port=8081)

To test the handlers you can use the pprof tool:

# Profile the current heap memory usage
pprof -http :6060 ""

# Profile the cpu for 5 seconds
pprof -http :6060 ""