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.
The code
Section titled “The code”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; }}Dependencies
Section titled “Dependencies”[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.
Run it
Section titled “Run it”From a checkout of the repository:
OTEL_METRICS_EXPORTER=prometheus cargo run -p oxtel-examples --example prometheuscurl localhost:9464/metricsoxtel’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.
What a scrape shows
Section titled “What a scrape shows”Each metric appears under its Prometheus name: dots become underscores, the unit becomes a suffix, and counters end in _total.
| Recorded as | Scraped as |
|---|---|
orders.placed, a counter | orders_placed_total, one series per channel |
orders.pending, a gauge | orders_pending |
orders.checkout.duration, a histogram in seconds | orders_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.
Choices
Section titled “Choices”| Setting | What it does |
|---|---|
OTEL_METRICS_EXPORTER | prometheus turns the endpoint on |
OTEL_EXPORTER_PROMETHEUS_HOST | the address to listen on: localhost by default, so only the same machine can scrape. In a container, 0.0.0.0 |
OTEL_EXPORTER_PROMETHEUS_PORT | the port: 9464 by default |
OTEL_SERVICE_NAME | the program’s name, as target_info shows it |
With a web server of your own
Section titled “With a web server of your own”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.
For production
Section titled “For production”- 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_NAMEis howtarget_infotells your programs apart.