{"id":4998,"library":"opentelemetry-exporter-prometheus-remote-write","title":"Prometheus Remote Write Metrics Exporter for OpenTelemetry","description":"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.","status":"active","version":"0.62b0","language":"en","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/exporter/opentelemetry-exporter-prometheus-remote-write","tags":["opentelemetry","metrics","prometheus","remote-write","exporter","observability"],"install":[{"cmd":"pip install opentelemetry-exporter-prometheus-remote-write","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"Required for core OpenTelemetry SDK functionalities, including MeterProvider and MetricReader.","package":"opentelemetry-sdk"},{"reason":"Required for OpenTelemetry API definitions, such as Meter and Counter.","package":"opentelemetry-api"},{"reason":"Used internally for handling Prometheus specific data structures and serialization.","package":"prometheus_client"},{"reason":"Required for serializing metrics into Prometheus Remote Write protocol buffer format.","package":"protobuf"}],"imports":[{"symbol":"PrometheusRemoteWriteMetricsExporter","correct":"from opentelemetry.exporter.prometheus_remote_write import PrometheusRemoteWriteMetricsExporter"},{"symbol":"MeterProvider","correct":"from opentelemetry.sdk.metrics import MeterProvider"},{"symbol":"PeriodicExportingMetricReader","correct":"from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader"}],"quickstart":{"code":"import os\nimport time\nfrom opentelemetry import metrics\nfrom opentelemetry.sdk.metrics import MeterProvider\nfrom opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader\nfrom opentelemetry.exporter.prometheus_remote_write import PrometheusRemoteWriteMetricsExporter\n\n# Configure the Prometheus Remote Write exporter\n# The endpoint should point to your Prometheus-compatible remote write receiver\n# e.g., Prometheus with remote_write enabled, Mimir, Thanos, Cortex.\n# Use os.environ.get for sensitive info like API keys/tokens if headers are needed.\nexporter = PrometheusRemoteWriteMetricsExporter(\n    endpoint=os.environ.get('OTEL_PROMETHEUS_REMOTE_WRITE_ENDPOINT', 'http://localhost:9090/api/v1/write'),\n    # headers={'Authorization': f'Bearer {os.environ.get(\"PROMETHEUS_API_TOKEN\", \"\")}'},\n    timeout=30\n)\n\n# Configure the MeterProvider with a PeriodicExportingMetricReader\n# The reader exports metrics to the exporter at a specified interval (default 60s).\nreader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000) # Export every 5 seconds for demonstration\nmeter_provider = MeterProvider(metric_readers=[reader])\n\n# Set the global MeterProvider (optional, but good practice)\nmetrics.set_meter_provider(meter_provider)\n\n# Get a meter from the provider\nmeter = metrics.get_meter(__name__)\n\n# Create a counter\nrequest_counter = meter.create_counter(\n    \"http_requests_total\",\n    description=\"Total number of HTTP requests\",\n    unit=\"1\"\n)\n\n# Record some observations\nprint(\"Recording metrics...\")\nrequest_counter.add(1, {\"method\": \"GET\", \"path\": \"/home\"})\ntime.sleep(1) # Give some time for the reader to potentially queue for export\nrequest_counter.add(2, {\"method\": \"POST\", \"path\": \"/data\"})\ntime.sleep(1) # More time\nrequest_counter.add(1, {\"method\": \"GET\", \"path\": \"/home\"})\n\nprint(\"Metrics recorded. Waiting for export...\")\n# In a real application, you'd keep the application running\n# For this example, we'll wait a bit longer to ensure export happens.\ntime.sleep(6) # Wait for at least one export cycle (5 seconds + buffer)\n\n# Shutdown the meter provider to ensure all buffered metrics are exported gracefully\n# This is crucial in short-lived applications or before exiting.\nprint(\"Shutting down MeterProvider...\")\nmeter_provider.shutdown()\nprint(\"Application finished.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Always pin to a specific beta version (`==0.62b0`) and thoroughly test when upgrading to newer beta releases. Monitor OpenTelemetry Python Contrib release notes for changes.","message":"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.","severity":"gotcha","affected_versions":"0.62b0 and earlier beta versions"},{"fix":"Ensure you understand which signals each exporter handles. If you need to export multiple signal types, configure a dedicated exporter for each.","message":"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).","severity":"gotcha","affected_versions":"All versions"},{"fix":"For short-lived scripts, add `time.sleep()` calls after recording metrics and always call `meter_provider.shutdown()` at the end of your application's lifecycle to guarantee metric delivery.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Double-check the `endpoint` URL. If your Prometheus remote write receiver requires authentication or specific routing headers, pass them via the `headers` dictionary to the `PrometheusRemoteWriteMetricsExporter` constructor. Test connectivity and authentication independently if issues arise.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}