oxtel-sql 0.3.1

Database client spans and metrics for rusqlite, tokio-postgres and sqlx, following the OpenTelemetry semantic conventions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! A real PostgreSQL, through instrumented tokio-postgres clients, recording
//! into memory. Set `TEST_DATABASE_URL` (such as
//! `postgres://postgres@localhost:5432/postgres`) to run these, as CI does;
//! without it they check nothing, and say so. Each test has its own
//! connection and temporary tables, so they can share one database.

#![cfg(feature = "postgres")]

use oxtel_sdk::Resource;
use oxtel_sdk::metrics::{DB_SECONDS_BUCKETS, MeterProvider, MetricFamily, PointValue};
use oxtel_sdk::trace::{InMemorySpanExporter, Sampler, SpanData, SpanKind, Status, TracerProvider};
use oxtel_sdk::{KeyValue, Value};
use oxtel_semconv::{Instrument, attribute, metric};
use oxtel_sql::postgres::{Client, GenericClient};
use tracing::Instrument as _;
use tracing_subscriber::layer::SubscriberExt;

struct Harness {
    spans: InMemorySpanExporter,
    meters: MeterProvider,
    _guards: (
        tracing::subscriber::DefaultGuard,
        metrics::LocalRecorderGuard<'static>,
    ),
    _traces: TracerProvider,
}

/// Pipelines recording into memory, installed for this thread. The tests run
/// on a single-threaded runtime, so every call is made on it.
fn harness() -> Harness {
    let spans = InMemorySpanExporter::default();
    let traces = TracerProvider::builder()
        .with_resource(Resource::builder().without_env().build())
        .with_sampler(Sampler::AlwaysOn)
        .with_span_events(false)
        .with_simple_exporter(spans.clone())
        .build();
    let meters = MeterProvider::builder().build();
    let recorder: &'static _ = Box::leak(Box::new(meters.recorder()));
    Harness {
        spans,
        _guards: (
            tracing::subscriber::set_default(tracing_subscriber::registry().with(traces.layer())),
            metrics::set_default_local_recorder(recorder),
        ),
        meters,
        _traces: traces,
    }
}

fn database_url() -> Option<String> {
    match std::env::var("TEST_DATABASE_URL") {
        Ok(url) if !url.is_empty() => Some(url),
        // CI has a database; passing there without one would hide a broken setup.
        _ if std::env::var_os("CI").is_some() => panic!("TEST_DATABASE_URL isn't set in CI"),
        _ => {
            eprintln!("TEST_DATABASE_URL isn't set: not checking against PostgreSQL");
            None
        }
    }
}

async fn connect(url: &str) -> (Client, tokio_postgres::Config) {
    let config: tokio_postgres::Config = url.parse().expect("TEST_DATABASE_URL parses");
    let (client, connection) = config
        .connect(tokio_postgres::NoTls)
        .await
        .expect("connect to TEST_DATABASE_URL");
    tokio::spawn(connection);
    (Client::new(client, &config), config)
}

fn attr<'a>(span: &'a SpanData, key: &str) -> Option<&'a Value> {
    span.attributes
        .iter()
        .find(|kv: &&KeyValue| kv.key == key)
        .map(|kv| &kv.value)
}

fn text(span: &SpanData, key: &str) -> Option<String> {
    match attr(span, key)? {
        Value::String(s) => Some(s.to_string()),
        Value::I64(n) => Some(n.to_string()),
        other => Some(format!("{other:?}")),
    }
}

fn named<'a>(spans: &'a [SpanData], name: &str) -> Vec<&'a SpanData> {
    spans.iter().filter(|s| s.name == name).collect()
}

fn durations(meters: &MeterProvider) -> MetricFamily {
    meters
        .collect()
        .into_iter()
        .find(|f| f.name == "db.client.operation.duration")
        .expect("db.client.operation.duration recorded")
}

