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