/metrics) and Prometheus (or a collector) scrapes that endpoint via HTTP GET on a schedule.
Prometheus scrapes metrics by pulling them from your application. To integrate with Prometheus you run a small HTTP server in your app that serves the
/metrics endpoint (the Prometheus client library provides this for you).What you’ll set up
- Start a Prometheus-compatible metrics server inside your app (so Prometheus can scrape
/metrics). - Configure the OpenTelemetry
PrometheusMetricReader. - Register counters, gauges, and a histogram (with explicit buckets).
- Expose application endpoints (Flask) and record metrics on request lifecycle hooks.
Prerequisites
Install the Prometheus exporter and the Prometheus client:Full example: Flask + OpenTelemetry + Prometheus
Save the following asmain.py. It contains imports, configuration, a small Flask app, and the instruments used by the application:
If you want Prometheus (running on another host or in a container) to scrape your app, bind the metrics server to an address accessible to Prometheus. In the example above
start_http_server(port=8000, addr="localhost") binds to localhost—change to 0.0.0.0 or the appropriate host if external scraping is required.Key parts explained
| Component | Purpose | Example / Notes |
|---|---|---|
| Prometheus metrics server | Exposes /metrics for Prometheus to scrape | start_http_server(port=8000, addr="localhost") |
| Metric Reader | Makes OpenTelemetry metrics available to Prometheus | PrometheusMetricReader() |
| Views & Aggregation | Apply bucket boundaries to histograms | View(..., aggregation=ExplicitBucketHistogramAggregation(boundaries=buckets)) |
| Instruments | Counters, gauges and histograms used by the app | http_requests_total, concurrent_requests, total_request_duration |
start_http_server(port=8000, addr="localhost")(from the Prometheus client library) serves metrics athttp://localhost:8000/metrics.PrometheusMetricReader()bridges OpenTelemetry metrics to the Prometheus client server.- The
ViewwithExplicitBucketHistogramAggregationconfigures histogram bucket boundaries fortotal_request_duration(milliseconds).
Exercise the application and inspect metrics
-
Start the Flask app:
-
Send a few requests to the Flask app (port 5000):
-
Request the metrics endpoint exposed for Prometheus (port 8000):
- Runtime and Python default metrics (if enabled),
- Your application counters and gauge (
http_requests_total,concurrent_requests), - Histogram buckets, count, and sum for
total_request_duration(milliseconds).
Summary
To expose OpenTelemetry metrics to Prometheus:- Start a Prometheus-compatible metrics server in your app (
prometheus_client.start_http_server). - Configure
PrometheusMetricReaderwith your MeterProvider (and views for histograms). - Register counters / gauges / histograms and record metrics via request hooks or application code.
- Point Prometheus to the app’s
/metricsendpoint (or use a scraping sidecar / collector).
Links and References
- Prometheus
- OpenTelemetry
- opentelemetry-exporter-prometheus (PyPI)
- Prometheus Python client library
- Flask Documentation