@@ -92,13 +92,22 @@ pub fn update_signer_nonce(nonce: u64) {
92
92
prometheus:: SIGNER_NONCE . set ( nonce as i64 ) ;
93
93
}
94
94
95
+ // Allow dead code because this is only used in the `monitoring_prom` feature
96
+ // but we want to run it in a test
97
+ #[ allow( dead_code) ]
98
+ /// Remove the origin from the full path to avoid duplicate metrics for different origins
99
+ fn remove_origin_from_path ( full_path : & str , origin : & str ) -> String {
100
+ let path = full_path. replace ( origin, "" ) ;
101
+ path
102
+ }
103
+
95
104
/// Start a new RPC call timer.
96
105
/// The `origin` parameter is the base path of the RPC call, e.g. `http://node.com`.
97
106
/// The `origin` parameter is removed from `full_path` when storing in prometheus.
98
107
#[ cfg( feature = "monitoring_prom" ) ]
99
108
pub fn new_rpc_call_timer ( full_path : & str , origin : & str ) -> HistogramTimer {
100
- let path = & full_path[ origin. len ( ) .. ] ;
101
- let histogram = prometheus:: SIGNER_RPC_CALL_LATENCIES_HISTOGRAM . with_label_values ( & [ path] ) ;
109
+ let path = remove_origin_from_path ( full_path, origin) ;
110
+ let histogram = prometheus:: SIGNER_RPC_CALL_LATENCIES_HISTOGRAM . with_label_values ( & [ & path] ) ;
102
111
histogram. start_timer ( )
103
112
}
104
113
@@ -140,3 +149,16 @@ pub fn start_serving_monitoring_metrics(config: GlobalConfig) -> Result<(), Stri
140
149
}
141
150
Ok ( ( ) )
142
151
}
152
+
153
+ #[ test]
154
+ fn test_remove_origin_from_path ( ) {
155
+ let full_path = "http://localhost:20443/v2/info" ;
156
+ let origin = "http://localhost:20443" ;
157
+ let path = remove_origin_from_path ( full_path, origin) ;
158
+ assert_eq ! ( path, "/v2/info" ) ;
159
+
160
+ let full_path = "/v2/info" ;
161
+ let origin = "http://localhost:20443" ;
162
+ let path = remove_origin_from_path ( full_path, origin) ;
163
+ assert_eq ! ( path, "/v2/info" ) ;
164
+ }
0 commit comments