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
//! sqlx 0.8. See the [`sqlx`](super) module for what's recorded.

use std::fmt;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use std::time::Instant;

use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use futures_util::TryStreamExt;
use sqlx::database::Database;
use sqlx::pool::PoolConnection;
use sqlx::{ConnectOptions, Connection as _, Describe, Either, Error, Execute, Executor};
use sqlx_0_8 as sqlx;

use super::{Fail, QueryCall, Recorded, Target, has_url, record_one, statement};
use crate::pool::{Lease, Waiting};
use crate::record::analyze;

/// What a query yields as it runs: a statement's result, or a row.
type Step<DB> = Either<<DB as Database>::QueryResult, <DB as Database>::Row>;

fn is_row<DB: Database>(step: &Step<DB>) -> bool {
    step.is_right()
}

fn one_row<R>(row: &Option<R>) -> u64 {
    u64::from(row.is_some())
}

fn no_rows<T>(_: &T) -> u64 {
    0
}

fn fail(error: &Error) -> Fail {
    let kind = match error {
        Error::Database(e) => return Fail::new(e.code().map(|c| c.into_owned()), Some("Database")),
        Error::Configuration(_) => "Configuration",
        Error::InvalidArgument(_) => "InvalidArgument",
        Error::Io(_) => "Io",
        Error::Tls(_) => "Tls",
        Error::Protocol(_) => "Protocol",
        Error::RowNotFound => "RowNotFound",
        Error::TypeNotFound { .. } => "TypeNotFound",
        Error::ColumnIndexOutOfBounds { .. } => "ColumnIndexOutOfBounds",
        Error::ColumnNotFound(_) => "ColumnNotFound",
        Error::ColumnDecode { .. } => "ColumnDecode",
        Error::Encode(_) => "Encode",
        Error::Decode(_) => "Decode",
        Error::AnyDriverError(_) => "AnyDriverError",
        Error::PoolTimedOut => "PoolTimedOut",
        Error::PoolClosed => "PoolClosed",
        Error::WorkerCrashed => "WorkerCrashed",
        Error::InvalidSavePointStatement => "InvalidSavePointStatement",
        Error::BeginFailed => "BeginFailed",
        _ => return Fail::new(None, None),
    };
    Fail::new(None, Some(kind))
}

/// The query's text, known before it runs in sqlx 0.8.
fn statement_of<'q, DB: Database>(query: &impl Execute<'q, DB>) -> super::Statement {
    statement(Some(analyze(query.sql())))
}

/// An instrumented sqlx pool. Run queries on `&pool`; [`begin`](Pool::begin)
/// and [`acquire`](Pool::acquire) give an instrumented transaction or
/// connection. It dereferences to sqlx's [`Pool`](sqlx::Pool).
pub struct Pool<DB: Database> {
    inner: sqlx::Pool<DB>,
    target: Arc<Target>,
}

/// A connection out of the pool, and the lease that records its use when
/// it's back. `conn` is declared first, so it's returned before the lease
/// ends.
struct Leased<DB: Database> {
    conn: PoolConnection<DB>,
    _lease: Lease,
}

impl<DB: Database> Pool<DB> {
    /// Instrument a pool. The database's name and server come from its
    /// connection options; its name in the pool metrics is
    /// `server.address:server.port/db.namespace`.
    pub fn new(pool: sqlx::Pool<DB>) -> Self {
        crate::record::describe_metrics();
        crate::pool::describe_metrics();
        let url = has_url(DB::NAME).then(|| pool.connect_options().to_url_lossy());
        let target = Target::new(
            DB::NAME,
            url.as_ref().map(|u| (u.host_str(), u.port(), u.path())),
        );
        Pool {
            inner: pool,
            target: Arc::new(target),
        }
    }

    /// Name the pool in its metrics, for a program with more than one.
    pub fn with_name(mut self, name: &str) -> Self {
        self.target = Arc::new(self.target.with_pool_name(name));
        self
    }

    /// The wrapped pool.
    pub fn get_ref(&self) -> &sqlx::Pool<DB> {
        &self.inner
    }

    /// Take a connection from the pool, instrumented.
    pub async fn acquire(&self) -> Result<Connection<DB>, Error> {
        Ok(Connection {
            leased: self.lease().await?,
            target: Arc::clone(&self.target),
        })
    }

    /// Start a transaction. Its `BEGIN`, which includes waiting for a
    /// connection, is recorded as a call.
    pub async fn begin(&self) -> Result<Transaction<'static, DB>, Error> {
        let call = QueryCall::start(&self.target, statement(Some(analyze("BEGIN"))));
        let inner = record_one(call, self.inner.begin(), no_rows, fail).await?;
        sample(&self.inner, &self.target.pool_name);
        Ok(Transaction {
            inner,
            target: Arc::clone(&self.target),
            _lease: Some(self.lease_for_use()),
        })
    }

    async fn lease(&self) -> Result<Leased<DB>, Error> {
        let name = &self.target.pool_name;
        let waiting = Waiting::start(name);
        let started = Instant::now();
        let result = self.inner.acquire().await;
        drop(waiting);
        crate::pool::record_wait(name, started, matches!(&result, Err(Error::PoolTimedOut)));
        sample(&self.inner, name);
        Ok(Leased {
            conn: result?,
            _lease: self.lease_for_use(),
        })
    }

    fn lease_for_use(&self) -> Lease {
        let (pool, name) = (self.inner.clone(), Arc::clone(&self.target.pool_name));
        Lease::start(&self.target.pool_name, move || {
            sample_returning(&pool, &name)
        })
    }
}

