@@ -7,7 +7,7 @@ use std::time::Instant;
7
7
8
8
use crate :: config:: get_config;
9
9
10
- #[ derive( Debug ) ]
10
+ #[ derive( Debug , Clone , Copy ) ]
11
11
pub enum StatisticName {
12
12
CheckoutTime ,
13
13
//QueryRuntime,
@@ -16,15 +16,17 @@ pub enum StatisticName {
16
16
Transactions ,
17
17
DataSent ,
18
18
DataReceived ,
19
- ClientsWaiting ,
20
- ClientsActive ,
21
- ClientsIdle ,
19
+ ClientWaiting ,
20
+ ClientActive ,
21
+ ClientIdle ,
22
+ ClientDisconnecting ,
22
23
}
23
24
24
25
#[ derive( Debug ) ]
25
26
pub struct Statistic {
26
27
pub name : StatisticName ,
27
28
pub value : i64 ,
29
+ pub process_id : Option < i32 > ,
28
30
}
29
31
30
32
#[ derive( Clone , Debug ) ]
@@ -41,6 +43,7 @@ impl Reporter {
41
43
let statistic = Statistic {
42
44
name : StatisticName :: Queries ,
43
45
value : 1 ,
46
+ process_id : None ,
44
47
} ;
45
48
46
49
let _ = self . tx . try_send ( statistic) ;
@@ -50,6 +53,7 @@ impl Reporter {
50
53
let statistic = Statistic {
51
54
name : StatisticName :: Transactions ,
52
55
value : 1 ,
56
+ process_id : None ,
53
57
} ;
54
58
55
59
let _ = self . tx . try_send ( statistic) ;
@@ -59,6 +63,7 @@ impl Reporter {
59
63
let statistic = Statistic {
60
64
name : StatisticName :: DataSent ,
61
65
value : amount as i64 ,
66
+ process_id : None ,
62
67
} ;
63
68
64
69
let _ = self . tx . try_send ( statistic) ;
@@ -68,6 +73,7 @@ impl Reporter {
68
73
let statistic = Statistic {
69
74
name : StatisticName :: DataReceived ,
70
75
value : amount as i64 ,
76
+ process_id : None ,
71
77
} ;
72
78
73
79
let _ = self . tx . try_send ( statistic) ;
@@ -77,54 +83,48 @@ impl Reporter {
77
83
let statistic = Statistic {
78
84
name : StatisticName :: CheckoutTime ,
79
85
value : ms as i64 ,
86
+ process_id : None ,
80
87
} ;
81
88
82
89
let _ = self . tx . try_send ( statistic) ;
83
90
}
84
91
85
- pub fn client_waiting ( & mut self ) {
92
+ pub fn client_waiting ( & mut self , process_id : i32 ) {
86
93
let statistic = Statistic {
87
- name : StatisticName :: ClientsWaiting ,
94
+ name : StatisticName :: ClientWaiting ,
88
95
value : 1 ,
89
- } ;
90
-
91
- let _ = self . tx . try_send ( statistic) ;
92
-
93
- let statistic = Statistic {
94
- name : StatisticName :: ClientsIdle ,
95
- value : -1 ,
96
+ process_id : Some ( process_id) ,
96
97
} ;
97
98
98
99
let _ = self . tx . try_send ( statistic) ;
99
100
}
100
101
101
- pub fn client_active ( & mut self ) {
102
- let statistic = Statistic {
103
- name : StatisticName :: ClientsWaiting ,
104
- value : -1 ,
105
- } ;
106
-
107
- let _ = self . tx . try_send ( statistic) ;
102
+ pub fn client_active ( & mut self , process_id : i32 ) {
108
103
109
104
let statistic = Statistic {
110
- name : StatisticName :: ClientsActive ,
105
+ name : StatisticName :: ClientActive ,
111
106
value : 1 ,
107
+ process_id : Some ( process_id) ,
112
108
} ;
113
109
114
110
let _ = self . tx . try_send ( statistic) ;
115
111
}
116
112
117
- pub fn client_idle ( & mut self ) {
113
+ pub fn client_idle ( & mut self , process_id : i32 ) {
118
114
let statistic = Statistic {
119
- name : StatisticName :: ClientsActive ,
120
- value : -1 ,
115
+ name : StatisticName :: ClientIdle ,
116
+ value : 1 ,
117
+ process_id : Some ( process_id) ,
121
118
} ;
122
119
123
120
let _ = self . tx . try_send ( statistic) ;
121
+ }
124
122
123
+ pub fn client_disconnecting ( & mut self , process_id : i32 ) {
125
124
let statistic = Statistic {
126
- name : StatisticName :: ClientsIdle ,
125
+ name : StatisticName :: ClientDisconnecting ,
127
126
value : 1 ,
127
+ process_id : Some ( process_id) ,
128
128
} ;
129
129
130
130
let _ = self . tx . try_send ( statistic) ;
@@ -158,6 +158,8 @@ impl Collector {
158
158
( "cl_idle" , 0 ) ,
159
159
] ) ;
160
160
161
+ let mut client_states: HashMap < i32 , StatisticName > = HashMap :: new ( ) ;
162
+
161
163
let mut now = Instant :: now ( ) ;
162
164
163
165
loop {
@@ -210,32 +212,43 @@ impl Collector {
210
212
}
211
213
}
212
214
213
- StatisticName :: ClientsActive => {
214
- let counter = stats. entry ( "cl_active" ) . or_insert ( 0 ) ;
215
-
216
- * counter += stat. value ;
217
- * counter = std:: cmp:: max ( * counter, 0 ) ;
218
- }
219
-
220
- StatisticName :: ClientsWaiting => {
221
- let counter = stats. entry ( "cl_waiting" ) . or_insert ( 0 ) ;
222
- * counter += stat. value ;
223
- * counter = std:: cmp:: max ( * counter, 0 ) ;
215
+ StatisticName :: ClientActive | StatisticName :: ClientWaiting | StatisticName :: ClientIdle => {
216
+ client_states. insert ( stat. process_id . unwrap ( ) , stat. name ) ;
224
217
}
225
218
226
- StatisticName :: ClientsIdle => {
227
- let counter = stats. entry ( "cl_idle" ) . or_insert ( 0 ) ;
228
- * counter += stat. value ;
229
- * counter = std:: cmp:: max ( * counter, 0 ) ;
219
+ StatisticName :: ClientDisconnecting => {
220
+ client_states. remove ( & stat. process_id . unwrap ( ) ) ;
230
221
}
231
222
} ;
232
223
224
+
233
225
// It's been 15 seconds. If there is no traffic, it won't publish anything,
234
226
// but it also doesn't matter then.
235
227
if now. elapsed ( ) . as_secs ( ) > 15 {
236
- let mut pipeline = self . client . pipeline ( ) ;
228
+ for ( _, state) in & client_states {
229
+ match state {
230
+ StatisticName :: ClientActive => {
231
+ let counter = stats. entry ( "cl_active" ) . or_insert ( 0 ) ;
232
+ * counter += 1 ;
233
+ }
234
+
235
+ StatisticName :: ClientWaiting => {
236
+ let counter = stats. entry ( "cl_waiting" ) . or_insert ( 0 ) ;
237
+ * counter += 1 ;
238
+ }
239
+
240
+ StatisticName :: ClientIdle => {
241
+ let counter = stats. entry ( "cl_idle" ) . or_insert ( 0 ) ;
242
+ * counter += 1 ;
243
+ }
244
+
245
+ _ => unreachable ! ( ) ,
246
+ } ;
247
+ }
248
+
249
+ println ! ( ">> Reporting to StatsD: {:?}" , stats) ;
237
250
238
- println ! ( ">> Publishing statistics to StatsD: {:?}" , stats ) ;
251
+ let mut pipeline = self . client . pipeline ( ) ;
239
252
240
253
for ( key, value) in stats. iter_mut ( ) {
241
254
pipeline. gauge ( key, * value as f64 ) ;
0 commit comments