Skip to main content

web/
telemetry.rs

1//! Metric names and recorder setup.
2//!
3//! Names carry the `nomisync_` prefix because the series land in a
4//! VictoriaMetrics shared with every other app on the cluster: a bare
5//! `http_requests_total` would collide with anything else exporting the obvious
6//! name, and an ad-hoc query without a namespace filter would silently mix them.
7//! Keeping the names here as constants means the exporter and any test that
8//! asserts on them cannot drift apart. The recorder itself is
9//! `server::telemetry::recorder`.
10
11/// Requests served, labelled by method, matched path and status.
12pub const HTTP_REQUESTS: &str = "nomisync_http_requests_total";
13
14/// Request latency in seconds, same labels; buckets live in `server::telemetry::recorder`.
15pub use server::telemetry::recorder::HTTP_REQUEST_DURATION;
16
17/// Auth attempts refused by the per-address throttle.
18pub const AUTH_THROTTLED: &str = "nomisync_auth_throttled_total";
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn names_carry_the_app_prefix() {
26        for name in [HTTP_REQUESTS, HTTP_REQUEST_DURATION] {
27            assert!(
28                name.starts_with("nomisync_"),
29                "{name} would collide with other apps in the shared VictoriaMetrics"
30            );
31        }
32    }
33
34    #[test]
35    fn recorder_renders_the_registered_series() {
36        let Ok(handle) = server::telemetry::recorder::install_recorder() else {
37            // A recorder is global and install-once; another test in this binary
38            // may already hold it. Nothing to assert then.
39            return;
40        };
41        metrics::counter!(HTTP_REQUESTS, &[("method", "GET")]).increment(1);
42        let rendered = handle.render();
43        assert!(
44            rendered.contains(HTTP_REQUESTS),
45            "exporter did not render {HTTP_REQUESTS}"
46        );
47    }
48}