11// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22
3+ #[ cfg( feature = "prometheus" ) ]
4+ use prometheus:: { local:: LocalHistogram , IntCounter } ;
35use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
46use std:: sync:: mpsc;
57use std:: sync:: Arc ;
@@ -16,39 +18,44 @@ use {
1618 GRPC_TASK_WAIT_DURATION ,
1719 } ,
1820 crate :: task:: resolve,
19- prometheus:: {
20- core:: { AtomicU64 , Counter } ,
21- Histogram ,
22- } ,
2321 std:: time:: Instant ,
2422} ;
2523
24+ #[ allow( dead_code) ]
25+ const METRICS_FLUSH_INTERVAL : u64 = 10_000 ; // 10s
26+
2627#[ cfg( feature = "prometheus" ) ]
2728pub 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 ,
3234}
3335
3436#[ cfg( feature = "prometheus" ) ]
3537impl GRPCRunner {
3638 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 ( ) ;
4046 let event_counter = [ "batch" , "request" , "unary" , "abort" , "action" , "spawn" ]
4147 . map ( |event| GRPC_POOL_EVENT_COUNT_VEC . with_label_values ( & [ name, event] ) ) ;
4248 GRPCRunner {
4349 cq_next_duration_his,
4450 execute_duration_his,
4551 wait_duration_his,
4652 event_counter,
53+ last_flush_time : Instant :: now ( ) ,
4754 }
4855 }
4956
5057 // event loop
51- pub fn run ( & self , tx : mpsc:: Sender < CompletionQueue > ) {
58+ pub fn run ( & mut self , tx : mpsc:: Sender < CompletionQueue > ) {
5259 let cq = Arc :: new ( CompletionQueueHandle :: new ( ) ) ;
5360 let worker_info = Arc :: new ( WorkQueue :: new ( ) ) ;
5461 let cq = CompletionQueue :: new ( cq, worker_info) ;
@@ -73,7 +80,21 @@ impl GRPCRunner {
7380 }
7481 self . execute_duration_his
7582 . 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 ;
7693 }
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 ( ) ;
7798 }
7899
79100 fn resolve ( & self , tag : Box < CallTag > , cq : & CompletionQueue , success : bool ) {
@@ -193,7 +214,7 @@ impl EnvBuilder {
193214 . as_ref ( )
194215 . map_or ( format ! ( "grpc-pool-{i}" ) , |prefix| format ! ( "{prefix}-{i}" ) ) ;
195216 #[ cfg( feature = "prometheus" ) ]
196- let runner = GRPCRunner :: new ( & name) ;
217+ let mut runner = GRPCRunner :: new ( & name) ;
197218 builder = builder. name ( name) ;
198219 let after_start = self . after_start . clone ( ) ;
199220 let before_stop = self . before_stop . clone ( ) ;
0 commit comments