oxtel-sql 0.3.0

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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//! PostgreSQL through tokio-postgres, and deadpool-postgres pools.
//!
//! tokio-postgres doesn't say what SQL a prepared statement holds, and its
//! traits are sealed, so instrumenting it means wrapping the client. Wrap it
//! where the connection is made:
//!
//! ```no_run
//! # async fn run() -> Result<(), tokio_postgres::Error> {
//! let config: tokio_postgres::Config = "host=localhost user=app dbname=shop".parse()?;
//! let (client, connection) = config.connect(tokio_postgres::NoTls).await?;
//! tokio::spawn(connection);
//! let client = oxtel_sql::postgres::Client::new(client, &config);
//!
//! let rows = client.query("SELECT name FROM users WHERE id = $1", &[&7i32]).await?;
//! # Ok(()) }
//! ```
//!
//! [`Client`] and [`Transaction`] have tokio-postgres's `query`,
//! `query_one`, `query_opt`, `execute`, `batch_execute` and `simple_query`,
//! each recorded as one database call: a client span named by the query
//! summary, with `db.namespace`, `server.address` and `server.port` from the
//! connection's configuration, and `db.client.operation.duration`. A call
//! that fails records the error's SQLSTATE as `db.response.status_code` and
//! `error.type`. Starting, committing and rolling back a transaction are
//! calls too.
//!
//! [`prepare`](Client::prepare) returns a [`Statement`] that keeps its text,
//! so calls running it are named like any other. Preparing isn't recorded
//! as a call of its own. Both types dereference to tokio-postgres's, so
//! anything else, such as `copy_in` or `query_raw`, still works, without
//! telemetry.
//!
//! Helpers that should take either a client or a transaction can be generic
//! over [`GenericClient`]. With the `deadpool-postgres` feature, [`Pool`]
//! wraps a pool so that every client taken from it is instrumented, and
//! records the conventions' connection pool metrics.

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

use tokio_postgres::error::SqlState;
use tokio_postgres::types::ToSql;
use tokio_postgres::{Error, Row, SimpleQueryMessage};
use tracing::Instrument;

use crate::record::{Analysis, Call, Failure, analyze};

const POSTGRESQL: &str = "postgresql";

/// `error.type` for a failure that has no SQLSTATE, such as a lost
/// connection.
const CLIENT_ERROR: &str = "tokio_postgres::Error";

/// Where a client's connections go, for `db.namespace`, `server.address`
/// and `server.port`. Cheap to clone.
#[derive(Clone, Debug, Default)]
pub struct Database(Arc<Target>);

#[derive(Debug, Default)]
struct Target {
    namespace: Option<String>,
    server: Option<(String, u16)>,
}

impl Database {
    /// The database a configuration connects to. The server is recorded
    /// only when the configuration names exactly one host, since with
    /// several there's no telling which one answered.
    pub fn new(config: &tokio_postgres::Config) -> Self {
        crate::record::describe_metrics();
        let server = match config.get_hosts() {
            [host] => {
                let address = match host {
                    tokio_postgres::config::Host::Tcp(name) => name.clone(),
                    #[cfg(unix)]
                    tokio_postgres::config::Host::Unix(path) => path.display().to_string(),
                };
                let port = config.get_ports().first().copied().unwrap_or(5432);
                Some((address, port))
            }
            _ => None,
        };
        Database(Arc::new(Target {
            namespace: config.get_dbname().map(str::to_owned),
            server,
        }))
    }

    fn call<'a>(&'a self, analysis: &'a Analysis) -> Call<'a> {
        Call {
            system: POSTGRESQL,
            namespace: self.0.namespace.as_deref(),
            server: self.0.server.as_ref().map(|(a, p)| (a.as_str(), *p)),
            analysis,
        }
    }
}

