servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Process-owned trace counters, independent of application readiness and metrics registries.
use std::sync::{
    Arc, OnceLock,
    atomic::{AtomicU64, Ordering},
};
static PROCESS: OnceLock<Arc<Counters>> = OnceLock::new();
#[derive(Debug, Default)]
pub(super) struct Counters {
    pub(super) queued: AtomicU64,
    pub(super) exported: AtomicU64,
    pub(super) dropped: AtomicU64,
    pub(super) failed: AtomicU64,
    pub(super) lost: AtomicU64,
    pub(super) shutdown: AtomicU64,
}
/// A point-in-time observation of Servlet's explicitly installed process trace pipeline.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct TraceStats {
    /// Completed spans currently waiting for batching.
    pub queued_spans: u64,
    /// Spans in batches acknowledged successfully by the collector.
    pub exported_spans: u64,
    /// Spans rejected because the queue was full or closed.
    pub dropped_spans: u64,
    /// Export attempts that failed, timed out, or received a partial rejection.
    pub export_failures: u64,
    /// Spans in failed batches, conservatively including partial-success batches.
    pub failed_spans: u64,
    /// Queued spans abandoned after the shutdown allowance expired.
    pub shutdown_dropped_spans: u64,
}
/// Return counters, or zeros when Servlet owns no process trace pipeline.
pub fn trace_stats() -> TraceStats {
    PROCESS
        .get()
        .map_or_else(TraceStats::default, |c| c.snapshot())
}
pub(super) fn register(counters: Arc<Counters>) {
    let _ = PROCESS.set(counters);
}
impl Counters {
    pub(super) fn snapshot(&self) -> TraceStats {
        TraceStats {
            queued_spans: self.queued.load(Ordering::Relaxed),
            exported_spans: self.exported.load(Ordering::Relaxed),
            dropped_spans: self.dropped.load(Ordering::Relaxed),
            export_failures: self.failed.load(Ordering::Relaxed),
            failed_spans: self.lost.load(Ordering::Relaxed),
            shutdown_dropped_spans: self.shutdown.load(Ordering::Relaxed),
        }
    }
}
pub(crate) fn render() -> String {
    let stats = trace_stats();
    let mut output = String::new();
    for (name, kind, value) in [
        ("queued_spans", "gauge", stats.queued_spans),
        ("exported_spans_total", "counter", stats.exported_spans),
        ("dropped_spans_total", "counter", stats.dropped_spans),
        ("export_failures_total", "counter", stats.export_failures),
        ("failed_spans_total", "counter", stats.failed_spans),
        (
            "shutdown_dropped_spans_total",
            "counter",
            stats.shutdown_dropped_spans,
        ),
    ] {
        output.push_str(&format!(
            "# TYPE servlet_process_trace_{name} {kind}\nservlet_process_trace_{name} {value}\n"
        ));
    }
    output
}