1
1
# Prometheus Python Client
2
2
3
- This client is under active development.
3
+ The official Python 2 and 3 client for [ Prometheus] ( http://prometheus.io ) .
4
+
5
+ ## Three Step Demo
6
+
7
+ ** One** : Install the client:
8
+ ```
9
+ pip install prometheus_client
10
+ ```
11
+
12
+ ** Two** : Paste the following into a Python interpreter:
13
+ ``` python
14
+ from prometheus_client import start_http_server,Summary
15
+ import random
16
+ import time
17
+
18
+ # Create a metric to track time spent and requests made.
19
+ REQUEST_TIME = Summary(' request_processing_seconds' , ' Time spent processing request' )
20
+
21
+ # Decorate function with metric.
22
+ @REQUEST_TIME.time ()
23
+ def process_request (t ):
24
+ """ A dummy function that takes some time."""
25
+ time.sleep(t)
26
+
27
+ if __name__ == ' __main__' :
28
+ # Start up the server to expose the metrics.
29
+ start_http_server(8000 )
30
+ # Generate some requests.
31
+ while True :
32
+ process_request(random.random())
33
+ ```
34
+
35
+ ** Three** : Visit [ http://localhost:8000/ ] ( http://localhost:8000/ ) to view the metrics.
36
+
37
+ From one easy to use decorator you get:
38
+ * ` request_processing_seconds_count ` : Number of times this function was called.
39
+ * ` request_processing_seconds_sum ` : Total amount of time spent in this function.
40
+
41
+ Prometheus's ` rate ` function allows calculation of both requests per second,
42
+ and latency over time from this data.
43
+
44
+ In addition if you're on Linux the ` process ` metrics expose CPU, memory and
45
+ other information about the process for free!
4
46
5
47
## Installation
6
48
7
49
```
8
50
pip install prometheus_client
9
51
```
10
52
11
- This package can be found on [ PyPI] ( https://pypi.python.org/pypi/prometheus_client ) .
53
+ This package can be found on
54
+ [ PyPI] ( https://pypi.python.org/pypi/prometheus_client ) .
12
55
13
56
## Instrumenting
14
57
@@ -48,7 +91,6 @@ with c.count_exceptions(ValueError):
48
91
49
92
Gauges can go up and down.
50
93
51
-
52
94
``` python
53
95
from prometheus_client import Gauge
54
96
g = Gauge(' my_inprogress_requests' , ' Description of gauge' )
@@ -136,7 +178,7 @@ c.labels('post', '/submit').inc()
136
178
### Process Collector
137
179
138
180
The Python Client automatically exports metrics about process CPU usage, RAM,
139
- file descriptors and start time. These all have the prefix ` process_ ` , and
181
+ file descriptors and start time. These all have the prefix ` process ` , and
140
182
are only currently available on Linux.
141
183
142
184
The namespace and pid constructor arguments allows for exporting metrics about
@@ -149,32 +191,24 @@ ProcessCollector(namespace='mydaemon', pid=lambda: open('/var/run/daemon.pid').r
149
191
150
192
There are several options for exporting metrics.
151
193
152
- ## HTTP handler
194
+ ## HTTP
153
195
154
- Metrics are usually exposed over HTTP, to be read by the Prometheus server. For example:
196
+ Metrics are usually exposed over HTTP, to be read by the Prometheus server.
155
197
156
- Python 2:
198
+ The easiest way to do this is via ` start_http_server ` , which will start a HTTP
199
+ server in a daemon thread on the given port:
157
200
158
201
``` python
159
- from prometheus_client import MetricsHandler
160
- from BaseHTTPServer import HTTPServer
161
- server_address = (' ' , 8000 )
162
- httpd = HTTPServer(server_address, MetricsHandler)
163
- httpd.serve_forever()
164
- ```
165
-
166
- Python 3:
167
-
168
- ``` python
169
- from prometheus_client import MetricsHandler
170
- from http.server import HTTPServer
171
- server_address = (' ' , 8000 )
172
- httpd = HTTPServer(server_address, MetricsHandler)
173
- httpd.serve_forever()
202
+ from prometheus_client import start_http_server
203
+ start_http_server(8000 )
174
204
```
175
205
176
206
Visit [ http://localhost:8000/ ] ( http://localhost:8000/ ) to view the metrics.
177
207
208
+ To add Prometheus exposition to an existing HTTP server, see the ` MetricsServlet ` class
209
+ which provides a ` BaseHTTPRequestHandler ` . It also serves as a simple example of how
210
+ to write a custom endpoint.
211
+
178
212
## Node exporter textfile collector
179
213
180
214
The [ textfile collector] ( https://github.com/prometheus/node_exporter#textfile-collector )
0 commit comments