/// Run one database call inside its span, then record its duration and,
/// if it failed, why.
async fn record<R>(
    database: &Database,
    analysis: &Analysis,
    operation: impl Future<Output = Result<R, Error>>,
) -> Result<R, Error> {
    let call = database.call(analysis);
    let span = call.span();
    let started = Instant::now();
    let result = operation.instrument(span.clone()).await;
    let seconds = started.elapsed().as_secs_f64();
    // Inside the span, so the measurement's exemplar points at it.
    let _entered = span.enter();
    match &result {
        Ok(_) => call.record_duration(seconds, None),
        Err(error) => {
            let code = error.code().map(SqlState::code);
            let failure = Failure {
                error_type: code.unwrap_or(CLIENT_ERROR),
                status_code: code,
            };
            failure.record(&span);
            call.record_duration(seconds, Some(&failure));
        }
    }
    result
}

/// A statement prepared through an instrumented client: tokio-postgres's
/// [`Statement`](tokio_postgres::Statement), keeping its text so the calls
/// that run it are named. It dereferences to tokio-postgres's.
#[derive(Clone)]
pub struct Statement {
    inner: tokio_postgres::Statement,
    analysis: Arc<Analysis>,
}

impl Statement {
    /// tokio-postgres's statement.
    pub fn into_inner(self) -> tokio_postgres::Statement {
        self.inner
    }
}

impl Deref for Statement {
    type Target = tokio_postgres::Statement;

    fn deref(&self) -> &tokio_postgres::Statement {
        &self.inner
    }
}

impl std::fmt::Debug for Statement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Statement")
            .field("summary", &self.analysis.summary)
            .finish_non_exhaustive()
    }
}

/// A statement the instrumented clients run: SQL text, a [`Statement`]
/// prepared through them, or one of tokio-postgres's own, which is recorded
/// without a name or text.
pub trait Query: sealed::Query + Sync {}

impl Query for str {}
impl Query for String {}
impl Query for Statement {}
impl Query for tokio_postgres::Statement {}

mod sealed {
    use crate::record::Analysis;

    pub enum Target<'a> {
        Text(&'a str),
        Prepared(Prepared<'a>),
    }

    /// A prepared statement and what's known about its text.
    pub struct Prepared<'a> {
        pub(crate) statement: &'a tokio_postgres::Statement,
        pub(crate) analysis: &'a Analysis,
    }

    pub trait Query {
        fn target(&self) -> Target<'_>;
    }

    impl Query for str {
        fn target(&self) -> Target<'_> {
            Target::Text(self)
        }
    }

    impl Query for String {
        fn target(&self) -> Target<'_> {
            Target::Text(self)
        }
    }

    impl Query for super::Statement {
        fn target(&self) -> Target<'_> {
            Target::Prepared(Prepared {
                statement: &self.inner,
                analysis: &self.analysis,
            })
        }
    }

    impl Query for tokio_postgres::Statement {
        fn target(&self) -> Target<'_> {
            Target::Prepared(Prepared {
                statement: self,
                analysis: &crate::record::UNKNOWN,
            })
        }
    }
}

use sealed::Target as Run;

