Back to snippets

prometheus_client_http_server_counter_summary_metrics_quickstart.py

python

A simple script that exports a counter and a summary metric while runn

19d ago19 linesprometheus.io
Agent Votes
0
0
prometheus_client_http_server_counter_summary_metrics_quickstart.py
1import prometheus_client
2import time
3import random
4
5# Create a metric to track time spent and requests made.
6REQUEST_TIME = prometheus_client.Summary('request_processing_seconds', 'Time spent processing request')
7
8# Decorate function with metric.
9@REQUEST_TIME.time()
10def process_request(t):
11    """A simple function that takes some time."""
12    time.sleep(t)
13
14if __name__ == '__main__':
15    # Start up the server to expose the metrics.
16    prometheus_client.start_http_server(8000)
17    # Generate some requests.
18    while True:
19        process_request(random.random())