sqlx_otel/connection.rs
1use std::sync::Arc;
2use std::time::Instant;
3
4use opentelemetry::KeyValue;
5use opentelemetry::metrics::Histogram;
6
7use crate::database::Database;
8use crate::pool::SharedState;
9
10/// A pooled connection instrumented for OpenTelemetry.
11///
12/// Acquired via [`Pool::acquire`](crate::Pool::acquire). `&mut PoolConnection<DB>` implements
13/// [`sqlx::Executor`], so the value plugs straight into the standard `SQLx` query builders. When
14/// the value is dropped, the connection is returned to the pool and `db.client.connection.use_time`
15/// is recorded with the elapsed time between `acquire` and `drop`.
16///
17/// Use [`with_annotations`](Self::with_annotations) / [`with_operation`](Self::with_operation) to
18/// attach per-query semantic-convention attributes – the methods are inherited via the same macro
19/// that powers the equivalents on [`Pool`](crate::Pool) and [`Transaction`](crate::Transaction).
20///
21/// # Example
22///
23/// ```no_run
24/// # #[cfg(feature = "sqlite")]
25/// # async fn _doc() -> Result<(), sqlx::Error> {
26/// # use sqlx_otel::PoolBuilder;
27/// # let pool = PoolBuilder::from(sqlx::SqlitePool::connect(":memory:").await?).build();
28/// let mut conn = pool.acquire().await?;
29/// let row: (i64,) = sqlx::query_as("SELECT 1").fetch_one(&mut conn).await?;
30/// assert_eq!(row.0, 1);
31/// # Ok(())
32/// # }
33/// ```
34pub struct PoolConnection<DB: sqlx::Database> {
35 pub(crate) inner: sqlx::pool::PoolConnection<DB>,
36 pub(crate) state: SharedState,
37 pub(crate) use_time: Arc<Histogram<f64>>,
38 pub(crate) acquired_at: Instant,
39 pub(crate) base_attrs: Vec<KeyValue>,
40}
41
42impl<DB: sqlx::Database> Drop for PoolConnection<DB> {
43 fn drop(&mut self) {
44 self.use_time
45 .record(self.acquired_at.elapsed().as_secs_f64(), &self.base_attrs);
46 }
47}
48
49impl<DB: sqlx::Database> std::fmt::Debug for PoolConnection<DB> {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 f.debug_struct("PoolConnection").finish_non_exhaustive()
52 }
53}
54
55impl<DB: Database> PoolConnection<DB> {
56 impl_with_annotations_mut!();
57}