Prometheus Remote Write Metrics Exporter for OpenTelemetry
The OpenTelemetry Prometheus Remote Write Metrics Exporter allows Python applications to export OpenTelemetry metrics data directly to a Prometheus Remote Write endpoint. It is part of the `opentelemetry-python-contrib` repository and is currently in beta (0.62b0), with breaking changes possible between minor versions. The project maintains a regular release cadence, often in sync with the broader OpenTelemetry Python Contrib releases.
Warnings
- gotcha The package is in beta (indicated by `b0` in the version number). This means the API and behavior might change in future versions without adhering to strict semantic versioning, potentially introducing breaking changes.
- gotcha This exporter only handles OpenTelemetry Metrics. It does not export Traces or Logs. For those signals, you would need to configure separate exporters (e.g., OTLP exporter for traces/logs).
- gotcha The `PeriodicExportingMetricReader` exports metrics asynchronously at intervals. If your application is short-lived, you must ensure it runs long enough for at least one export cycle to complete and call `meter_provider.shutdown()` before exiting to flush any remaining metrics.
- gotcha Configuration of the Prometheus Remote Write `endpoint` and any required `headers` (e.g., for authentication or proxy routing) is crucial. Incorrect endpoint URLs or missing authentication headers are common reasons for metrics not appearing in the receiver.
Install
-
pip install opentelemetry-exporter-prometheus-remote-write
Imports
- PrometheusRemoteWriteMetricsExporter
from opentelemetry.exporter.prometheus_remote_write import PrometheusRemoteWriteMetricsExporter
- MeterProvider
from opentelemetry.sdk.metrics import MeterProvider
- PeriodicExportingMetricReader
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
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_remote_write import PrometheusRemoteWriteMetricsExporter
# Configure the Prometheus Remote Write exporter
# The endpoint should point to your Prometheus-compatible remote write receiver
# e.g., Prometheus with remote_write enabled, Mimir, Thanos, Cortex.
# Use os.environ.get for sensitive info like API keys/tokens if headers are needed.
exporter = PrometheusRemoteWriteMetricsExporter(
endpoint=os.environ.get('OTEL_PROMETHEUS_REMOTE_WRITE_ENDPOINT', 'http://localhost:9090/api/v1/write'),
# headers={'Authorization': f'Bearer {os.environ.get("PROMETHEUS_API_TOKEN", "")}'},
timeout=30
)
# Configure the MeterProvider with a PeriodicExportingMetricReader
# The reader exports metrics to the exporter at a specified interval (default 60s).
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000) # Export every 5 seconds for demonstration
meter_provider = MeterProvider(metric_readers=[reader])
# Set the global MeterProvider (optional, but good practice)
metrics.set_meter_provider(meter_provider)
# Get a meter from the provider
meter = metrics.get_meter(__name__)
# Create a counter
request_counter = meter.create_counter(
"http_requests_total",
description="Total number of HTTP requests",
unit="1"
)
# Record some observations
print("Recording metrics...")
request_counter.add(1, {"method": "GET", "path": "/home"})
time.sleep(1) # Give some time for the reader to potentially queue for export
request_counter.add(2, {"method": "POST", "path": "/data"})
time.sleep(1) # More time
request_counter.add(1, {"method": "GET", "path": "/home"})
print("Metrics recorded. Waiting for export...")
# In a real application, you'd keep the application running
# For this example, we'll wait a bit longer to ensure export happens.
time.sleep(6) # Wait for at least one export cycle (5 seconds + buffer)
# Shutdown the meter provider to ensure all buffered metrics are exported gracefully
# This is crucial in short-lived applications or before exiting.
print("Shutting down MeterProvider...")
meter_provider.shutdown()
print("Application finished.")