#[tokio::test]
async fn calls_are_spans_named_by_summary_with_the_connection() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let (client, config) = connect(&url).await;

    client
        .batch_execute("CREATE TEMP TABLE users (id SERIAL PRIMARY KEY, name TEXT)")
        .await
        .unwrap();
    client
        .execute("INSERT INTO users (name) VALUES ($1)", &[&"Ann"])
        .await
        .unwrap();
    // A literal in the text itself: it must not reach telemetry.
    client
        .batch_execute("INSERT INTO users (name) VALUES ('secret-name')")
        .await
        .unwrap();
    let request = tracing::info_span!("request");
    let row = client
        .query_one("SELECT name FROM users WHERE id = $1", &[&1i32])
        .instrument(request)
        .await
        .unwrap();
    assert_eq!(row.get::<_, String>(0), "Ann");

    let spans = h.spans.finished_spans();
    let selects = named(&spans, "SELECT users");
    assert_eq!(
        selects.len(),
        1,
        "one SELECT in {:?}",
        spans.iter().map(|s| &s.name).collect::<Vec<_>>()
    );
    let select = selects[0];
    let request = named(&spans, "request")[0];
    assert_eq!(
        select.parent_span_id,
        Some(request.context.span_id),
        "inside the caller's span"
    );
    assert_eq!(select.kind, SpanKind::Client);
    assert_eq!(
        text(select, "db.system.name").as_deref(),
        Some("postgresql")
    );
    assert_eq!(
        text(select, "db.query.summary").as_deref(),
        Some("SELECT users")
    );
    assert_eq!(
        text(select, "db.query.text").as_deref(),
        Some("SELECT name FROM users WHERE id = $1")
    );
    assert_eq!(text(select, "db.namespace").as_deref(), config.get_dbname());
    if let [tokio_postgres::config::Host::Tcp(host)] = config.get_hosts() {
        assert_eq!(
            text(select, "server.address").as_deref(),
            Some(host.as_str())
        );
        let port = config.get_ports().first().copied().unwrap_or(5432);
        assert_eq!(text(select, "server.port"), Some(port.to_string()));
    }
    assert_eq!(select.status, Status::Unset);

    for span in &spans {
        for kv in &span.attributes {
            assert!(
                !format!("{:?}", kv.value).contains("secret-name"),
                "a literal reached {:?} on {:?}",
                kv.key,
                span.name
            );
        }
    }
    let inserts: Vec<String> = named(&spans, "INSERT users")
        .iter()
        .filter_map(|s| text(s, "db.query.text"))
        .collect();
    assert_eq!(
        inserts,
        [
            "INSERT INTO users (name) VALUES ($1)",
            "INSERT INTO users (name) VALUES (?)"
        ]
    );

    let family = durations(&h.meters);
    assert_eq!(family.unit.as_deref(), Some("s"));
    let point = family
        .points
        .iter()
        .find(|p| {
            p.attributes
                .contains(&("db.query.summary".into(), "INSERT users".into()))
        })
        .expect("a series for INSERT users");
    assert!(
        point
            .attributes
            .contains(&("db.system.name".into(), "postgresql".into()))
    );
    let PointValue::Histogram(histogram) = &point.value else {
        panic!("not a histogram")
    };
    assert_eq!(histogram.count(), 2, "two inserts");
    assert_eq!(&*histogram.bounds, DB_SECONDS_BUCKETS.as_slice());
}

#[tokio::test]
async fn a_failed_call_records_its_sqlstate() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let (client, _) = connect(&url).await;
    client
        .batch_execute("CREATE TEMP TABLE keys (id INT PRIMARY KEY)")
        .await
        .unwrap();
    client
        .execute("INSERT INTO keys (id) VALUES ($1)", &[&1i32])
        .await
        .unwrap();
    let error = client
        .execute("INSERT INTO keys (id) VALUES ($1)", &[&1i32])
        .await
        .expect_err("a duplicate key");
    assert_eq!(error.code().map(|c| c.code()), Some("23505"));

    let spans = h.spans.finished_spans();
    let inserts = named(&spans, "INSERT keys");
    assert_eq!(inserts.len(), 2);
    let (ok, failed) = (inserts[0], inserts[1]);
    assert_eq!(text(ok, "error.type"), None);
    assert_eq!(ok.status, Status::Unset);
    assert_eq!(text(failed, "error.type").as_deref(), Some("23505"));
    assert_eq!(
        text(failed, "db.response.status_code").as_deref(),
        Some("23505")
    );
    assert!(
        matches!(failed.status, Status::Error { .. }),
        "{:?}",
        failed.status
    );

    let family = durations(&h.meters);
    let failures: Vec<_> = family
        .points
        .iter()
        .filter(|p| {
            p.attributes
                .contains(&("error.type".into(), "23505".into()))
        })
        .collect();
    assert_eq!(failures.len(), 1, "one series for the failure");
    assert!(
        failures[0]
            .attributes
            .contains(&("db.response.status_code".into(), "23505".into()))
    );
}

