|
1 | 1 | ---
|
2 |
| -title: "client_python" |
| 2 | +title: Three Step Demo |
| 3 | +weight: 1 |
3 | 4 | ---
|
4 | 5 |
|
5 |
| -This is the documentation for the [Prometheus Python client library](https://github.com/prometheus/client_python). |
| 6 | +This tutorial shows the quickest way to get started with the Prometheus Python library. |
| 7 | + |
| 8 | +**One**: Install the client: |
| 9 | +``` |
| 10 | +pip install prometheus-client |
| 11 | +``` |
| 12 | + |
| 13 | +**Two**: Paste the following into a Python interpreter: |
| 14 | +```python |
| 15 | +from prometheus_client import start_http_server, Summary |
| 16 | +import random |
| 17 | +import time |
| 18 | + |
| 19 | +# Create a metric to track time spent and requests made. |
| 20 | +REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request') |
| 21 | + |
| 22 | +# Decorate function with metric. |
| 23 | +@REQUEST_TIME.time() |
| 24 | +def process_request(t): |
| 25 | + """A dummy function that takes some time.""" |
| 26 | + time.sleep(t) |
| 27 | + |
| 28 | +if __name__ == '__main__': |
| 29 | + # Start up the server to expose the metrics. |
| 30 | + start_http_server(8000) |
| 31 | + # Generate some requests. |
| 32 | + while True: |
| 33 | + process_request(random.random()) |
| 34 | +``` |
| 35 | + |
| 36 | +**Three**: Visit [http://localhost:8000/](http://localhost:8000/) to view the metrics. |
| 37 | + |
| 38 | +From one easy to use decorator you get: |
| 39 | + * `request_processing_seconds_count`: Number of times this function was called. |
| 40 | + * `request_processing_seconds_sum`: Total amount of time spent in this function. |
| 41 | + |
| 42 | +Prometheus's `rate` function allows calculation of both requests per second, |
| 43 | +and latency over time from this data. |
| 44 | + |
| 45 | +In addition if you're on Linux the `process` metrics expose CPU, memory and |
| 46 | +other information about the process for free! |
0 commit comments