/// The instrumented calls, on [`Client`] and [`Transaction`] alike. Each
/// type has `fn pg(&self)`, returning tokio-postgres's client or
/// transaction, and a `database` field.
macro_rules! calls {
    () => {
        /// Prepare a statement, keeping its text so that calls running it
        /// are named. Preparing isn't recorded as a call.
        pub async fn prepare(&self, query: &str) -> Result<Statement, Error> {
            let inner = self.pg().prepare(query).await?;
            Ok(Statement {
                inner,
                analysis: analyze(query),
            })
        }

        /// Run a statement and return the rows it produced.
        pub async fn query<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> Result<Vec<Row>, Error>
        where
            T: ?Sized + Query,
        {
            match statement.target() {
                Run::Text(sql) => {
                    record(&self.database, &analyze(sql), self.pg().query(sql, params)).await
                }
                Run::Prepared(p) => {
                    record(
                        &self.database,
                        p.analysis,
                        self.pg().query(p.statement, params),
                    )
                    .await
                }
            }
        }

        /// Run a statement that returns exactly one row.
        pub async fn query_one<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> Result<Row, Error>
        where
            T: ?Sized + Query,
        {
            match statement.target() {
                Run::Text(sql) => {
                    record(
                        &self.database,
                        &analyze(sql),
                        self.pg().query_one(sql, params),
                    )
                    .await
                }
                Run::Prepared(p) => {
                    record(
                        &self.database,
                        p.analysis,
                        self.pg().query_one(p.statement, params),
                    )
                    .await
                }
            }
        }

        /// Run a statement that returns at most one row.
        pub async fn query_opt<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> Result<Option<Row>, Error>
        where
            T: ?Sized + Query,
        {
            match statement.target() {
                Run::Text(sql) => {
                    record(
                        &self.database,
                        &analyze(sql),
                        self.pg().query_opt(sql, params),
                    )
                    .await
                }
                Run::Prepared(p) => {
                    record(
                        &self.database,
                        p.analysis,
                        self.pg().query_opt(p.statement, params),
                    )
                    .await
                }
            }
        }

        /// Run a statement and return how many rows it changed.
        pub async fn execute<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> Result<u64, Error>
        where
            T: ?Sized + Query,
        {
            match statement.target() {
                Run::Text(sql) => {
                    record(
                        &self.database,
                        &analyze(sql),
                        self.pg().execute(sql, params),
                    )
                    .await
                }
                Run::Prepared(p) => {
                    record(
                        &self.database,
                        p.analysis,
                        self.pg().execute(p.statement, params),
                    )
                    .await
                }
            }
        }

        /// Run statements separated by semicolons, without parameters, as
        /// one call.
        pub async fn batch_execute(&self, query: &str) -> Result<(), Error> {
            record(
                &self.database,
                &analyze(query),
                self.pg().batch_execute(query),
            )
            .await
        }

        /// Run statements through the simple query protocol, as one call.
        pub async fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
            record(
                &self.database,
                &analyze(query),
                self.pg().simple_query(query),
            )
            .await
        }
    };
}

/// An instrumented tokio-postgres client, or a pooled one (see [`AsClient`]).
/// It dereferences to tokio-postgres's [`Client`](tokio_postgres::Client),
/// so methods it doesn't wrap still work, without telemetry.
pub struct Client<C = tokio_postgres::Client> {
    inner: C,
    database: Database,
    // Declared after `inner`, so a pooled connection is back in its pool
    // when the lease ends.
    _lease: Option<Lease>,
}

// A pooled client's time out of its pool.
#[cfg(feature = "deadpool-postgres")]
use crate::pool::Lease;
#[cfg(not(feature = "deadpool-postgres"))]
type Lease = std::convert::Infallible;

/// The clients a [`Client`] can wrap: tokio-postgres's own, and with the
/// `deadpool-postgres` feature, a client taken from a deadpool pool.
pub trait AsClient {
    /// tokio-postgres's client.
    fn as_client(&self) -> &tokio_postgres::Client;
    /// tokio-postgres's client, mutably, for starting transactions.
    fn as_client_mut(&mut self) -> &mut tokio_postgres::Client;
}

impl AsClient for tokio_postgres::Client {
    fn as_client(&self) -> &tokio_postgres::Client {
        self
    }

    fn as_client_mut(&mut self) -> &mut tokio_postgres::Client {
        self
    }
}

impl<C: AsClient> Client<C> {
    /// Instrument a client connected with `config`.
    pub fn new(client: C, config: &tokio_postgres::Config) -> Self {
        Self::with_database(client, &Database::new(config))
    }

    /// Instrument a client, with what's known about where it's connected.
    /// For many clients to one database, work that out once with
    /// [`Database::new`].
    pub fn with_database(client: C, database: &Database) -> Self {
        Client {
            inner: client,
            database: database.clone(),
            _lease: None,
        }
    }

    /// The wrapped client.
    pub fn get_ref(&self) -> &C {
        &self.inner
    }

    /// The wrapped client, without the instrumentation. For a client from
    /// a [`Pool`], its use ends here as far as the pool's metrics go.
    pub fn into_inner(self) -> C {
        self.inner
    }

    fn pg(&self) -> &tokio_postgres::Client {
        self.inner.as_client()
    }

    calls!();

    /// Start a transaction. Its `BEGIN` is recorded as a call.
    pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
        let database = self.database.clone();
        let inner = record(
            &database,
            &analyze("BEGIN"),
            self.inner.as_client_mut().transaction(),
        )
        .await?;
        Ok(Transaction { inner, database })
    }
}

impl<C: AsClient> Deref for Client<C> {
    type Target = tokio_postgres::Client;

