OpenTelemetry Prometheus Exporter
The `opentelemetry-exporter-prometheus` library provides a Prometheus Metric Exporter for OpenTelemetry, allowing applications instrumented with OpenTelemetry to expose their metrics in a format that a Prometheus server can scrape. It is part of the larger OpenTelemetry Python project, currently at version 0.61b0, and is under active development with frequent releases.
Warnings
- breaking The `opentelemetry-exporter-prometheus` package is currently in 'beta' and is marked as 'experimental'. New releases may introduce breaking changes to its API or behavior before it reaches a stable (1.0) release.
- gotcha OpenTelemetry and Prometheus have different naming conventions. The exporter automatically translates OpenTelemetry metric and attribute names to comply with Prometheus rules (e.g., dots become underscores, units and `_total` suffixes are added for counters). This can lead to unexpected metric names in Prometheus.
- gotcha Prometheus primarily supports cumulative counter metrics. If OpenTelemetry metrics are configured to export as delta temporality, they may not be correctly interpreted or displayed in Prometheus.
- gotcha The direct SDK export of Prometheus metrics, while simple, may not scale well for large applications or complex deployments. It forces all metrics into a single scrape endpoint, potentially leading to performance and reliability issues.
- gotcha The PrometheusMetricReader starts an HTTP server to expose metrics, defaulting to port 9464. If another service is already using this port, the exporter will fail to start.
Install
-
pip install opentelemetry-exporter-prometheus opentelemetry-sdk prometheus_client
Imports
- PrometheusMetricReader
from opentelemetry.exporter.prometheus import PrometheusMetricReader
- MeterProvider
from opentelemetry.sdk.metrics import MeterProvider
- PeriodicExportingMetricReader
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
- Resource, SERVICE_NAME
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
- start_http_server
from prometheus_client import start_http_server
Quickstart
import os
import time
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from prometheus_client import start_http_server
# Configure the MeterProvider with the PrometheusMetricReader
# PrometheusMetricReader starts an HTTP server, typically on port 9464.
# Ensure this port is available or configure a different one.
reader = PrometheusMetricReader()
provider = MeterProvider(
resource=Resource.create({
SERVICE_NAME: os.environ.get('OTEL_SERVICE_NAME', 'my-prometheus-service')
}),
metric_readers=[reader]
)
metrics.set_meter_provider(provider)
# Start the Prometheus HTTP server for scraping
# By default, it serves on 0.0.0.0:9464/metrics
start_http_server(port=9464, addr='0.0.0.0')
# Get a meter from the MeterProvider
meter = metrics.get_meter(__name__)
# Create a counter instrument
requests_counter = meter.create_counter(
name='requests_total',
description='Total number of requests',
unit='1'
)
print("Prometheus exporter running on http://localhost:9464/metrics")
print("Press Ctrl+C to exit")
# Simulate work and increment the counter
while True:
requests_counter.add(1, {"route": "/", "method": "GET"})
time.sleep(1)