Documentation

Metrics with Prometheus

Overview

The data model can be unambiguously translated into the Prometheus Remote Write protocol without loss of features or semantics, through well-defined translations of the data, including the ability to automatically remove attributes and lower histogram resolution

Use Cases

To use metrics with Prometheus:

  1. Make sure the pool of OpenTelemetry collectors receives OTLP and exports to Prometheus Remote Write.
  2. Ensure the Collector joins service discovery with metric resources.
  3. If done correctly, the Collector then computes “up” with a staleness marker that is a special signal emitted to indicate that a previously exported metric time series no longer exists..
  4. The Collector applies a distinct external label to your metrics.

To use Prometheus directly:

from opentelemetry.exporter.prometheus import PrometheusMetricReader
from prometheus_client import start_http_server

reader = PrometheusMetricReader()
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)

start_http_server(8000)  # Prometheus will scrape this

Edit the Prometheus config to set up scraping, for example at 15 second intervals:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "my_app"
    static_configs:
      - targets: ["localhost:8000"]

Run Prometheus:

./prometheus --config.file=prometheus.yml

And with a query such as example_counter, you should see metrics with labels.

Limitations

Prometheus through OpenTelemetry has some limitations. Using Using OpenTelemetry as an intermediary format between two non-compatible formats such as Prometheus and statsd or Prometheus and collectcd. Although not explicitly impossible, these combinations are not supported.