    fn deref(&self) -> &tokio_postgres::Client {
        self.inner.as_client()
    }
}

impl<C: AsClient> DerefMut for Client<C> {
    fn deref_mut(&mut self) -> &mut tokio_postgres::Client {
        self.inner.as_client_mut()
    }
}

impl<C> std::fmt::Debug for Client<C> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("database", &self.database)
            .finish_non_exhaustive()
    }
}

/// An instrumented transaction, from [`Client::transaction`]. Like
/// tokio-postgres's, dropping it without committing rolls it back, but that
/// rollback isn't recorded. It dereferences to tokio-postgres's
/// [`Transaction`](tokio_postgres::Transaction).
pub struct Transaction<'a> {
    inner: tokio_postgres::Transaction<'a>,
    database: Database,
}

impl<'a> Transaction<'a> {
    fn pg(&self) -> &tokio_postgres::Transaction<'a> {
        &self.inner
    }

    calls!();

    /// Commit, recorded as a call.
    pub async fn commit(self) -> Result<(), Error> {
        record(&self.database, &analyze("COMMIT"), self.inner.commit()).await
    }

    /// Roll back, recorded as a call.
    pub async fn rollback(self) -> Result<(), Error> {
        record(&self.database, &analyze("ROLLBACK"), self.inner.rollback()).await
    }

    /// Start a nested transaction, which is a savepoint, recorded as a call.
    pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
        let database = self.database.clone();
        let inner = record(&database, &analyze("SAVEPOINT"), self.inner.transaction()).await?;
        Ok(Transaction { inner, database })
    }
}

impl<'a> Deref for Transaction<'a> {
    type Target = tokio_postgres::Transaction<'a>;

    fn deref(&self) -> &tokio_postgres::Transaction<'a> {
        &self.inner
    }
}

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