/// Set the connection counts and the pool's size limit.
fn sample<DB: Database>(pool: &sqlx::Pool<DB>, name: &str) {
    record(pool, name, pool.num_idle());
}

/// The counts as a connection is given back. sqlx returns it to the pool
/// in a task of its own, so it isn't idle yet; count it as idle. Where that
/// task has already run, the next sample puts it right.
fn sample_returning<DB: Database>(pool: &sqlx::Pool<DB>, name: &str) {
    record(pool, name, pool.num_idle() + 1);
}

fn record<DB: Database>(pool: &sqlx::Pool<DB>, name: &str, idle: usize) {
    let size = pool.size() as usize;
    let idle = idle.min(size);
    let max = pool.options().get_max_connections() as usize;
    crate::pool::record_counts(name, idle, size - idle, max);
}

impl<DB: Database> Clone for Pool<DB> {
    fn clone(&self) -> Self {
        Pool {
            inner: self.inner.clone(),
            target: Arc::clone(&self.target),
        }
    }
}

impl<DB: Database> Deref for Pool<DB> {
    type Target = sqlx::Pool<DB>;

    fn deref(&self) -> &sqlx::Pool<DB> {
        &self.inner
    }
}

impl<DB: Database> fmt::Debug for Pool<DB> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Pool")
            .field("target", &self.target)
            .finish_non_exhaustive()
    }
}

impl<'p, DB> Executor<'p> for &'p Pool<DB>
where
    DB: Database,
    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
{
    type Database = DB;

    fn fetch_many<'e, 'q: 'e, E>(self, query: E) -> BoxStream<'e, Result<Step<DB>, Error>>
    where
        'p: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let statement = statement_of(&query);
        Box::pin(async_stream::try_stream! {
            let mut leased = self.lease().await?;
            let call = QueryCall::start(&self.target, statement);
            let mut steps = Recorded::new(
                (&mut *leased.conn).fetch_many(query),
                call,
                is_row::<DB>,
                fail,
            );
            while let Some(step) = steps.try_next().await? {
                yield step;
            }
        })
    }

    fn fetch_optional<'e, 'q: 'e, E>(
        self,
        query: E,
    ) -> BoxFuture<'e, Result<Option<DB::Row>, Error>>
    where
        'p: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let statement = statement_of(&query);
        Box::pin(async move {
            let mut leased = self.lease().await?;
            let call = QueryCall::start(&self.target, statement);
            record_one(
                call,
                (&mut *leased.conn).fetch_optional(query),
                one_row,
                fail,
            )
            .await
        })
    }

    fn prepare_with<'e, 'q: 'e>(
        self,
        sql: &'q str,
        parameters: &'e [DB::TypeInfo],
    ) -> BoxFuture<'e, Result<DB::Statement<'q>, Error>>
    where
        'p: 'e,
    {
        (&self.inner).prepare_with(sql, parameters)
    }

    fn describe<'e, 'q: 'e>(self, sql: &'q str) -> BoxFuture<'e, Result<Describe<DB>, Error>>
    where
        'p: 'e,
    {
        (&self.inner).describe(sql)
    }
}

/// An instrumented connection out of a [`Pool`]. Run queries on
/// `&mut connection`. It dereferences to sqlx's
/// [`PoolConnection`].
pub struct Connection<DB: Database> {
    leased: Leased<DB>,
    target: Arc<Target>,
}

impl<DB: Database> Connection<DB> {
    /// Start a transaction on this connection. Its `BEGIN` is recorded as a
    /// call.
    pub async fn begin(&mut self) -> Result<Transaction<'_, DB>, Error> {
        let call = QueryCall::start(&self.target, statement(Some(analyze("BEGIN"))));
        let inner = record_one(call, (*self.leased.conn).begin(), no_rows, fail).await?;
        Ok(Transaction {
            inner,
            target: Arc::clone(&self.target),
            _lease: None,
        })
    }
}

impl<DB: Database> Deref for Connection<DB> {
    type Target = PoolConnection<DB>;

    fn deref(&self) -> &PoolConnection<DB> {
        &self.leased.conn
    }
}

impl<DB: Database> DerefMut for Connection<DB> {
    fn deref_mut(&mut self) -> &mut PoolConnection<DB> {
        &mut self.leased.conn
    }
}

impl<DB: Database> fmt::Debug for Connection<DB> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Connection")
            .field("target", &self.target)
            .finish_non_exhaustive()
    }
}

