Skip to content

Monitoring Python Applications with Prometheus

Professional

You can instrument your Python application with the official Prometheus Python client library and let the Glouton agent scrape its /metrics endpoint.

Terminal window
pip install prometheus-client

The snippet below defines a counter and exposes it on http://localhost:8080/metrics:

import time
from prometheus_client import Counter, start_http_server
requests_total = Counter(
"myapp_requests_total",
"Total number of processed requests.",
)
if __name__ == "__main__":
start_http_server(8080)
while True:
requests_total.inc()
time.sleep(1)

Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:

metric:
prometheus:
targets:
- url: "http://localhost:8080/metrics"
name: "my_python_application"
allow_metrics:
- "myapp_requests_total"

Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.