@@ -39,6 +39,7 @@ use crate::{
39
39
consts:: { CapabilityFlags , Command , StatusFlags } ,
40
40
error:: * ,
41
41
io:: Stream ,
42
+ metrics:: ConnMetrics ,
42
43
opts:: Opts ,
43
44
queryable:: {
44
45
query_result:: { QueryResult , ResultSetMeta } ,
@@ -59,6 +60,8 @@ const DEFAULT_WAIT_TIMEOUT: usize = 28800;
59
60
60
61
/// Helper that asynchronously disconnects the givent connection on the default tokio executor.
61
62
fn disconnect ( mut conn : Conn ) {
63
+ conn. metrics ( ) . disconnects . incr ( ) ;
64
+
62
65
let disconnected = conn. inner . disconnected ;
63
66
64
67
// Mark conn as disconnected.
@@ -119,6 +122,7 @@ struct ConnInner {
119
122
/// One-time connection-level infile handler.
120
123
infile_handler :
121
124
Option < Pin < Box < dyn Future < Output = crate :: Result < InfileData > > + Send + Sync + ' static > > > ,
125
+ conn_metrics : Arc < ConnMetrics > ,
122
126
}
123
127
124
128
impl fmt:: Debug for ConnInner {
@@ -140,6 +144,7 @@ impl fmt::Debug for ConnInner {
140
144
impl ConnInner {
141
145
/// Constructs an empty connection.
142
146
fn empty ( opts : Opts ) -> ConnInner {
147
+ let conn_metrics: Arc < ConnMetrics > = Default :: default ( ) ;
143
148
ConnInner {
144
149
capabilities : opts. get_capabilities ( ) ,
145
150
status : StatusFlags :: empty ( ) ,
@@ -154,7 +159,7 @@ impl ConnInner {
154
159
tx_status : TxStatus :: None ,
155
160
last_io : Instant :: now ( ) ,
156
161
wait_timeout : Duration :: from_secs ( 0 ) ,
157
- stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) ) ,
162
+ stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) , conn_metrics . clone ( ) ) ,
158
163
socket : opts. socket ( ) . map ( Into :: into) ,
159
164
opts,
160
165
nonce : Vec :: default ( ) ,
@@ -164,6 +169,7 @@ impl ConnInner {
164
169
server_key : None ,
165
170
infile_handler : None ,
166
171
reset_upon_returning_to_a_pool : false ,
172
+ conn_metrics,
167
173
}
168
174
}
169
175
@@ -175,6 +181,18 @@ impl ConnInner {
175
181
. as_mut ( )
176
182
. ok_or_else ( || DriverError :: ConnectionClosed . into ( ) )
177
183
}
184
+
185
+ fn set_pool ( & mut self , pool : Option < Pool > ) {
186
+ let conn_metrics = if let Some ( ref pool) = pool {
187
+ Arc :: clone ( & pool. inner . metrics . conn )
188
+ } else {
189
+ Default :: default ( )
190
+ } ;
191
+ self . conn_metrics = Arc :: clone ( & conn_metrics) ;
192
+ self . stmt_cache . conn_metrics = conn_metrics;
193
+
194
+ self . pool = pool;
195
+ }
178
196
}
179
197
180
198
/// MySql server connection.
@@ -926,6 +944,8 @@ impl Conn {
926
944
conn. run_init_commands ( ) . await ?;
927
945
conn. run_setup_commands ( ) . await ?;
928
946
947
+ conn. metrics ( ) . connects . incr ( ) ;
948
+
929
949
Ok ( conn)
930
950
}
931
951
. boxed ( )
@@ -1162,6 +1182,10 @@ impl Conn {
1162
1182
self . inner . stmt_cache . clear ( ) ;
1163
1183
self . inner . infile_handler = None ;
1164
1184
self . run_setup_commands ( ) . await ?;
1185
+ // self.inner.set_pool(pool);
1186
+
1187
+ // TODO: clear some metrics?
1188
+
1165
1189
Ok ( ( ) )
1166
1190
}
1167
1191
@@ -1276,6 +1300,10 @@ impl Conn {
1276
1300
1277
1301
Ok ( BinlogStream :: new ( self ) )
1278
1302
}
1303
+
1304
+ pub ( crate ) fn metrics ( & self ) -> & ConnMetrics {
1305
+ & self . inner . conn_metrics
1306
+ }
1279
1307
}
1280
1308
1281
1309
#[ cfg( test) ]
0 commit comments