A command-line tool
A program that does one job and exits: a build step, an import, a script that cron runs. Its results go to standard output, so they can be piped into another program or saved to a file. Its log lines go to standard error, so they never get mixed into the results.
This one prints the prime numbers up to a limit, 100 unless it’s given another.
The code
Section titled “The code”use std::error::Error;use std::io::Write;use std::process::ExitCode;
fn main() -> Result<ExitCode, oxtel::Error> { // First in main, so it's dropped last, after everything that logs. // Log lines go to standard error. let _telemetry = oxtel::init()?;
let up_to = std::env::args().nth(1).unwrap_or_else(|| "100".into()); let code = match primes(&up_to) { Ok(_) => ExitCode::SUCCESS, // `primes` has already logged why, inside its span. Err(_) => ExitCode::FAILURE, }; // Either way, main returns, which drops the guard, which sends what's // still pending. `std::process::exit` would skip that. Ok(code)}
/// Writes the primes up to `up_to` on standard output, one per line, and/// returns how many there were. The run is one span: its log lines carry/// `up_to` and the run's trace id, and `err` logs a failure inside it.#[tracing::instrument(err)]fn primes(up_to: &str) -> Result<u64, Box<dyn Error>> { let up_to: u64 = up_to .parse() .map_err(|_| format!("{up_to} isn't a whole number"))?; let mut out = std::io::stdout().lock(); let mut found = 0; for n in 2..=up_to { if (2..).take_while(|d| d * d <= n).all(|d| n % d != 0) { writeln!(out, "{n}")?; found += 1; } } tracing::info!(found, "done"); Ok(found)}Dependencies
Section titled “Dependencies”[dependencies]oxtel = "0.4"tracing = "0.1"Run it
Section titled “Run it”From a checkout of the repository:
cargo run -p oxtel-examples --example cli # the primes up to 100cargo run -p oxtel-examples --example cli -- 20 2>/dev/null # the results alonecargo run -p oxtel-examples --example cli -- twenty # fails, and says whyWhat it prints
Section titled “What it prints”The primes go to standard output, one per line. Standard error gets oxtel’s startup line, then the tool’s own:
{"timestamp":"2026-11-20T21:07:29.623883Z","level":"INFO","target":"oxtel","message":"log lines: standard error, as JSON; traces, metrics and logs: not exported (set OTEL_EXPORTER_OTLP_ENDPOINT to send them to a collector)"}{"timestamp":"2026-11-20T21:07:29.623964Z","level":"INFO","target":"cli","message":"done","trace_id":"82b6e67938ef6c606a80bad8d4f68ada","span_id":"3b0cdb47dbf75b25","up_to":"100","found":25}2026-11-20T21:07:30.066414Z INFO oxtel: log lines: standard error, as text; traces, metrics and logs: not exported (set OTEL_EXPORTER_OTLP_ENDPOINT to send them to a collector)2026-11-20T21:07:30.066522Z INFO primes{up_to=100}: cli: done found=25 trace_id=0c58d2ffe31317bb642d87586ed592aa span_id=e87e150d19256bdbThe whole run is one span, primes, so its lines carry the limit and the run’s trace id. Given something that isn’t a number, the tool logs the error inside the same span, at error, and exits with status 1. CI runs the example and checks it still prints these lines.
Exiting without losing anything
Section titled “Exiting without losing anything”Once oxtel sends telemetry somewhere, it sends traces and logs in batches, in the background, and metrics every 60 seconds. A tool that runs for a second would exit before any of that happened, if it weren’t for the guard: the value oxtel::init() returns. Dropping it sends everything still pending, and waits for that to finish.
- Return from
main, on failure too. Return anExitCode, as here, or an error. Either waymainends normally and the guard is dropped.std::process::exitends the process on the spot and drops nothing, so the last batch is lost. If you must call it, drop the guard first. - Create the guard first. Rust drops a function’s values in the reverse of the order they were made, so the guard, made first, is dropped last, after any span still open has ended and can be sent with the rest.
- A panic in
maindrops the guard as it unwinds, so what was pending is still sent. Withpanic = "abort"in the build profile, nothing is dropped. - An unreachable collector delays exit by at most 5 seconds.
oxtel::builder().with_shutdown_timeout(...)changes that.
For production
Section titled “For production”- Keep standard output for results.
OXTEL_LOG_OUTPUT=stdoutwould put log lines among them. - Name the tool. Set
OTEL_SERVICE_NAME; otherwise backends showunknown_service:and the executable’s name. - Expect one trace per run. With a span around the run, as here, a backend shows each run as one trace, with everything it logged.