@@ -68,6 +68,22 @@ impl<C: AleoClient> MetricHttpClient<C> {
6868 }
6969}
7070
71+ impl < C : AleoClient > MetricHttpClient < C > {
72+ /// Helper function to track metrics for RPC calls
73+ async fn track_request < T , F , Fut > ( & self , path : & str , operation : F ) -> ChainResult < T >
74+ where
75+ F : FnOnce ( ) -> Fut ,
76+ Fut : std:: future:: Future < Output = ChainResult < T > > ,
77+ {
78+ let start = Instant :: now ( ) ;
79+ let res = operation ( ) . await ;
80+ let method = path. split ( '/' ) . next ( ) . unwrap_or_default ( ) ;
81+ self . metrics
82+ . increment_metrics ( & self . metrics_config , method, start, res. is_ok ( ) ) ;
83+ res
84+ }
85+ }
86+
7187#[ async_trait]
7288impl < C : AleoClient > HttpClient for MetricHttpClient < C > {
7389 /// Makes a GET request to the API
@@ -76,11 +92,8 @@ impl<C: AleoClient> HttpClient for MetricHttpClient<C> {
7692 path : & str ,
7793 query : impl Into < Option < serde_json:: Value > > + Send ,
7894 ) -> ChainResult < T > {
79- let start = Instant :: now ( ) ;
80- let res = self . inner . request ( path, query) . await ;
81- self . metrics
82- . increment_metrics ( & self . metrics_config , path, start, res. is_ok ( ) ) ;
83- res
95+ self . track_request ( path, || self . inner . request ( path, query) )
96+ . await
8497 }
8598
8699 /// Makes a POST request to the API
@@ -89,10 +102,7 @@ impl<C: AleoClient> HttpClient for MetricHttpClient<C> {
89102 path : & str ,
90103 body : & serde_json:: Value ,
91104 ) -> ChainResult < T > {
92- let start = Instant :: now ( ) ;
93- let res = self . inner . request_post ( path, body) . await ;
94- self . metrics
95- . increment_metrics ( & self . metrics_config , path, start, res. is_ok ( ) ) ;
96- res
105+ self . track_request ( path, || self . inner . request_post ( path, body) )
106+ . await
97107 }
98108}
0 commit comments