#[cfg(feature = "metrics")]
use metrics::counter;
use crate::push::PushPolicy;
pub const PUSH_WRITES_ABANDONED: &str = "wireframe_push_writes_abandoned_total";
pub const PUSH_RATE_LIMITED: &str = "wireframe_push_rate_limited_total";
pub const PUSH_DROPS: &str = "wireframe_push_drops_total";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PushDropOutcome {
NoDeadLetterQueue,
DeadLetterFull,
DeadLetterClosed,
}
#[cfg(feature = "metrics")]
impl PushDropOutcome {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::NoDeadLetterQueue => "no_dlq",
Self::DeadLetterFull => "dlq_full",
Self::DeadLetterClosed => "dlq_closed",
}
}
}
impl PushDropOutcome {
pub(crate) fn record(self, policy: PushPolicy) { inc_push_drop(self, policy); }
}
#[cfg(feature = "metrics")]
const fn policy_label(policy: PushPolicy) -> &'static str {
match policy {
PushPolicy::ReturnErrorIfFull => "return_error_if_full",
PushPolicy::DropIfFull => "drop_if_full",
PushPolicy::WarnAndDropIfFull => "warn_and_drop_if_full",
}
}
#[cfg(feature = "metrics")]
pub(crate) fn inc_push_drop(outcome: PushDropOutcome, policy: PushPolicy) {
counter!(
PUSH_DROPS,
"policy" => policy_label(policy),
"outcome" => outcome.as_str()
)
.increment(1);
}
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_push_drop(_outcome: PushDropOutcome, _policy: PushPolicy) {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum PushWriteAbandonReason {
IdleGraceExpired,
Shutdown,
}
impl PushWriteAbandonReason {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::IdleGraceExpired => "idle_grace_expired",
Self::Shutdown => "shutdown",
}
}
pub(crate) fn record(self) { inc_push_write_abandoned(self); }
}
#[cfg(feature = "metrics")]
pub(crate) fn inc_push_write_abandoned(reason: PushWriteAbandonReason) {
counter!(PUSH_WRITES_ABANDONED, "reason" => reason.as_str()).increment(1);
}
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_push_write_abandoned(_reason: PushWriteAbandonReason) {}
#[cfg(feature = "metrics")]
pub(crate) fn inc_push_rate_limited() { counter!(PUSH_RATE_LIMITED).increment(1); }
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_push_rate_limited() {}
#[cfg(all(test, feature = "metrics", not(loom)))]
mod tests {
use metrics::{SharedString, Unit};
use metrics_util::{
CompositeKey,
debugging::{DebugValue, DebuggingRecorder},
};
use super::{
PUSH_DROPS,
PUSH_RATE_LIMITED,
PUSH_WRITES_ABANDONED,
PushDropOutcome,
PushWriteAbandonReason,
inc_push_drop,
inc_push_rate_limited,
inc_push_write_abandoned,
};
use crate::push::PushPolicy;
type MetricSample = (CompositeKey, Option<Unit>, Option<SharedString>, DebugValue);
fn sample_matches_reason(sample: &MetricSample, reason: PushWriteAbandonReason) -> bool {
let (key, _, _, value) = sample;
if key.key().name() != PUSH_WRITES_ABANDONED || key.key().labels().count() != 1 {
return false;
}
let has_reason = key
.key()
.labels()
.any(|label| label.key() == "reason" && label.value() == reason.as_str());
has_reason && matches!(value, DebugValue::Counter(count) if *count == 1)
}
fn sample_matches_rate_limited(sample: &MetricSample) -> bool {
let (key, _, _, value) = sample;
key.key().name() == PUSH_RATE_LIMITED
&& key.key().labels().count() == 0
&& matches!(value, DebugValue::Counter(count) if *count == 1)
}
fn sample_matches_drop(sample: &MetricSample, outcome: PushDropOutcome, policy: &str) -> bool {
let (key, _, _, value) = sample;
if key.key().name() != PUSH_DROPS || key.key().labels().count() != 2 {
return false;
}
let has_policy = key
.key()
.labels()
.any(|label| label.key() == "policy" && label.value() == policy);
let has_outcome = key
.key()
.labels()
.any(|label| label.key() == "outcome" && label.value() == outcome.as_str());
has_policy && has_outcome && matches!(value, DebugValue::Counter(count) if *count == 1)
}
#[test]
fn push_write_abandonment_uses_a_fixed_reason_label() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
let reasons = [
PushWriteAbandonReason::IdleGraceExpired,
PushWriteAbandonReason::Shutdown,
];
::metrics::with_local_recorder(&recorder, || {
for reason in reasons {
inc_push_write_abandoned(reason);
}
});
let metric_samples = snapshotter.snapshot().into_vec();
assert_eq!(metric_samples.len(), reasons.len());
for reason in reasons {
assert!(
metric_samples
.iter()
.any(|sample| sample_matches_reason(sample, reason)),
"missing sample for reason {reason:?}"
);
}
}
#[test]
fn rate_limited_refusals_use_an_unlabelled_counter() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
::metrics::with_local_recorder(&recorder, inc_push_rate_limited);
let metric_samples = snapshotter.snapshot().into_vec();
assert_eq!(metric_samples.len(), 1);
assert!(
metric_samples.iter().any(sample_matches_rate_limited),
"missing the unlabelled rate-limited counter"
);
}
#[test]
fn push_drops_use_bounded_policy_and_outcome_labels() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
let cases = [
(
PushDropOutcome::NoDeadLetterQueue,
PushPolicy::DropIfFull,
"drop_if_full",
),
(
PushDropOutcome::DeadLetterFull,
PushPolicy::DropIfFull,
"drop_if_full",
),
(
PushDropOutcome::DeadLetterClosed,
PushPolicy::DropIfFull,
"drop_if_full",
),
(
PushDropOutcome::DeadLetterFull,
PushPolicy::WarnAndDropIfFull,
"warn_and_drop_if_full",
),
];
::metrics::with_local_recorder(&recorder, || {
for (outcome, policy, _) in cases {
inc_push_drop(outcome, policy);
}
});
let metric_samples = snapshotter.snapshot().into_vec();
assert_eq!(metric_samples.len(), cases.len());
for (outcome, _, policy) in cases {
assert!(
metric_samples
.iter()
.any(|sample| sample_matches_drop(sample, outcome, policy)),
"missing sample for outcome {outcome:?} and policy {policy}"
);
}
}
}