/// The instrumented calls of a [`Client`] or a [`Transaction`], for helpers
/// that take either. tokio-postgres's own `GenericClient` is sealed.
pub trait GenericClient: Sync {
    /// See [`Client::prepare`].
    fn prepare(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send;

    /// See [`Client::query`].
    fn query<T>(
        &self,
        statement: &T,
        params: &[&(dyn ToSql + Sync)],
    ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send
    where
        T: ?Sized + Query;

    /// See [`Client::query_one`].
    fn query_one<T>(
        &self,
        statement: &T,
        params: &[&(dyn ToSql + Sync)],
    ) -> impl Future<Output = Result<Row, Error>> + Send
    where
        T: ?Sized + Query;

    /// See [`Client::query_opt`].
    fn query_opt<T>(
        &self,
        statement: &T,
        params: &[&(dyn ToSql + Sync)],
    ) -> impl Future<Output = Result<Option<Row>, Error>> + Send
    where
        T: ?Sized + Query;

    /// See [`Client::execute`].
    fn execute<T>(
        &self,
        statement: &T,
        params: &[&(dyn ToSql + Sync)],
    ) -> impl Future<Output = Result<u64, Error>> + Send
    where
        T: ?Sized + Query;

    /// See [`Client::batch_execute`].
    fn batch_execute(&self, query: &str) -> impl Future<Output = Result<(), Error>> + Send;
}

/// Implements [`GenericClient`] by calling the type's own methods.
macro_rules! generic_client {
    () => {
        fn prepare(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send {
            Self::prepare(self, query)
        }

        fn query<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send
        where
            T: ?Sized + Query,
        {
            Self::query(self, statement, params)
        }

        fn query_one<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> impl Future<Output = Result<Row, Error>> + Send
        where
            T: ?Sized + Query,
        {
            Self::query_one(self, statement, params)
        }

        fn query_opt<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> impl Future<Output = Result<Option<Row>, Error>> + Send
        where
            T: ?Sized + Query,
        {
            Self::query_opt(self, statement, params)
        }

        fn execute<T>(
            &self,
            statement: &T,
            params: &[&(dyn ToSql + Sync)],
        ) -> impl Future<Output = Result<u64, Error>> + Send
        where
            T: ?Sized + Query,
        {
            Self::execute(self, statement, params)
        }

        fn batch_execute(&self, query: &str) -> impl Future<Output = Result<(), Error>> + Send {
            Self::batch_execute(self, query)
        }
    };
}

impl<C: AsClient + Sync> GenericClient for Client<C> {
    generic_client!();
}

impl GenericClient for Transaction<'_> {
    generic_client!();
}

#[cfg(feature = "deadpool-postgres")]
mod pool {
    use super::*;

    impl AsClient for deadpool_postgres::Object {
        fn as_client(&self) -> &tokio_postgres::Client {
            self
        }

        fn as_client_mut(&mut self) -> &mut tokio_postgres::Client {
            self
        }
    }

    impl Client<deadpool_postgres::Object> {
        /// Prepare a statement through the pooled connection's statement
        /// cache, keeping its text so that calls running it are named.
        pub async fn prepare_cached(&self, query: &str) -> Result<Statement, Error> {
            let inner = self.inner.prepare_cached(query).await?;
            Ok(Statement {
                inner,
                analysis: analyze(query),
            })
        }
    }

    /// A deadpool-postgres pool whose clients are instrumented. It
    /// dereferences to deadpool's [`Pool`](deadpool_postgres::Pool).
    ///
    /// It also records the conventions' connection pool metrics, labelled
    /// with `db.client.connection.pool.name`:
    ///
    /// - `db.client.connection.count`, by `db.client.connection.state`
    ///   (`idle` or `used`), and `db.client.connection.max`;
    /// - `db.client.connection.pending_requests`: callers waiting for a
    ///   connection;
    /// - `db.client.connection.wait_time` and `db.client.connection.use_time`,
    ///   in seconds;
    /// - `db.client.connection.timeouts`.
    ///
    /// The counts are taken from the pool's status whenever a client is
    /// taken or returned. These metrics are still in development in the
    /// conventions, so their names may change. The counts carry no unit,
    /// since the `metrics` facade can't express `{connection}`.
    #[derive(Clone)]
    pub struct Pool {
        inner: deadpool_postgres::Pool,
        database: Database,
        name: Arc<str>,
    }

    impl Pool {
        /// Instrument a pool whose connections are made with `config`. Its
        /// name in the pool metrics is `server.address:server.port/db.namespace`,
        /// as the conventions suggest for a pool without one.
        pub fn new(pool: deadpool_postgres::Pool, config: &tokio_postgres::Config) -> Self {
            crate::pool::describe_metrics();
            let database = Database::new(config);
            let name = crate::pool::default_name(
                POSTGRESQL,
                database.0.namespace.as_deref(),
                database.0.server.as_ref().map(|(a, p)| (a.as_str(), *p)),
            );
            Pool {
                inner: pool,
                database,
                name: name.into(),
            }
        }

        /// Name the pool in its metrics, for a program with more than one.
        pub fn with_name(mut self, name: impl Into<String>) -> Self {
            self.name = name.into().into();
            self
        }

        /// Take a client from the pool, instrumented.
        pub async fn get(
            &self,
        ) -> Result<Client<deadpool_postgres::Object>, deadpool_postgres::PoolError> {
            let waiting = crate::pool::Waiting::start(&self.name);
            let started = Instant::now();
            let result = self.inner.get().await;
            drop(waiting);
            let timed_out = matches!(&result, Err(deadpool_postgres::PoolError::Timeout(_)));
            crate::pool::record_wait(&self.name, started, timed_out);
            sample(&self.inner, &self.name);
            let client = result?;
            let (pool, name) = (self.inner.clone(), Arc::clone(&self.name));
            Ok(Client {
                inner: client,
                database: self.database.clone(),
                _lease: Some(Lease::start(&self.name, move || sample(&pool, &name))),
            })
        }
    }

    /// Set the connection counts and the pool's size limit from its status.
    fn sample(pool: &deadpool_postgres::Pool, name: &str) {
        let status = pool.status();
        crate::pool::record_counts(
            name,
            status.available,
            status.size.saturating_sub(status.available),
            status.max_size,
        );
    }

    impl Deref for Pool {
        type Target = deadpool_postgres::Pool;

        fn deref(&self) -> &deadpool_postgres::Pool {
            &self.inner
        }
    }

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

#[cfg(feature = "deadpool-postgres")]
pub use pool::Pool;