commonware-p2p 2026.4.0

Communicate with authenticated peers over encrypted connections.
Documentation
use crate::authenticated::discovery::metrics;
use commonware_runtime::Metrics as RuntimeMetrics;
use prometheus_client::metrics::{counter::Counter, family::Family, gauge::Gauge};

/// Metrics for the [super::Actor]
#[derive(Default)]
pub struct Metrics {
    /// The total number of unique peers in all peer sets being tracked.
    /// Includes bootstrappers, even if they are not in any peer set.
    /// Does not include self, despite having a record for it.
    pub tracked: Gauge,

    /// Blocked peers (value = expiry time as epoch millis).
    pub blocked: Family<metrics::Peer, Gauge>,

    /// The total number of outstanding reservations.
    pub reserved: Gauge,

    /// Unix timestamp in milliseconds when each connected peer became active.
    pub connected: Family<metrics::Peer, Gauge>,

    /// A count of the number of rate-limited reservation events for each peer.
    pub limits: Family<metrics::Peer, Counter>,

    /// A count of the number of updates for each peer.
    pub updates: Family<metrics::Peer, Counter>,
}

impl Metrics {
    /// Create and return a new set of metrics, registered with the given context.
    pub fn init<E: RuntimeMetrics>(context: E) -> Self {
        let metrics = Self::default();
        context.register(
            "tracked",
            "Total number of unique peers in all peer sets being tracked",
            metrics.tracked.clone(),
        );
        context.register(
            "blocked",
            "Blocked peers (value = expiry time as epoch millis)",
            metrics.blocked.clone(),
        );
        context.register(
            "reserved",
            "Total number of outstanding reservations",
            metrics.reserved.clone(),
        );
        context.register(
            "connected",
            "Unix timestamp in milliseconds when each connected peer became active",
            metrics.connected.clone(),
        );
        context.register(
            "limits",
            "Count of the number of rate-limited reservation events for each peer",
            metrics.limits.clone(),
        );
        context.register(
            "updates",
            "Count of the number of updates for each peer",
            metrics.updates.clone(),
        );
        metrics
    }
}