Skip to content

Prometheus scraping only

For a program whose metrics Prometheus collects, and that sends nothing anywhere itself. oxtel serves /metrics on a port of its own, from a thread of its own, so there’s no web framework to add. The program records metrics with the metrics crate, as usual, and one setting turns the endpoint on.

examples/prometheus.rs
use std::time::Duration;
fn main() -> Result<(), oxtel::Error> {
// First, so the metrics below find oxtel's recorder. The guard keeps
// the endpoint running.
let _telemetry = oxtel::init()?;
// Units and descriptions, for the scrape's `# UNIT` and `# HELP` lines.
metrics::describe_counter!("orders.placed", "Orders placed, by channel.");
metrics::describe_gauge!("orders.pending", "Orders placed but not yet shipped.");
metrics::describe_histogram!(
"orders.checkout.duration",
metrics::Unit::Seconds,
"How long checkout took."
);
// Stands in for a real service: an order every second, and every third
// second two of them shipped. Each scrape reads the values as they are.
let mut pending: u32 = 0;
let mut second: u64 = 0;
loop {
let channel = if second.is_multiple_of(4) {
"app"
} else {
"web"
};
metrics::counter!("orders.placed", "channel" => channel).increment(1);
let checkout = 0.05 + (second % 5) as f64 * 0.02;
metrics::histogram!("orders.checkout.duration").record(checkout);
pending += 1;
if second % 3 == 2 {
pending -= pending.min(2);
}
metrics::gauge!("orders.pending").set(pending);
std::thread::sleep(Duration::from_secs(1));
second += 1;
}
}
Cargo.toml
[dependencies]
oxtel = { version = "0.4", default-features = false }
metrics = "0.24"

Without oxtel’s default features, there’s no OTLP code in the program, and none of the HTTP and TLS crates it needs: the endpoint uses only the standard library. With the default features, the same program can also send traces, metrics and logs to a collector once OTEL_EXPORTER_OTLP_ENDPOINT is set.

From a checkout of the repository:

Terminal window
OTEL_METRICS_EXPORTER=prometheus cargo run -p oxtel-examples --example prometheus
curl localhost:9464/metrics

oxtel’s startup line says where the endpoint is listening. The program runs until it’s stopped. With nothing to send, stopping it at any moment loses nothing but the values since the last scrape, which is how Prometheus works anyway.

Each metric appears under its Prometheus name: dots become underscores, the unit becomes a suffix, and counters end in _total.

Recorded asScraped as
orders.placed, a counterorders_placed_total, one series per channel
orders.pending, a gaugeorders_pending
orders.checkout.duration, a histogram in secondsorders_checkout_duration_seconds, with _bucket, _sum and _count series

Each has a # HELP line from its description. Every series also has an otel_scope_name label, naming the crate that recorded it, and one more series, target_info, describes the program itself: its service name, host and process.

Prometheus asks for OpenMetrics, and gets it, with # UNIT lines, and exemplars linking a measurement to its trace when it was made inside a span. Anything else, such as curl, gets the classic text format. CI checks both formats with Prometheus’s own promtool and a strict OpenMetrics parser.

SettingWhat it does
OTEL_METRICS_EXPORTERprometheus turns the endpoint on
OTEL_EXPORTER_PROMETHEUS_HOSTthe address to listen on: localhost by default, so only the same machine can scrape. In a container, 0.0.0.0
OTEL_EXPORTER_PROMETHEUS_PORTthe port: 9464 by default
OTEL_SERVICE_NAMEthe program’s name, as target_info shows it

A program that already serves HTTP, such as an axum service, can serve /metrics on its own port instead, beside its routes. oxtel_tower::MetricsService is a tower service that answers each request with a scrape, rendered by an oxtel::prometheus::PrometheusExporter made from the guard’s meter provider. Build oxtel with oxtel::builder().with_pull_metrics(), so it keeps metrics without an exporter of its own. See its documentation.

  • Listen where the scraper can reach. In a container, set OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0.
  • Keep label values few. Each distinct value makes a series of its own. A channel or an outcome is fine; a user or order id isn’t.
  • Name the service. OTEL_SERVICE_NAME is how target_info tells your programs apart.