Skip to main content

ash_lock/
telemetry.rs

1//! Metrics for `ash-lock`.
2//!
3//! MOCK: the spec calls for exporting these counters through an `ash-telemetry`
4//! crate, which does not yet exist. This module provides the same
5//! `AtomicU64`-backed counters with a stable API so that, once `ash-telemetry`
6//! lands, the export sink can be swapped in without touching call sites.
7//!
8//! The counter names mirror the spec:
9//! - `ash_lock_acquisitions_total`
10//! - `ash_lock_contention_events_total`
11//! - `ash_lock_renewal_failures_total`
12
13use std::sync::atomic::{AtomicU64, Ordering};
14
15/// Number of successful lock acquisitions.
16pub static ACQUISITIONS_TOTAL: AtomicU64 = AtomicU64::new(0);
17
18/// Number of times an acquire had to block/retry due to contention.
19pub static CONTENTION_EVENTS_TOTAL: AtomicU64 = AtomicU64::new(0);
20
21/// Number of watchdog renewal failures.
22pub static RENEWAL_FAILURES_TOTAL: AtomicU64 = AtomicU64::new(0);
23
24#[inline]
25pub(crate) fn inc_acquisitions() {
26    ACQUISITIONS_TOTAL.fetch_add(1, Ordering::Relaxed);
27}
28
29#[inline]
30pub(crate) fn inc_contention() {
31    CONTENTION_EVENTS_TOTAL.fetch_add(1, Ordering::Relaxed);
32}
33
34#[inline]
35pub(crate) fn inc_renewal_failures() {
36    RENEWAL_FAILURES_TOTAL.fetch_add(1, Ordering::Relaxed);
37}
38
39/// A point-in-time snapshot of every exported counter. Useful in tests and for
40/// the eventual `ash-telemetry` scrape endpoint.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct Snapshot {
43    /// `ash_lock_acquisitions_total`
44    pub acquisitions_total: u64,
45    /// `ash_lock_contention_events_total`
46    pub contention_events_total: u64,
47    /// `ash_lock_renewal_failures_total`
48    pub renewal_failures_total: u64,
49}
50
51/// Read all counters atomically enough for observability purposes.
52pub fn snapshot() -> Snapshot {
53    Snapshot {
54        acquisitions_total: ACQUISITIONS_TOTAL.load(Ordering::Relaxed),
55        contention_events_total: CONTENTION_EVENTS_TOTAL.load(Ordering::Relaxed),
56        renewal_failures_total: RENEWAL_FAILURES_TOTAL.load(Ordering::Relaxed),
57    }
58}