impl<'c, DB> Executor<'c> for &'c mut Connection<DB>
where
    DB: Database,
    for<'x> &'x mut DB::Connection: Executor<'x, Database = DB>,
{
    type Database = DB;

    fn fetch_many<'e, 'q: 'e, E>(self, query: E) -> BoxStream<'e, Result<Step<DB>, Error>>
    where
        'c: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let call = QueryCall::start(&self.target, statement_of(&query));
        Box::pin(Recorded::new(
            (&mut *self.leased.conn).fetch_many(query),
            call,
            is_row::<DB>,
            fail,
        ))
    }

    fn fetch_optional<'e, 'q: 'e, E>(
        self,
        query: E,
    ) -> BoxFuture<'e, Result<Option<DB::Row>, Error>>
    where
        'c: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let call = QueryCall::start(&self.target, statement_of(&query));
        Box::pin(record_one(
            call,
            (&mut *self.leased.conn).fetch_optional(query),
            one_row,
            fail,
        ))
    }

    fn prepare_with<'e, 'q: 'e>(
        self,
        sql: &'q str,
        parameters: &'e [DB::TypeInfo],
    ) -> BoxFuture<'e, Result<DB::Statement<'q>, Error>>
    where
        'c: 'e,
    {
        (&mut *self.leased.conn).prepare_with(sql, parameters)
    }

    fn describe<'e, 'q: 'e>(self, sql: &'q str) -> BoxFuture<'e, Result<Describe<DB>, Error>>
    where
        'c: 'e,
    {
        (&mut *self.leased.conn).describe(sql)
    }
}

/// An instrumented transaction, from [`Pool::begin`] or
/// [`Connection::begin`]. Run queries on `&mut transaction`. Like sqlx's,
/// dropping it without committing rolls it back, but that rollback isn't
/// recorded. It dereferences to sqlx's [`Transaction`](sqlx::Transaction).
pub struct Transaction<'c, DB: Database> {
    inner: sqlx::Transaction<'c, DB>,
    target: Arc<Target>,
    // After `inner`, so the connection is back in the pool when it ends.
    _lease: Option<Lease>,
}

impl<DB: Database> Transaction<'_, DB> {
    /// Commit, recorded as a call.
    pub async fn commit(self) -> Result<(), Error> {
        let call = QueryCall::start(&self.target, statement(Some(analyze("COMMIT"))));
        record_one(call, self.inner.commit(), no_rows, fail).await
    }

    /// Roll back, recorded as a call.
    pub async fn rollback(self) -> Result<(), Error> {
        let call = QueryCall::start(&self.target, statement(Some(analyze("ROLLBACK"))));
        record_one(call, self.inner.rollback(), no_rows, fail).await
    }
}

impl<'c, DB: Database> Deref for Transaction<'c, DB> {
    type Target = sqlx::Transaction<'c, DB>;

    fn deref(&self) -> &sqlx::Transaction<'c, DB> {
        &self.inner
    }
}

impl<'c, DB: Database> DerefMut for Transaction<'c, DB> {
    fn deref_mut(&mut self) -> &mut sqlx::Transaction<'c, DB> {
        &mut self.inner
    }
}

impl<DB: Database> fmt::Debug for Transaction<'_, DB> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Transaction")
            .field("target", &self.target)
            .finish_non_exhaustive()
    }
}

impl<'t, DB> Executor<'t> for &'t mut Transaction<'_, DB>
where
    DB: Database,
    for<'x> &'x mut DB::Connection: Executor<'x, Database = DB>,
{
    type Database = DB;

    fn fetch_many<'e, 'q: 'e, E>(self, query: E) -> BoxStream<'e, Result<Step<DB>, Error>>
    where
        't: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let call = QueryCall::start(&self.target, statement_of(&query));
        Box::pin(Recorded::new(
            (&mut *self.inner).fetch_many(query),
            call,
            is_row::<DB>,
            fail,
        ))
    }

    fn fetch_optional<'e, 'q: 'e, E>(
        self,
        query: E,
    ) -> BoxFuture<'e, Result<Option<DB::Row>, Error>>
    where
        't: 'e,
        E: 'q + Execute<'q, DB>,
    {
        let call = QueryCall::start(&self.target, statement_of(&query));
        Box::pin(record_one(
            call,
            (&mut *self.inner).fetch_optional(query),
            one_row,
            fail,
        ))
    }

    fn prepare_with<'e, 'q: 'e>(
        self,
        sql: &'q str,
        parameters: &'e [DB::TypeInfo],
    ) -> BoxFuture<'e, Result<DB::Statement<'q>, Error>>
    where
        't: 'e,
    {
        (&mut *self.inner).prepare_with(sql, parameters)
    }

    fn describe<'e, 'q: 'e>(self, sql: &'q str) -> BoxFuture<'e, Result<Describe<DB>, Error>>
    where
        't: 'e,
    {
        (&mut *self.inner).describe(sql)
    }
}