Skip to main content
So far in this lesson we’ve used the console metric exporter for quick debugging and learning. In production, you’ll usually expose metrics to a backend such as Prometheus so you can centralize scraping, alerting, and long-term storage. Prometheus uses a pull model: your application runs a small HTTP endpoint (commonly /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).
This guide shows how to configure OpenTelemetry to expose metrics via the Prometheus exporter, create a few instruments, and hook everything into a Flask app.

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 as main.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

ComponentPurposeExample / Notes
Prometheus metrics serverExposes /metrics for Prometheus to scrapestart_http_server(port=8000, addr="localhost")
Metric ReaderMakes OpenTelemetry metrics available to PrometheusPrometheusMetricReader()
Views & AggregationApply bucket boundaries to histogramsView(..., aggregation=ExplicitBucketHistogramAggregation(boundaries=buckets))
InstrumentsCounters, gauges and histograms used by the apphttp_requests_total, concurrent_requests, total_request_duration
Notes on the example:
  • start_http_server(port=8000, addr="localhost") (from the Prometheus client library) serves metrics at http://localhost:8000/metrics.
  • PrometheusMetricReader() bridges OpenTelemetry metrics to the Prometheus client server.
  • The View with ExplicitBucketHistogramAggregation configures histogram bucket boundaries for total_request_duration (milliseconds).

Exercise the application and inspect metrics

  1. Start the Flask app:
  2. Send a few requests to the Flask app (port 5000):
  3. Request the metrics endpoint exposed for Prometheus (port 8000):
You should see Prometheus-formatted metrics similar to the excerpt below:
This output shows:
  • 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:
  1. Start a Prometheus-compatible metrics server in your app (prometheus_client.start_http_server).
  2. Configure PrometheusMetricReader with your MeterProvider (and views for histograms).
  3. Register counters / gauges / histograms and record metrics via request hooks or application code.
  4. Point Prometheus to the app’s /metrics endpoint (or use a scraping sidecar / collector).

Watch Video

Practice Lab