footing 0.5.2

Feature-gated foundations for Rust backend services
Documentation
use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};

use super::MetricsError;

/// Shared failure accounting for best-effort instrumentation.
#[derive(Clone, Debug, Default)]
pub(crate) struct RecordingFailures(Arc<AtomicU64>);

impl RecordingFailures {
    #[cfg(any(feature = "http", test))]
    pub(crate) fn count(&self) -> u64 {
        self.0.load(Ordering::Relaxed)
    }

    pub(crate) fn record(&self, result: Result<(), MetricsError>, component: &'static str) {
        if let Err(error) = result {
            let previous = self
                .0
                .update(Ordering::Relaxed, Ordering::Relaxed, |value| {
                    value.saturating_add(1)
                });
            let failures = previous.saturating_add(1);
            // Report the first failure and powers of two without exposing label values.
            if failures.is_power_of_two() {
                tracing::warn!(component, failures, error = %error, "metric recording failed");
            }
        }
    }
}