Files
crank/crates/crank-metrics/tests/exemplars.rs
T
Hermes DevOps 75fe5a123c [skip ci] wip: preserve Hermes crank development checkpoint
Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
2026-10-25 10:00:22 +00:00

71 lines
2.5 KiB
Rust

use std::time::Duration;
use crank_metrics::{
ExemplarTraceId, HttpMethod, HttpRoute, HttpStatusClass, exemplar_snapshot,
record_http_request_with_exemplar,
};
use metrics_util::debugging::DebuggingRecorder;
#[test]
fn trace_id_is_validated_and_never_becomes_an_ordinary_label() {
assert!(ExemplarTraceId::parse("0123456789abcdef0123456789abcdef").is_some());
assert!(ExemplarTraceId::parse("0123456789ABCDEF0123456789ABCDEF").is_none());
assert!(ExemplarTraceId::parse("short").is_none());
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
crank_metrics::bind_metric_service(crank_metrics::MetricService::AdminApi)
.expect("isolated metrics binding for the exemplar fixture");
metrics::with_local_recorder(&recorder, || {
record_http_request_with_exemplar(
HttpRoute::from_matched_path("/health"),
HttpMethod::Get,
HttpStatusClass::Success,
Duration::ZERO,
ExemplarTraceId::parse("0123456789abcdef0123456789abcdef"),
);
record_http_request_with_exemplar(
HttpRoute::from_matched_path("/health"),
HttpMethod::Get,
HttpStatusClass::Success,
Duration::from_millis(10),
ExemplarTraceId::parse("0123456789abcdef0123456789abcdef"),
);
record_http_request_with_exemplar(
HttpRoute::from_matched_path("/health"),
HttpMethod::Get,
HttpStatusClass::Success,
Duration::from_secs(61),
ExemplarTraceId::parse("0123456789abcdef0123456789abcdef"),
);
record_http_request_with_exemplar(
HttpRoute::from_matched_path("/health"),
HttpMethod::Get,
HttpStatusClass::Success,
Duration::from_millis(10),
None,
);
});
let aggregate = snapshotter.snapshot().into_vec();
assert!(
aggregate
.iter()
.all(|(key, _, _, _)| { key.key().labels().all(|label| label.key() != "trace_id") })
);
let exemplars = exemplar_snapshot();
assert_eq!(exemplars.len(), 3);
assert!(
exemplars
.iter()
.all(|exemplar| exemplar.trace_id.as_str() == "0123456789abcdef0123456789abcdef")
);
let bounds = exemplars
.iter()
.map(|exemplar| exemplar.bucket_upper_bound)
.collect::<Vec<_>>();
assert!(bounds.contains(&Some(0.005)));
assert!(bounds.contains(&Some(0.01)));
assert!(bounds.contains(&None));
}