Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 42afb63

Browse files
authored
Merge pull request #347 from grafana/20221021_docs-python
Add Python language examples
2 parents d3e6ded + 7149fc6 commit 42afb63

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

docs/sources/operators-guide/configure-agent/language-support/_index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ weight: 20
1010
The following languages provide support for profiles in the pprof format:
1111

1212
{{< section menuTitle="true" >}}
13+
14+
If you're interested in integrating other ecosystems, please reach out to us on [github](https://github.com/grafana/phlare/discussions/293).
15+
Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
11
---
22
title: "Python"
3-
menuTitle: "Python (Coming Soon)"
4-
description: ""
3+
menuTitle: "Python"
4+
description: "Instrumenting Pyhton applications for continuous profiling"
55
weight: 50
66
---
77

8-
Coming Soon.
8+
# Python
99

10-
If you're interested into Python integration, please reach out to us on [github](https://github.com/grafana/phlare/discussions/293).
10+
The Python module [pypprof] adds HTTP-based endpoints similar like Go's [`net/http/pprof`] for collecting profiles from a Python application.
11+
12+
Under the hood, it uses [zprofile] and [mprofile] to collect CPU and heap profiles with minimal overhead.
13+
14+
[`net/http/pprof`]: https://golang.org/pkg/net/http/pprof/
15+
[pypprof]: https://github.com/timpalpant/pypprof
16+
[zprofile]: https://github.com/timpalpant/zprofile
17+
[mprofile]: https://github.com/timpalpant/mprofile
18+
19+
## How to instrument your application
20+
21+
First of all required Python modules need to be installed:
22+
23+
```shell
24+
# Add the modules to the requirements.txt file
25+
cat >> requirements.txt <<EOF
26+
mprofile==0.0.14
27+
protobuf==3.20.3
28+
pypprof==0.0.1
29+
six==1.16.0
30+
zprofile==1.0.12
31+
EOF
32+
33+
# Build the module's wheels locally
34+
pip3 wheel --wheel-dir=/tmp/wheels -r requirements.txt
35+
36+
# Install the modules
37+
pip3 install --no-index --find-links=/tmp/wheels -r requirements.txt
38+
```
39+
40+
Now the initialization code of your application should be invoking the web server exposing the profiling data:
41+
42+
```python
43+
# import continous profiling modules
44+
from pypprof.net_http import start_pprof_server
45+
import mprofile
46+
47+
# start memory profiling
48+
mprofile.start(sample_rate=128 * 1024)
49+
50+
# enable pprof http server
51+
start_pprof_server(host='0.0.0.0', port=8081)
52+
```
53+
54+
To test the handlers you can use the [pprof] tool:
55+
56+
```shell
57+
# Profile the current heap memory usage
58+
pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/heap"
59+
60+
# Profile the cpu for 5 seconds
61+
pprof -http :6060 "http://127.0.0.1:8081/debug/pprof/profile?seconds=5"
62+
```
63+
64+
[pprof]: https://github.com/google/pprof

0 commit comments

Comments
 (0)