@@ -38,6 +38,7 @@ use crate::{
3838 consts:: { CapabilityFlags , Command , StatusFlags } ,
3939 error:: * ,
4040 io:: Stream ,
41+ metrics:: ConnMetrics ,
4142 opts:: Opts ,
4243 queryable:: {
4344 query_result:: { QueryResult , ResultSetMeta } ,
@@ -56,6 +57,8 @@ pub mod stmt_cache;
5657
5758/// Helper that asynchronously disconnects the givent connection on the default tokio executor.
5859fn disconnect ( mut conn : Conn ) {
60+ conn. metrics ( ) . disconnects . incr ( ) ;
61+
5962 let disconnected = conn. inner . disconnected ;
6063
6164 // Mark conn as disconnected.
@@ -116,6 +119,7 @@ struct ConnInner {
116119 /// One-time connection-level infile handler.
117120 infile_handler :
118121 Option < Pin < Box < dyn Future < Output = crate :: Result < InfileData > > + Send + Sync + ' static > > > ,
122+ conn_metrics : Arc < ConnMetrics > ,
119123}
120124
121125impl fmt:: Debug for ConnInner {
@@ -137,6 +141,7 @@ impl fmt::Debug for ConnInner {
137141impl ConnInner {
138142 /// Constructs an empty connection.
139143 fn empty ( opts : Opts ) -> ConnInner {
144+ let conn_metrics: Arc < ConnMetrics > = Default :: default ( ) ;
140145 ConnInner {
141146 capabilities : opts. get_capabilities ( ) ,
142147 status : StatusFlags :: empty ( ) ,
@@ -151,7 +156,7 @@ impl ConnInner {
151156 tx_status : TxStatus :: None ,
152157 last_io : Instant :: now ( ) ,
153158 wait_timeout : Duration :: from_secs ( 0 ) ,
154- stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) ) ,
159+ stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) , conn_metrics . clone ( ) ) ,
155160 socket : opts. socket ( ) . map ( Into :: into) ,
156161 opts,
157162 nonce : Vec :: default ( ) ,
@@ -161,6 +166,7 @@ impl ConnInner {
161166 server_key : None ,
162167 infile_handler : None ,
163168 reset_upon_returning_to_a_pool : false ,
169+ conn_metrics,
164170 }
165171 }
166172
@@ -172,6 +178,18 @@ impl ConnInner {
172178 . as_mut ( )
173179 . ok_or_else ( || DriverError :: ConnectionClosed . into ( ) )
174180 }
181+
182+ fn set_pool ( & mut self , pool : Option < Pool > ) {
183+ let conn_metrics = if let Some ( ref pool) = pool {
184+ Arc :: clone ( & pool. inner . metrics . conn )
185+ } else {
186+ Default :: default ( )
187+ } ;
188+ self . conn_metrics = Arc :: clone ( & conn_metrics) ;
189+ self . stmt_cache . conn_metrics = conn_metrics;
190+
191+ self . pool = pool;
192+ }
175193}
176194
177195/// MySql server connection.
@@ -907,6 +925,8 @@ impl Conn {
907925 conn. read_wait_timeout ( ) . await ?;
908926 conn. run_init_commands ( ) . await ?;
909927
928+ conn. metrics ( ) . connects . incr ( ) ;
929+
910930 Ok ( conn)
911931 }
912932 . boxed ( )
@@ -1045,6 +1065,10 @@ impl Conn {
10451065 self . routine ( routines:: ChangeUser ) . await ?;
10461066 self . inner . stmt_cache . clear ( ) ;
10471067 self . inner . infile_handler = None ;
1068+ // self.inner.set_pool(pool);
1069+
1070+ // TODO: clear some metrics?
1071+
10481072 Ok ( ( ) )
10491073 }
10501074
@@ -1159,6 +1183,10 @@ impl Conn {
11591183
11601184 Ok ( BinlogStream :: new ( self ) )
11611185 }
1186+
1187+ pub ( crate ) fn metrics ( & self ) -> & ConnMetrics {
1188+ & self . inner . conn_metrics
1189+ }
11621190}
11631191
11641192#[ cfg( test) ]
0 commit comments