Skip to content

Database access

A program that uses SQLite through rusqlite. One line per connection, and every statement the connection runs gets:

  • a span named for what it does, such as INSERT orders or SELECT orders;
  • the statement’s text on the span, with every value replaced by ?, so no customer data reaches your telemetry;
  • its duration, in the db.client.operation.duration metric.

This one uses an in-memory database, so there’s nothing to set up. For PostgreSQL and sqlx, see Databases.

examples/database.rs
use rusqlite::Connection;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// First, so the statements' duration metric finds oxtel's recorder.
// Keep the guard until main returns.
let _telemetry = oxtel::init()?;
let db = Connection::open_in_memory()?;
// One line per connection: every statement it runs from here on gets a
// span and a duration.
oxtel_sql::rusqlite::instrument(&db);
db.execute(
"CREATE TABLE orders (id INTEGER PRIMARY KEY, item TEXT NOT NULL, cents INTEGER NOT NULL)",
(),
)?;
for (item, cents) in [("tea", 450), ("cups", 1200), ("tea", 450)] {
db.execute(
"INSERT INTO orders (item, cents) VALUES (?1, ?2)",
(item, cents),
)?;
}
// The statements run inside this span become its children, so a trace
// shows what the report ran and how long each statement took.
let report = tracing::info_span!("report");
let _entered = report.enter();
let (orders, cents): (i64, i64) = db.query_row(
"SELECT count(*), sum(cents) FROM orders WHERE item = 'tea'",
(),
|row| Ok((row.get(0)?, row.get(1)?)),
)?;
tracing::info!(orders, cents, "tea sold");
Ok(())
}
Cargo.toml
[dependencies]
oxtel = "0.4"
oxtel-sql = { version = "0.4", features = ["rusqlite"] }
rusqlite = { version = "0.39", features = ["bundled"] }
tracing = "0.1"

rusqlite’s bundled feature builds SQLite into the program. Leave it out to use the system’s SQLite.

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example database
OTEL_TRACES_EXPORTER=console cargo run -p oxtel-examples --example database # and the spans, on standard output

The program’s own log line, inside its report span:

Standard error
{"timestamp":"2026-11-20T21:07:30.761876Z","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:30.762987Z","level":"INFO","target":"database","message":"tea sold","trace_id":"b70cd70af0f1cbea64e8d03a7f2ded77","span_id":"338513c425ca77f4","orders":2,"cents":900}

The statements don’t write log lines: they’re spans. With nothing configured they aren’t sent anywhere. OTEL_TRACES_EXPORTER=console, as in the second command above, prints each span on standard output as it ends:

Standard output
2026-11-20T21:07:31.109624Z span "CREATE TABLE orders" kind=client status=unset duration=0.047ms trace_id=4fd4e756c80b4796971c3467819ef619 span_id=975ab6dc0f5856fe db.system.name=sqlite db.query.summary="CREATE TABLE orders" db.query.text="CREATE TABLE orders (id INTEGER PRIMARY KEY, item TEXT NOT NULL, cents INTEGER NOT NULL)"
2026-11-20T21:07:31.109749Z span "INSERT orders" kind=client status=unset duration=0.014ms trace_id=dfb48fbcfc7bf6934d3a1a58b79b878e span_id=8621ccc4551dd413 db.system.name=sqlite db.query.summary="INSERT orders" db.query.text="INSERT INTO orders (item, cents) VALUES (?1, ?2)"
2026-11-20T21:07:31.109795Z span "INSERT orders" kind=client status=unset duration=0.015ms trace_id=5a326af22251caa1f578ea679cb6a1b7 span_id=e4307c2546f10b13 db.system.name=sqlite db.query.summary="INSERT orders" db.query.text="INSERT INTO orders (item, cents) VALUES (?1, ?2)"
2026-11-20T21:07:31.109829Z span "INSERT orders" kind=client status=unset duration=0.010ms trace_id=9e16abf40f24fe4211a80ec0137ac273 span_id=0ba399cc1caeedfa db.system.name=sqlite db.query.summary="INSERT orders" db.query.text="INSERT INTO orders (item, cents) VALUES (?1, ?2)"
2026-11-20T21:07:31.109883Z span "SELECT orders" kind=client status=unset duration=0.009ms trace_id=9f10ba962a3ab0fcbd674993d28be612 span_id=7b3bd32069bac2b7 parent_id=6343293518ea431c db.system.name=sqlite db.query.summary="SELECT orders" db.query.text="SELECT count(*), sum(cents) FROM orders WHERE item = ?"
2026-11-20T21:07:31.109848Z span "report" kind=internal status=unset duration=0.074ms trace_id=9f10ba962a3ab0fcbd674993d28be612 span_id=6343293518ea431c event="tea sold"
  • Each statement’s span is named by its operation and table, and marked kind=client: the program is the database’s client.
  • In the SELECT, the value 'tea' became ?. The INSERT’s values were never in its text: they were passed as parameters, ?1 and ?2.
  • The SELECT ran inside report, so it shares the report’s trace id, and its parent_id is the report’s span id. A backend shows it nested under the report. The other statements ran outside any span, so each is a trace of its own.
  • The report’s log line is kept on its span as an event, since nothing here exports log records.

CI runs the example both ways and checks it still prints these lines.

The names follow the OpenTelemetry conventions for databases.

Span nameThe statement’s summary: its operation and table, such as SELECT orders
db.system.namesqlite
db.query.summaryThe same summary
db.query.textThe statement, with every value replaced by ?
db.client.operation.durationA histogram of each statement’s duration in seconds, as SQLite timed it, by db.system.name and db.query.summary

SQLite’s hooks don’t say whether a statement failed, so these spans carry no error. Your code still sees the error from rusqlite as before.

  • Instrument every connection. instrument covers the one connection it’s given. If a pool opens connections, call it in the pool’s hook that runs for each new one.
  • Call it after oxtel::init(), so the duration metric gets its unit, seconds, and the duration buckets that go with it.
  • Leave the text out if even the shape of your statements is private: oxtel_sql::record_query_text(false).
  • Turn the spans off with RUST_LOG=info,oxtel_sql=warn: they’re info spans with the target oxtel_sql. The metric is still recorded.