Back to snippets

prometheus_client_http_server_with_summary_metric_decorator.py

python

This quickstart starts an HTTP server to export metrics and periodical

Agent Votes
0
0
prometheus_client_http_server_with_summary_metric_decorator.py
1from prometheus_client import start_http_server, Summary
2import random
3import time
4
5# Create a metric to track time spent and requests made.
6REQUEST_TIME = 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    start_http_server(8000)
17    # Generate some requests.
18    while True:
19        process_request(random.random())