Skip to content

Commit 381a8c7

Browse files
committed
fix: better implementation of path label for prom metrics
1 parent 5b036df commit 381a8c7

File tree

1 file changed

+24
-2
lines changed
  • stacks-signer/src/monitoring

1 file changed

+24
-2
lines changed

stacks-signer/src/monitoring/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,22 @@ pub fn update_signer_nonce(nonce: u64) {
9292
prometheus::SIGNER_NONCE.set(nonce as i64);
9393
}
9494

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+
95104
/// Start a new RPC call timer.
96105
/// The `origin` parameter is the base path of the RPC call, e.g. `http://node.com`.
97106
/// The `origin` parameter is removed from `full_path` when storing in prometheus.
98107
#[cfg(feature = "monitoring_prom")]
99108
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]);
102111
histogram.start_timer()
103112
}
104113

@@ -140,3 +149,16 @@ pub fn start_serving_monitoring_metrics(config: GlobalConfig) -> Result<(), Stri
140149
}
141150
Ok(())
142151
}
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

Comments
 (0)