Skip to content

Python Prometheus

prometheus-client is the official Python client for Prometheus.


pip install prometheus-client


Here is a simple script:

from prometheus_client import start_http_server, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
def process_request(t):
    """A dummy function that takes some time."""

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    # Generate some requests.
    while True:

Then you can visit http://localhost:8000/ to view the metrics.

From one easy to use decorator you get:

  • request_processing_seconds_count: Number of times this function was called.
  • request_processing_seconds_sum: Total amount of time spent in this function.

Prometheus's rate function allows calculation of both requests per second, and latency over time from this data.

In addition if you're on Linux the process metrics expose CPU, memory and other information about the process for free.