#[tokio::test]
async fn prepared_statements_keep_their_names() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let (client, _) = connect(&url).await;
    client
        .batch_execute("CREATE TEMP TABLE items (id INT, price INT)")
        .await
        .unwrap();

    let insert = client
        .prepare("INSERT INTO items (id, price) VALUES ($1, $2)")
        .await
        .unwrap();
    for id in 0..3i32 {
        client.execute(&insert, &[&id, &(id * 10)]).await.unwrap();
    }
    // One prepared outside the wrapper: it runs, but has no name to give.
    let raw = client
        .get_ref()
        .prepare("SELECT sum(price) FROM items")
        .await
        .unwrap();
    let total: i64 = client.query_one(&raw, &[]).await.unwrap().get(0);
    assert_eq!(total, 30);

    let spans = h.spans.finished_spans();
    assert_eq!(
        named(&spans, "INSERT items").len(),
        3,
        "preparing isn't a call"
    );
    let unnamed = named(&spans, "postgresql");
    assert_eq!(unnamed.len(), 1);
    assert_eq!(text(unnamed[0], "db.query.text"), None);
    assert_eq!(text(unnamed[0], "db.query.summary"), None);
}

/// Works with a client or a transaction.
async fn count(client: &impl GenericClient) -> i64 {
    client
        .query_one("SELECT count(*) FROM events", &[])
        .await
        .unwrap()
        .get(0)
}

#[tokio::test]
async fn transactions_record_begin_commit_rollback_and_savepoints() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let (mut client, _) = connect(&url).await;
    client
        .batch_execute("CREATE TEMP TABLE events (id INT)")
        .await
        .unwrap();

    let mut tx = client.transaction().await.unwrap();
    tx.execute("INSERT INTO events (id) VALUES ($1)", &[&1i32])
        .await
        .unwrap();
    let nested = tx.transaction().await.unwrap();
    nested
        .execute("INSERT INTO events (id) VALUES ($1)", &[&2i32])
        .await
        .unwrap();
    nested.rollback().await.unwrap();
    assert_eq!(count(&tx).await, 1);
    tx.commit().await.unwrap();
    assert_eq!(count(&client).await, 1);

    let spans = h.spans.finished_spans();
    let names: Vec<&str> = spans
        .iter()
        .map(|s| s.name.as_ref())
        .filter(|n| !n.starts_with("CREATE"))
        .collect();
    assert_eq!(
        names,
        [
            "BEGIN",
            "INSERT events",
            "SAVEPOINT",
            "INSERT events",
            "ROLLBACK",
            "SELECT events",
            "COMMIT",
            "SELECT events"
        ]
    );
}

#[cfg(feature = "deadpool-postgres")]
#[tokio::test]
async fn clients_from_a_pool_are_instrumented() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let config: tokio_postgres::Config = url.parse().unwrap();
    let manager = deadpool_postgres::Manager::new(config.clone(), tokio_postgres::NoTls);
    let pool = deadpool_postgres::Pool::builder(manager)
        .max_size(2)
        .build()
        .unwrap();
    let pool = oxtel_sql::postgres::Pool::new(pool, &config);

    let client = pool.get().await.unwrap();
    let one = client
        .prepare_cached("SELECT $1::INT AS one")
        .await
        .unwrap();
    let row = client.query_one(&one, &[&1i32]).await.unwrap();
    assert_eq!(row.get::<_, i32>(0), 1);
    assert_eq!(pool.status().size, 1, "the pool still works as deadpool's");

    let spans = h.spans.finished_spans();
    let selects = named(&spans, "SELECT");
    assert_eq!(selects.len(), 1);
    assert_eq!(
        text(selects[0], "db.namespace").as_deref(),
        config.get_dbname()
    );
}

#[cfg(feature = "deadpool-postgres")]
fn pool_metric(meters: &MeterProvider, name: &str, labels: &[(&str, &str)]) -> PointValue {
    let family = meters
        .collect()
        .into_iter()
        .find(|f| f.name == name)
        .unwrap_or_else(|| panic!("{name} recorded"));
    family
        .points
        .into_iter()
        .find(|p| {
            labels
                .iter()
                .all(|(k, v)| p.attributes.contains(&((*k).into(), (*v).into())))
        })
        .unwrap_or_else(|| panic!("{name} has a series for {labels:?}"))
        .value
}

