#[cfg(feature = "metrics")]
use metrics::counter;
pub const PREAMBLE_REJECTIONS: &str = "wireframe_preamble_rejections_total";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PreambleRejectionReason {
SuccessHandlerError,
}
impl PreambleRejectionReason {
#[cfg(feature = "metrics")]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::SuccessHandlerError => "success_handler_error",
}
}
}
#[cfg(feature = "metrics")]
pub(crate) fn inc_preamble_rejection(reason: PreambleRejectionReason) {
counter!(PREAMBLE_REJECTIONS, "reason" => reason.as_str()).increment(1);
}
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_preamble_rejection(_reason: PreambleRejectionReason) {}
#[cfg(all(test, feature = "metrics", not(loom)))]
mod tests {
use metrics_util::{
CompositeKey,
debugging::{DebugValue, DebuggingRecorder},
};
use super::{PREAMBLE_REJECTIONS, PreambleRejectionReason, inc_preamble_rejection};
fn has_label(key: &CompositeKey, expected_key: &str, expected_value: &str) -> bool {
key.key()
.labels()
.any(|label| label.key() == expected_key && label.value() == expected_value)
}
#[test]
fn preamble_rejections_use_fixed_reason_label() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
::metrics::with_local_recorder(&recorder, || {
inc_preamble_rejection(PreambleRejectionReason::SuccessHandlerError);
});
let samples = snapshotter.snapshot().into_vec();
assert_eq!(samples.len(), 1, "samples.len() should equal 1");
assert!(
samples.iter().any(|(key, _, _, value)| {
key.key().name() == PREAMBLE_REJECTIONS
&& key.key().labels().count() == 1
&& has_label(key, "reason", "success_handler_error")
&& matches!(value, DebugValue::Counter(count) if *count == 1)
}),
"expected one success-handler rejection sample, got {samples:?}",
);
}
}