Skip to content

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.

examples/cli.rs
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)
}
Cargo.toml
[dependencies]
oxtel = "0.4"
tracing = "0.1"

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example cli # the primes up to 100
cargo run -p oxtel-examples --example cli -- 20 2>/dev/null # the results alone
cargo run -p oxtel-examples --example cli -- twenty # fails, and says why

The primes go to standard output, one per line. Standard error gets oxtel’s startup line, then the tool’s own:

Standard error
{"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}

The 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.

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 an ExitCode, as here, or an error. Either way main ends normally and the guard is dropped. std::process::exit ends 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 main drops the guard as it unwinds, so what was pending is still sent. With panic = "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.
  • Keep standard output for results. OXTEL_LOG_OUTPUT=stdout would put log lines among them.
  • Name the tool. Set OTEL_SERVICE_NAME; otherwise backends show unknown_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.