#[cfg(feature = "deadpool-postgres")]
#[tokio::test]
async fn the_pool_records_its_connections() {
    use std::time::Duration;

    let Some(url) = database_url() else { return };
    let h = harness();
    let config: tokio_postgres::Config = url.parse().unwrap();
    let pool = |size, wait| {
        let manager = deadpool_postgres::Manager::new(config.clone(), tokio_postgres::NoTls);
        deadpool_postgres::Pool::builder(manager)
            .max_size(size)
            .wait_timeout(wait)
            .runtime(deadpool_postgres::Runtime::Tokio1)
            .build()
            .unwrap()
    };
    let named = oxtel_sql::postgres::Pool::new(pool(2, None), &config).with_name("orders");

    let first = named.get().await.unwrap();
    let second = named.get().await.unwrap();
    let pool_name = ("db.client.connection.pool.name", "orders");
    let count = |state| {
        pool_metric(
            &h.meters,
            "db.client.connection.count",
            &[pool_name, ("db.client.connection.state", state)],
        )
    };
    assert_eq!(count("used"), PointValue::Gauge(2.0));
    assert_eq!(count("idle"), PointValue::Gauge(0.0));
    drop(first);
    assert_eq!(count("used"), PointValue::Gauge(1.0), "one returned");
    assert_eq!(count("idle"), PointValue::Gauge(1.0));
    drop(second);
    assert_eq!(
        pool_metric(&h.meters, "db.client.connection.max", &[pool_name]),
        PointValue::Gauge(2.0)
    );
    assert_eq!(
        pool_metric(
            &h.meters,
            "db.client.connection.pending_requests",
            &[pool_name]
        ),
        PointValue::Gauge(0.0)
    );
    let histogram_count = |name| match pool_metric(&h.meters, name, &[pool_name]) {
        PointValue::Histogram(h) => h.count(),
        other => panic!("{name} isn't a histogram: {other:?}"),
    };
    assert_eq!(histogram_count("db.client.connection.wait_time"), 2);
    assert_eq!(histogram_count("db.client.connection.use_time"), 2);

    // A pool of one, already in use: the next caller times out.
    let tight = oxtel_sql::postgres::Pool::new(pool(1, Some(Duration::from_millis(50))), &config)
        .with_name("tight");
    let held = tight.get().await.unwrap();
    assert!(matches!(
        tight.get().await,
        Err(deadpool_postgres::PoolError::Timeout(_))
    ));
    drop(held);
    let tight_name = ("db.client.connection.pool.name", "tight");
    assert_eq!(
        pool_metric(&h.meters, "db.client.connection.timeouts", &[tight_name]),
        PointValue::Counter(1)
    );
    assert_eq!(
        pool_metric(
            &h.meters,
            "db.client.connection.pending_requests",
            &[tight_name]
        ),
        PointValue::Gauge(0.0),
        "the caller that timed out isn't waiting any more"
    );

    // Every name and label is one the conventions define; the durations
    // are in seconds. (oxtel records up-down counters as gauges.)
    for family in h.meters.collect() {
        if !family.name.starts_with("db.client.connection.") {
            continue;
        }
        let definition = metric::by_name(&family.name)
            .unwrap_or_else(|| panic!("{} isn't defined", family.name));
        if definition.instrument == Instrument::Histogram {
            assert_eq!(
                family.unit.as_deref(),
                Some(definition.unit),
                "{}",
                family.name
            );
        }
        for point in &family.points {
            for (key, _) in &point.attributes {
                assert!(attribute::is_defined(key), "label {key:?} isn't defined");
            }
        }
    }
}

/// `otel.*` fields steer the span itself; they aren't attributes.
fn is_instruction(key: &str) -> bool {
    key.starts_with("otel.")
}

#[tokio::test]
async fn spans_and_metrics_use_only_defined_names() {
    let Some(url) = database_url() else { return };
    let h = harness();
    let (client, _) = connect(&url).await;
    client
        .batch_execute("CREATE TEMP TABLE t (v INT PRIMARY KEY)")
        .await
        .unwrap();
    client
        .execute("INSERT INTO t (v) VALUES ($1)", &[&1i32])
        .await
        .unwrap();
    // And a failure, so its attributes are checked too.
    let _ = client
        .execute("INSERT INTO t (v) VALUES ($1)", &[&1i32])
        .await;

    let spans = h.spans.finished_spans();
    assert!(spans.len() >= 3);
    for span in &spans {
        for kv in &span.attributes {
            assert!(
                is_instruction(&kv.key) || attribute::is_defined(&kv.key),
                "span {:?} has attribute {:?}, which the conventions don't define",
                span.name,
                kv.key
            );
        }
    }
    let family = durations(&h.meters);
    let definition = metric::by_name(&family.name).expect("a defined metric");
    assert_eq!(definition.instrument, Instrument::Histogram);
    assert_eq!(family.unit.as_deref(), Some(definition.unit));
    for point in &family.points {
        for (key, _) in &point.attributes {
            assert!(attribute::is_defined(key), "label {key:?} isn't defined");
        }
    }
}