#[cfg(feature = "metrics")]
use metrics::counter;
pub const DECODE_REJECTIONS: &str = "wireframe_decode_rejections_total";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum DecodeRejectionPath {
Message,
#[cfg(feature = "serializer-serde")]
Serde,
#[cfg(not(loom))]
Preamble,
Envelope,
}
impl DecodeRejectionPath {
#[cfg(feature = "metrics")]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Message => "message",
#[cfg(feature = "serializer-serde")]
Self::Serde => "serde",
#[cfg(not(loom))]
Self::Preamble => "preamble",
Self::Envelope => "envelope",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum DecodeRejectionReason {
LimitExceeded,
TrailingBytes,
#[cfg(not(loom))]
DecodeError,
}
impl DecodeRejectionReason {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::LimitExceeded => "limit_exceeded",
Self::TrailingBytes => "trailing_bytes",
#[cfg(not(loom))]
Self::DecodeError => "decode_error",
}
}
}
#[cfg(feature = "metrics")]
pub(crate) fn inc_decode_rejection(path: DecodeRejectionPath, reason: DecodeRejectionReason) {
counter!(
DECODE_REJECTIONS,
"path" => path.as_str(),
"reason" => reason.as_str()
)
.increment(1);
}
#[cfg(not(feature = "metrics"))]
pub(crate) fn inc_decode_rejection(_path: DecodeRejectionPath, _reason: DecodeRejectionReason) {}
#[cfg(all(test, feature = "metrics", not(loom)))]
mod tests {
use metrics::{SharedString, Unit};
use metrics_util::{
CompositeKey,
debugging::{DebugValue, DebuggingRecorder},
};
use super::{
DECODE_REJECTIONS,
DecodeRejectionPath,
DecodeRejectionReason,
inc_decode_rejection,
};
type MetricSample = (CompositeKey, Option<Unit>, Option<SharedString>, DebugValue);
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)
}
fn sample_matches_rejection(
sample: &MetricSample,
path: DecodeRejectionPath,
reason: DecodeRejectionReason,
) -> bool {
let (key, _, _, value) = sample;
if key.key().name() != DECODE_REJECTIONS || key.key().labels().count() != 2 {
return false;
}
if !has_label(key, "path", path.as_str()) {
return false;
}
if !has_label(key, "reason", reason.as_str()) {
return false;
}
matches!(value, DebugValue::Counter(count) if *count == 1)
}
#[test]
fn decode_rejections_use_fixed_path_and_reason_labels() {
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
let base_samples = [
(
DecodeRejectionPath::Message,
DecodeRejectionReason::LimitExceeded,
),
(
DecodeRejectionPath::Preamble,
DecodeRejectionReason::DecodeError,
),
(
DecodeRejectionPath::Envelope,
DecodeRejectionReason::TrailingBytes,
),
];
#[cfg(feature = "serializer-serde")]
let serde_case = [(
DecodeRejectionPath::Serde,
DecodeRejectionReason::LimitExceeded,
)];
#[cfg(not(feature = "serializer-serde"))]
let serde_case = [];
let samples: Vec<_> = base_samples.into_iter().chain(serde_case).collect();
::metrics::with_local_recorder(&recorder, || {
for &(path, reason) in &samples {
inc_decode_rejection(path, reason);
}
});
let metric_samples = snapshotter.snapshot().into_vec();
assert_eq!(
metric_samples.len(),
samples.len(),
"metric_samples.len() should equal samples.len()"
);
for &(path, reason) in &samples {
assert!(
metric_samples
.iter()
.any(|sample| sample_matches_rejection(sample, path, reason)),
"assertion failed: metric_samples .iter() .any(|sample| \
sample_matches_rejection(sample, path, reason))"
);
}
}
}