#[cfg(feature = "metrics")]
use metrics::counter;
pub const APP_RESPONSE_SENDS: &str = "wireframe_app_response_sends_total";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum AppResponseSendOutcome {
Success,
SerializationFailure,
CodecRejected,
TransportFailure,
}
impl AppResponseSendOutcome {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Success => "success",
Self::SerializationFailure => "serialization_failure",
Self::CodecRejected => "codec_rejected",
Self::TransportFailure => "transport_failure",
}
}
}
#[cfg(feature = "metrics")]
pub(crate) fn inc_app_response_send(outcome: AppResponseSendOutcome) {
counter!(APP_RESPONSE_SENDS, "outcome" => outcome.as_str()).increment(1);
}
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_app_response_send(_outcome: AppResponseSendOutcome) {}
#[cfg(all(test, feature = "metrics", not(loom)))]
mod tests {
use metrics::{SharedString, Unit};
use metrics_util::{
CompositeKey,
debugging::{DebugValue, DebuggingRecorder},
};
use super::{APP_RESPONSE_SENDS, AppResponseSendOutcome, inc_app_response_send};
type MetricSample = (CompositeKey, Option<Unit>, Option<SharedString>, DebugValue);
fn sample_matches_outcome(sample: &MetricSample, outcome: AppResponseSendOutcome) -> bool {
let (key, _, _, value) = sample;
if key.key().name() != APP_RESPONSE_SENDS || key.key().labels().count() != 1 {
return false;
}
let has_outcome = key
.key()
.labels()
.any(|label| label.key() == "outcome" && label.value() == outcome.as_str());
has_outcome && matches!(value, DebugValue::Counter(count) if *count == 1)
}
#[test]
fn app_response_sends_use_a_fixed_outcome_label() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
let outcomes = [
AppResponseSendOutcome::Success,
AppResponseSendOutcome::SerializationFailure,
AppResponseSendOutcome::CodecRejected,
AppResponseSendOutcome::TransportFailure,
];
::metrics::with_local_recorder(&recorder, || {
for outcome in outcomes {
inc_app_response_send(outcome);
}
});
let metric_samples = snapshotter.snapshot().into_vec();
assert_eq!(metric_samples.len(), outcomes.len());
for outcome in outcomes {
assert!(
metric_samples
.iter()
.any(|sample| sample_matches_outcome(sample, outcome)),
"missing sample for outcome {outcome:?}"
);
}
}
}