1
1
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
2
3
+ #[ cfg( feature = "prometheus" ) ]
4
+ use prometheus:: { local:: LocalHistogram , IntCounter } ;
3
5
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
4
6
use std:: sync:: mpsc;
5
7
use std:: sync:: Arc ;
@@ -16,39 +18,44 @@ use {
16
18
GRPC_TASK_WAIT_DURATION ,
17
19
} ,
18
20
crate :: task:: resolve,
19
- prometheus:: {
20
- core:: { AtomicU64 , Counter } ,
21
- Histogram ,
22
- } ,
23
21
std:: time:: Instant ,
24
22
} ;
25
23
24
+ #[ allow( dead_code) ]
25
+ const METRICS_FLUSH_INTERVAL : u64 = 10_000 ; // 10s
26
+
26
27
#[ cfg( feature = "prometheus" ) ]
27
28
pub struct GRPCRunner {
28
- cq_next_duration_his : Histogram ,
29
- execute_duration_his : Histogram ,
30
- wait_duration_his : Histogram ,
31
- event_counter : [ Counter < AtomicU64 > ; 6 ] ,
29
+ cq_next_duration_his : LocalHistogram ,
30
+ execute_duration_his : LocalHistogram ,
31
+ wait_duration_his : LocalHistogram ,
32
+ event_counter : [ IntCounter ; 6 ] ,
33
+ last_flush_time : Instant ,
32
34
}
33
35
34
36
#[ cfg( feature = "prometheus" ) ]
35
37
impl GRPCRunner {
36
38
pub fn new ( name : & String ) -> GRPCRunner {
37
- let cq_next_duration_his = GRPC_POOL_CQ_NEXT_DURATION . with_label_values ( & [ name] ) ;
38
- let execute_duration_his = GRPC_POOL_EXECUTE_DURATION . with_label_values ( & [ name] ) ;
39
- let wait_duration_his = GRPC_TASK_WAIT_DURATION . with_label_values ( & [ name] ) ;
39
+ let cq_next_duration_his = GRPC_POOL_CQ_NEXT_DURATION
40
+ . with_label_values ( & [ name] )
41
+ . local ( ) ;
42
+ let execute_duration_his = GRPC_POOL_EXECUTE_DURATION
43
+ . with_label_values ( & [ name] )
44
+ . local ( ) ;
45
+ let wait_duration_his = GRPC_TASK_WAIT_DURATION . with_label_values ( & [ name] ) . local ( ) ;
40
46
let event_counter = [ "batch" , "request" , "unary" , "abort" , "action" , "spawn" ]
41
47
. map ( |event| GRPC_POOL_EVENT_COUNT_VEC . with_label_values ( & [ name, event] ) ) ;
42
48
GRPCRunner {
43
49
cq_next_duration_his,
44
50
execute_duration_his,
45
51
wait_duration_his,
46
52
event_counter,
53
+ last_flush_time : Instant :: now ( ) ,
47
54
}
48
55
}
49
56
50
57
// event loop
51
- pub fn run ( & self , tx : mpsc:: Sender < CompletionQueue > ) {
58
+ pub fn run ( & mut self , tx : mpsc:: Sender < CompletionQueue > ) {
52
59
let cq = Arc :: new ( CompletionQueueHandle :: new ( ) ) ;
53
60
let worker_info = Arc :: new ( WorkQueue :: new ( ) ) ;
54
61
let cq = CompletionQueue :: new ( cq, worker_info) ;
@@ -73,7 +80,21 @@ impl GRPCRunner {
73
80
}
74
81
self . execute_duration_his
75
82
. observe ( now. elapsed ( ) . as_secs_f64 ( ) ) ;
83
+ self . maybe_flush ( ) ;
84
+ }
85
+ }
86
+
87
+ fn maybe_flush ( & mut self ) {
88
+ let now = Instant :: now ( ) ;
89
+ if now. saturating_duration_since ( self . last_flush_time )
90
+ < std:: time:: Duration :: from_millis ( METRICS_FLUSH_INTERVAL )
91
+ {
92
+ return ;
76
93
}
94
+ self . last_flush_time = now;
95
+ self . cq_next_duration_his . flush ( ) ;
96
+ self . execute_duration_his . flush ( ) ;
97
+ self . wait_duration_his . flush ( ) ;
77
98
}
78
99
79
100
fn resolve ( & self , tag : Box < CallTag > , cq : & CompletionQueue , success : bool ) {
@@ -193,7 +214,7 @@ impl EnvBuilder {
193
214
. as_ref ( )
194
215
. map_or ( format ! ( "grpc-pool-{i}" ) , |prefix| format ! ( "{prefix}-{i}" ) ) ;
195
216
#[ cfg( feature = "prometheus" ) ]
196
- let runner = GRPCRunner :: new ( & name) ;
217
+ let mut runner = GRPCRunner :: new ( & name) ;
197
218
builder = builder. name ( name) ;
198
219
let after_start = self . after_start . clone ( ) ;
199
220
let before_stop = self . before_stop . clone ( ) ;
0 commit comments