@@ -38,6 +38,7 @@ use crate::{
38
38
consts:: { CapabilityFlags , Command , StatusFlags } ,
39
39
error:: * ,
40
40
io:: Stream ,
41
+ metrics:: ConnMetrics ,
41
42
opts:: Opts ,
42
43
queryable:: {
43
44
query_result:: { QueryResult , ResultSetMeta } ,
@@ -56,6 +57,8 @@ pub mod stmt_cache;
56
57
57
58
/// Helper that asynchronously disconnects the givent connection on the default tokio executor.
58
59
fn disconnect ( mut conn : Conn ) {
60
+ conn. metrics ( ) . disconnects . incr ( ) ;
61
+
59
62
let disconnected = conn. inner . disconnected ;
60
63
61
64
// Mark conn as disconnected.
@@ -116,6 +119,7 @@ struct ConnInner {
116
119
/// One-time connection-level infile handler.
117
120
infile_handler :
118
121
Option < Pin < Box < dyn Future < Output = crate :: Result < InfileData > > + Send + Sync + ' static > > > ,
122
+ conn_metrics : Arc < ConnMetrics > ,
119
123
}
120
124
121
125
impl fmt:: Debug for ConnInner {
@@ -137,6 +141,7 @@ impl fmt::Debug for ConnInner {
137
141
impl ConnInner {
138
142
/// Constructs an empty connection.
139
143
fn empty ( opts : Opts ) -> ConnInner {
144
+ let conn_metrics: Arc < ConnMetrics > = Default :: default ( ) ;
140
145
ConnInner {
141
146
capabilities : opts. get_capabilities ( ) ,
142
147
status : StatusFlags :: empty ( ) ,
@@ -151,7 +156,7 @@ impl ConnInner {
151
156
tx_status : TxStatus :: None ,
152
157
last_io : Instant :: now ( ) ,
153
158
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 ( ) ) ,
155
160
socket : opts. socket ( ) . map ( Into :: into) ,
156
161
opts,
157
162
nonce : Vec :: default ( ) ,
@@ -161,6 +166,7 @@ impl ConnInner {
161
166
server_key : None ,
162
167
infile_handler : None ,
163
168
reset_upon_returning_to_a_pool : false ,
169
+ conn_metrics,
164
170
}
165
171
}
166
172
@@ -172,6 +178,18 @@ impl ConnInner {
172
178
. as_mut ( )
173
179
. ok_or_else ( || DriverError :: ConnectionClosed . into ( ) )
174
180
}
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
+ }
175
193
}
176
194
177
195
/// MySql server connection.
@@ -907,6 +925,8 @@ impl Conn {
907
925
conn. read_wait_timeout ( ) . await ?;
908
926
conn. run_init_commands ( ) . await ?;
909
927
928
+ conn. metrics ( ) . connects . incr ( ) ;
929
+
910
930
Ok ( conn)
911
931
}
912
932
. boxed ( )
@@ -1045,6 +1065,10 @@ impl Conn {
1045
1065
self . routine ( routines:: ChangeUser ) . await ?;
1046
1066
self . inner . stmt_cache . clear ( ) ;
1047
1067
self . inner . infile_handler = None ;
1068
+ // self.inner.set_pool(pool);
1069
+
1070
+ // TODO: clear some metrics?
1071
+
1048
1072
Ok ( ( ) )
1049
1073
}
1050
1074
@@ -1159,6 +1183,10 @@ impl Conn {
1159
1183
1160
1184
Ok ( BinlogStream :: new ( self ) )
1161
1185
}
1186
+
1187
+ pub ( crate ) fn metrics ( & self ) -> & ConnMetrics {
1188
+ & self . inner . conn_metrics
1189
+ }
1162
1190
}
1163
1191
1164
1192
#[ cfg( test) ]
0 commit comments