Skip to content

A library crate

A library shouldn’t decide where telemetry goes: the application that uses it does. So a library depends on tracing and metrics, which only record, and on nothing from oxtel. Whatever the application sets up receives the library’s spans, log lines and metrics: oxtel, or anything else built on the same two crates. If the application sets up nothing, they cost next to nothing.

Here the library is shipping, which quotes the price of sending a parcel, and the application asks it for three quotes.

It depends on these, and nothing else:

examples/shipping/Cargo.toml
[dependencies]
metrics = "0.24"
tracing = "0.1"
examples/shipping/src/lib.rs
use std::fmt;
/// Why a parcel can't be quoted.
#[derive(Debug)]
pub enum Error {
/// The country isn't one this library ships to.
UnknownCountry(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::UnknownCountry(country) => write!(f, "no shipping to {country}"),
}
}
}
impl std::error::Error for Error {}
/// The price, in cents, of sending a parcel of `grams` to `country`.
///
/// Each call is a span named `quote`. A parcel heavy enough for the
/// freight surcharge is logged as a warning, and every call is counted in
/// `shipping.quotes`, by outcome.
#[tracing::instrument]
pub fn quote(grams: u32, country: &str) -> Result<u32, Error> {
let (base, per_kilogram) = match country {
"FR" | "DE" | "NL" => (450, 120),
"US" | "CA" => (1200, 300),
_ => {
metrics::counter!("shipping.quotes", "outcome" => "unknown_country").increment(1);
// Returned, not logged: the caller decides whether it matters.
return Err(Error::UnknownCountry(country.to_string()));
}
};
let mut cents = base + grams.div_ceil(1000) * per_kilogram;
if grams > 20_000 {
tracing::warn!("over 20 kg; adding the freight surcharge");
cents += 2500;
}
// Details, for when someone turns this library's logs up to `debug`.
tracing::debug!(base, per_kilogram, cents, "priced");
metrics::counter!("shipping.quotes", "outcome" => "ok").increment(1);
Ok(cents)
}

It depends on oxtel and the library, and on tracing for log lines of its own:

Cargo.toml
[dependencies]
oxtel = "0.4"
shipping = { path = "shipping" }
tracing = "0.1"
examples/library.rs
fn main() -> Result<(), oxtel::Error> {
// The application chooses: log lines here, and traces, metrics and
// logs to a collector once an endpoint is set. Keep the guard.
let _telemetry = oxtel::init()?;
for (grams, country) in [(800, "FR"), (24_000, "US"), (500, "AQ")] {
match shipping::quote(grams, country) {
Ok(cents) => tracing::info!(grams, country, cents, "quoted"),
Err(e) => tracing::warn!(grams, country, error = %e, "can't quote"),
}
}
Ok(())
}

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example library
RUST_LOG=info,shipping=debug cargo run -p oxtel-examples --example library # the library's details too
Standard error
{"timestamp":"2026-11-20T21:07:32.086159Z","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:32.086228Z","level":"INFO","target":"library","message":"quoted","grams":800,"country":"FR","cents":570}
{"timestamp":"2026-11-20T21:07:32.086244Z","level":"WARN","target":"shipping","message":"over 20 kg; adding the freight surcharge","trace_id":"459bb81dafa13e5c700253b92f41f8e9","span_id":"d87cbfdc9c6c139d","grams":24000,"country":"US"}
{"timestamp":"2026-11-20T21:07:32.086297Z","level":"INFO","target":"library","message":"quoted","grams":24000,"country":"US","cents":10900}
{"timestamp":"2026-11-20T21:07:32.086335Z","level":"WARN","target":"library","message":"can't quote","grams":500,"country":"AQ","error":"no shipping to AQ"}

The library’s warning carries its target, shipping, and its span, quote, with the call’s arguments and a trace id. The application’s lines carry its own target, library. The library’s debug line appears only when the application asks for it, as the second command above does. CI runs the example and checks it still prints these lines.

  • Depend on tracing and metrics only. Don’t call oxtel::init(), or install a subscriber or a recorder: a program can have only one of each, and choosing it is the application’s job.
  • A span for each operation a caller would want to see, at info, and details at debug or trace. Every line has the crate’s name as its target, so an application can turn the library up or down on its own: RUST_LOG=info,shipping=debug, or shipping=off.
  • Return errors rather than logging them. The caller decides whether a failure matters, and logs it if it does. This library logs only what its caller couldn’t see otherwise: the surcharge.
  • Name metrics after the library, such as shipping.quotes rather than quotes, and keep their attribute values few.
  • Describe metrics after the application has set up. metrics::describe_counter! and its neighbours reach only a recorder that’s already installed. A library can offer a function for the application to call after oxtel::init().
  • Test the telemetry with oxtel-sdk as a dev-dependency, as in Testing your telemetry. A dev-dependency doesn’t reach the library’s users.

The library’s spans and metrics arrive with the instrumentation scope shipping, the crate their target starts with, so a backend can tell them apart from the application’s. In a Prometheus scrape, that’s the otel_scope_name label.

Where the OpenTelemetry conventions name an attribute, such as http.request.method or db.system.name, use their name, so backends understand it. oxtel-semconv has every name as a constant, but a library that wants no oxtel dependency at all can write the names as strings.