11package  tech .ydb .apps ;
22
3+ import  java .util .concurrent .TimeUnit ;
34import  java .util .function .Consumer ;
45
56import  javax .annotation .Nullable ;
1516import  io .grpc .Status ;
1617import  io .micrometer .core .instrument .Counter ;
1718import  io .micrometer .core .instrument .MeterRegistry ;
19+ import  io .micrometer .core .instrument .Timer ;
1820
1921/** 
2022 * 
2325public  class  GrpcMetrics  implements  Consumer <ManagedChannelBuilder <?>>, ClientInterceptor  {
2426    private  static  final  Counter .Builder  REQUEST  = Counter .builder ("grpc.request" );
2527    private  static  final  Counter .Builder  RESPONSE  = Counter .builder ("grpc.response" );
28+     private  static  final  Timer .Builder  LATENCY  = Timer .builder ("grpc.latency" )
29+             .publishPercentiles (0.5 , 0.9 , 0.95 , 0.99 );
2630
2731    private  static  MeterRegistry  REGISTRY  = null ;
2832
@@ -101,9 +105,11 @@ public void sendMessage(ReqT message) {
101105
102106        private  class  ProxyListener  extends  Listener <RespT > {
103107            private  final  Listener <RespT > delegate ;
108+             private  final  long  startedAt ;
104109
105110            public  ProxyListener (Listener <RespT > delegate ) {
106111                this .delegate  = delegate ;
112+                 this .startedAt  = System .currentTimeMillis ();
107113            }
108114
109115
@@ -119,8 +125,11 @@ public void onMessage(RespT message) {
119125
120126            @ Override 
121127            public  void  onClose (Status  status , Metadata  trailers ) {
128+                 long  ms  = System .currentTimeMillis () - startedAt ;
122129                RESPONSE .tag ("method" , method ).tag ("authority" , authority ).tag ("status" , status .getCode ().toString ())
123130                        .register (registry ).increment ();
131+                 LATENCY .tag ("method" , method ).tag ("authority" , authority )
132+                         .register (registry ).record (ms , TimeUnit .MILLISECONDS );
124133                delegate .onClose (status , trailers );
125134            }
126135
0 commit comments