@@ -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 . inc ( ) ;
61
+
59
62
let disconnected = conn. inner . disconnected ;
60
63
61
64
// Mark conn as disconnected.
@@ -114,6 +117,7 @@ struct ConnInner {
114
117
/// One-time connection-level infile handler.
115
118
infile_handler :
116
119
Option < Pin < Box < dyn Future < Output = crate :: Result < InfileData > > + Send + Sync + ' static > > > ,
120
+ conn_metrics : Arc < ConnMetrics > ,
117
121
}
118
122
119
123
impl fmt:: Debug for ConnInner {
@@ -133,6 +137,7 @@ impl fmt::Debug for ConnInner {
133
137
impl ConnInner {
134
138
/// Constructs an empty connection.
135
139
fn empty ( opts : Opts ) -> ConnInner {
140
+ let conn_metrics: Arc < ConnMetrics > = Default :: default ( ) ;
136
141
ConnInner {
137
142
capabilities : opts. get_capabilities ( ) ,
138
143
status : StatusFlags :: empty ( ) ,
@@ -147,14 +152,15 @@ impl ConnInner {
147
152
tx_status : TxStatus :: None ,
148
153
last_io : Instant :: now ( ) ,
149
154
wait_timeout : Duration :: from_secs ( 0 ) ,
150
- stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) ) ,
155
+ stmt_cache : StmtCache :: new ( opts. stmt_cache_size ( ) , conn_metrics . clone ( ) ) ,
151
156
socket : opts. socket ( ) . map ( Into :: into) ,
152
157
opts,
153
158
nonce : Vec :: default ( ) ,
154
159
auth_plugin : AuthPlugin :: MysqlNativePassword ,
155
160
auth_switched : false ,
156
161
disconnected : false ,
157
162
infile_handler : None ,
163
+ conn_metrics,
158
164
}
159
165
}
160
166
@@ -166,6 +172,18 @@ impl ConnInner {
166
172
. as_mut ( )
167
173
. ok_or_else ( || DriverError :: ConnectionClosed . into ( ) )
168
174
}
175
+
176
+ fn set_pool ( & mut self , pool : Option < Pool > ) {
177
+ let conn_metrics = if let Some ( ref pool) = pool {
178
+ Arc :: clone ( & pool. inner . metrics . conn )
179
+ } else {
180
+ Default :: default ( )
181
+ } ;
182
+ self . conn_metrics = Arc :: clone ( & conn_metrics) ;
183
+ self . stmt_cache . conn_metrics = conn_metrics;
184
+
185
+ self . pool = pool;
186
+ }
169
187
}
170
188
171
189
/// MySql server connection.
@@ -854,6 +872,8 @@ impl Conn {
854
872
conn. read_wait_timeout ( ) . await ?;
855
873
conn. run_init_commands ( ) . await ?;
856
874
875
+ conn. metrics ( ) . connects . inc ( ) ;
876
+
857
877
Ok ( conn)
858
878
}
859
879
. boxed ( )
@@ -957,7 +977,10 @@ impl Conn {
957
977
958
978
self . inner . stmt_cache . clear ( ) ;
959
979
self . inner . infile_handler = None ;
960
- self . inner . pool = pool;
980
+ self . inner . set_pool ( pool) ;
981
+
982
+ // TODO: clear some metrics?
983
+
961
984
Ok ( ( ) )
962
985
}
963
986
@@ -1062,6 +1085,10 @@ impl Conn {
1062
1085
1063
1086
Ok ( BinlogStream :: new ( self ) )
1064
1087
}
1088
+
1089
+ pub ( crate ) fn metrics ( & self ) -> & ConnMetrics {
1090
+ & self . inner . conn_metrics
1091
+ }
1065
1092
}
1066
1093
1067
1094
#[ cfg( test) ]
0 commit comments