Prometheus
Prometheus keeps metrics and answers queries about them. It gets them two ways, and oxtel does both:
- Scraping: Prometheus fetches
/metricsfrom each program every few seconds. Best for services that run all the time, and needs nothing but oxtel, without its default features. - Pushing over OTLP: the program sends its metrics to Prometheus, as it would to any backend. Best for programs that don’t run long enough to be scraped, such as jobs and command-line tools, and for programs Prometheus can’t reach.
Run it
Section titled “Run it”The files are in the repository’s examples/backends/prometheus/; run docker compose up -d there. Prometheus is then on http://localhost:9090.
# Prometheus, on http://localhost:9090. It takes metrics pushed over OTLP,# and scrapes the targets prometheus.yml names.services: prometheus: image: prom/prometheus:v3.16.0 command: - --config.file=/etc/prometheus/prometheus.yml - --web.enable-otlp-receiver volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro ports: - "9090:9090" # Lets the scrape reach a program on your machine, on Linux as well. extra_hosts: - "host.docker.internal:host-gateway"--web.enable-otlp-receiver turns on pushing over OTLP. The configuration has a scrape target, for a program on your machine:
global: # Prometheus's default is a minute; this is quicker to try out. scrape_interval: 5s
scrape_configs: # A program on your machine serving /metrics on oxtel's port, 9464. - job_name: app static_configs: - targets: ["host.docker.internal:9464"]Scrape your program
Section titled “Scrape your program”Set these where your program runs, then start it:
# Or serve /metrics for Prometheus to scrape. 0.0.0.0 listens on every# interface, so Prometheus can reach it from its container.export OTEL_METRICS_EXPORTER=prometheusexport OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0oxtel serves /metrics on port 9464. Listening on every interface lets Prometheus reach it from its container, and lets anything on your network read it too, so in production listen on the address your Prometheus scrapes. To serve /metrics from your own axum router instead, use oxtel_tower::MetricsService. Prometheus scraping only has a whole program.
Or push over OTLP
Section titled “Or push over OTLP”# Push metrics to Prometheus over OTLP. It takes metrics only, so send just# those. The service's name becomes the `job` label.export OTEL_SERVICE_NAME=workerexport OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://localhost:9090/api/v1/otlp/v1/metricsPrometheus takes metrics only, so send only those, with the metrics endpoint alone. It expects totals, which is oxtel’s default, so leave OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE unset.
The names in Prometheus
Section titled “The names in Prometheus”Prometheus changes OpenTelemetry’s names to its own conventions, the same way whichever way the metrics come:
| In your code | In Prometheus |
|---|---|
jobs.processed, a counter | jobs_processed_total |
jobs.duration, a histogram in seconds | jobs_duration_seconds_bucket, _sum and _count |
OTEL_SERVICE_NAME=worker | the label job="worker", when pushed; scraped, job is the scrape’s name |
So rate(jobs_processed_total{job="worker"}[5m]) is the worker’s jobs a second. Metrics for your backend covers histograms’ buckets, and views that rename or drop a metric before it’s sent.
How it’s checked
Section titled “How it’s checked”On every change, CI starts this stack, pushes the background worker’s metrics with the OTLP settings, runs the scraping example with the scrape settings, and queries Prometheus for both. check.sh, beside the compose file, has the queries. CI also checks oxtel’s /metrics with Prometheus’s own promtool.