rust_observer 0.2.2

Express telemetry rust SDK
Documentation
use once_cell::sync::Lazy;
use std::sync::atomic::{AtomicU64, Ordering};

static WEBSOCKET_METRICS: Lazy<WebSocketMetricsData> = Lazy::new(WebSocketMetricsData::default);

#[derive(Debug, Default)]
pub struct WebSocketMetricsData {
    pub new_connections: AtomicU64,
    pub disconnections: AtomicU64,
}

impl WebSocketMetricsData {
    pub async fn increment_new_connections(&self) {
        self.new_connections.fetch_add(1, Ordering::Relaxed);
    }

    pub async fn increment_disconnections(&self) {
        self.disconnections.fetch_add(1, Ordering::Relaxed);
    }

    pub async fn flush(&self) -> (u64, u64) {
        let new_connections = self.new_connections.swap(0, Ordering::Relaxed);
        let disconnections = self.disconnections.swap(0, Ordering::Relaxed);
        (new_connections, disconnections)
    }
}

pub struct WebSocketMetrics;

impl WebSocketMetrics {
    pub async fn increment_new_connections() {
        WEBSOCKET_METRICS.increment_new_connections().await;
    }

    pub async fn increment_disconnections() {
        WEBSOCKET_METRICS.increment_disconnections().await;
    }

    pub async fn flush() -> Option<(u64, u64)> {
        let result = WEBSOCKET_METRICS.flush().await;
        Some(result)
    }
}

#[derive(Debug, Clone)]
pub struct WebSocketMetricsSnapshot {
    pub new_connections: u64,
    pub disconnections: u64,
}