@@ -16,6 +16,8 @@ pub mod postgres;
1616#[ cfg( feature = "sqlite" ) ]
1717pub mod sqlite;
1818
19+ /// Attributes describing the database connection and context.
20+ /// Used for span enrichment and attribute propagation.
1921#[ derive( Debug , Default ) ]
2022struct Attributes {
2123 name : Option < String > ,
@@ -24,13 +26,18 @@ struct Attributes {
2426 database : Option < String > ,
2527}
2628
29+ /// Builder for constructing a [`Pool`] with custom attributes.
30+ ///
31+ /// Allows setting database name, host, port, and other identifying information
32+ /// for tracing purposes.
2733#[ derive( Debug ) ]
2834pub struct PoolBuilder < DB : sqlx:: Database > {
2935 pool : sqlx:: Pool < DB > ,
3036 attributes : Attributes ,
3137}
3238
3339impl < DB : sqlx:: Database > PoolBuilder < DB > {
40+ /// Create a new builder from an existing SQLx pool.
3441 pub fn new ( pool : sqlx:: Pool < DB > ) -> Self {
3542 let url = pool. connect_options ( ) . to_url_lossy ( ) ;
3643 let attributes = Attributes {
@@ -44,26 +51,31 @@ impl<DB: sqlx::Database> PoolBuilder<DB> {
4451 Self { pool, attributes }
4552 }
4653
54+ /// Set a custom name for the pool (for peer.service attribute).
4755 pub fn with_name ( mut self , name : impl Into < String > ) -> Self {
4856 self . attributes . name = Some ( name. into ( ) ) ;
4957 self
5058 }
5159
60+ /// Set the database name attribute.
5261 pub fn with_database ( mut self , database : impl Into < String > ) -> Self {
5362 self . attributes . database = Some ( database. into ( ) ) ;
5463 self
5564 }
5665
66+ /// Set the host attribute.
5767 pub fn with_host ( mut self , host : impl Into < String > ) -> Self {
5868 self . attributes . host = Some ( host. into ( ) ) ;
5969 self
6070 }
6171
72+ /// Set the port attribute.
6273 pub fn with_port ( mut self , port : u16 ) -> Self {
6374 self . attributes . port = Some ( port) ;
6475 self
6576 }
6677
78+ /// Build the [`Pool`] with the configured attributes.
6779 pub fn build ( self ) -> Pool < DB > {
6880 Pool {
6981 inner : self . pool ,
@@ -72,7 +84,9 @@ impl<DB: sqlx::Database> PoolBuilder<DB> {
7284 }
7385}
7486
75- /// An asynchronous pool of SQLx database connections.
87+ /// An asynchronous pool of SQLx database connections with tracing instrumentation.
88+ ///
89+ /// Wraps a SQLx [`Pool`] and propagates tracing attributes to all acquired connections.
7690#[ derive( Clone , Debug ) ]
7791pub struct Pool < DB >
7892where
@@ -86,6 +100,7 @@ impl<DB> From<sqlx::Pool<DB>> for Pool<DB>
86100where
87101 DB : sqlx:: Database ,
88102{
103+ /// Convert a SQLx [`Pool`] into a tracing-instrumented [`Pool`].
89104 fn from ( inner : sqlx:: Pool < DB > ) -> Self {
90105 PoolBuilder :: new ( inner) . build ( )
91106 }
@@ -96,14 +111,16 @@ where
96111 DB : sqlx:: Database ,
97112{
98113 /// Retrieves a connection and immediately begins a new transaction.
114+ ///
115+ /// The returned [`Transaction`] is instrumented for tracing.
99116 pub async fn begin < ' c > ( & ' c self ) -> Result < Transaction < ' c , DB > , sqlx:: Error > {
100117 self . inner . begin ( ) . await . map ( |inner| Transaction {
101118 inner,
102119 attributes : self . attributes . clone ( ) ,
103120 } )
104121 }
105122
106- /// Retrieves a connection and immediately begins a new transaction .
123+ /// Acquires a pooled connection, instrumented for tracing .
107124 pub async fn acquire ( & self ) -> Result < PoolConnection < DB > , sqlx:: Error > {
108125 self . inner . acquire ( ) . await . map ( |inner| PoolConnection {
109126 attributes : self . attributes . clone ( ) ,
@@ -112,6 +129,9 @@ where
112129 }
113130}
114131
132+ /// Wrapper for a mutable SQLx connection reference with tracing attributes.
133+ ///
134+ /// Used internally for transaction and pool connection executors.
115135pub struct Connection < ' c , DB >
116136where
117137 DB : sqlx:: Database ,
@@ -126,6 +146,9 @@ impl<'c, DB: sqlx::Database> std::fmt::Debug for Connection<'c, DB> {
126146 }
127147}
128148
149+ /// A pooled SQLx connection instrumented for tracing.
150+ ///
151+ /// Implements [`sqlx::Executor`] and propagates tracing attributes.
129152#[ derive( Debug ) ]
130153pub struct PoolConnection < DB >
131154where
@@ -135,7 +158,9 @@ where
135158 attributes : Arc < Attributes > ,
136159}
137160
138- /// An in-progress database transaction or savepoint.
161+ /// An in-progress database transaction or savepoint, instrumented for tracing.
162+ ///
163+ /// Wraps a SQLx [`Transaction`] and propagates tracing attributes.
139164#[ derive( Debug ) ]
140165pub struct Transaction < ' c , DB >
141166where
0 commit comments