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:
- Make sure the pool of OpenTelemetry collectors receives OTLP and exports to Prometheus Remote Write.
- Ensure the Collector joins service discovery with metric resources.
- 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..
- 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.
Updated 1 day ago