Prometheus Remote Write Metrics Exporter for OpenTelemetry

0.62b0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to configure the Prometheus Remote Write exporter with a `MeterProvider` and a `PeriodicExportingMetricReader`. It creates a simple counter and adds observations, then gracefully shuts down the provider to ensure all metrics are exported. Remember to adjust the `endpoint` to your actual Prometheus remote write receiver.

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.")

view raw JSON →