use std::sync::OnceLock;
use liminal::metrics::{
CounterHandle, GaugeHandle, MetricsRegistry, global_registry, install_global_registry,
};
use crate::server::connection::refusal::AdmissionRefusal;
use crate::server::connection::websocket::UpgradeRefusal;
use crate::server::mount::MountKind;
const CONNECTIONS_ACTIVE: &str = "liminal_connections_active";
const PUBLISHES_TOTAL: &str = "liminal_publishes_total";
const DELIVERIES_TOTAL: &str = "liminal_deliveries_total";
const TRANSPORT_DELIVERIES_TOTAL: &str = "liminal_transport_deliveries_total";
const SHEDS_TOTAL: &str = "liminal_subscription_sheds_total";
const TRANSPORT_LABEL: &str = "transport";
const ADMISSION_REFUSALS_TOTAL: &str = "liminal_admission_refusals_total";
const HANDSHAKE_REFUSALS_TOTAL: &str = "liminal_handshake_refusals_total";
const REASON_LABEL: &str = "reason";
const RECEIPT_DISPLACEMENTS_TOTAL: &str = "liminal_receipt_displacements_total";
const RECEIPT_POOL_RUNAWAY_TOTAL: &str = "liminal_receipt_pool_runaway_total";
const SCOPE_LABEL: &str = "scope";
const POOL_LABEL: &str = "pool";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ReceiptWindowScope {
LiveReceiptParticipant,
ProvenanceParticipant,
}
impl ReceiptWindowScope {
pub(crate) const LABELS: [&'static str; 2] =
["live_receipt_participant", "provenance_participant"];
const fn slot(self) -> usize {
match self {
Self::LiveReceiptParticipant => 0,
Self::ProvenanceParticipant => 1,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SharedReceiptPool {
LiveReceiptServer,
ProvenanceServer,
ProvenanceConversation,
}
impl SharedReceiptPool {
pub(crate) const LABELS: [&'static str; 3] = [
"live_receipt_server",
"provenance_server",
"provenance_conversation",
];
const fn slot(self) -> usize {
match self {
Self::LiveReceiptServer => 0,
Self::ProvenanceServer => 1,
Self::ProvenanceConversation => 2,
}
}
pub(crate) const fn label(self) -> &'static str {
Self::LABELS[self.slot()]
}
}
static SERVER_METRICS: OnceLock<ServerMetrics> = OnceLock::new();
#[derive(Clone, Debug)]
struct ServerMetrics {
connections_active: GaugeHandle,
publishes_total: CounterHandle,
deliveries_total: CounterHandle,
transport_deliveries: [CounterHandle; MOUNT_KINDS.len()],
sheds_total: CounterHandle,
admission_refusals: [CounterHandle; AdmissionRefusal::LABELS.len()],
handshake_refusals: [CounterHandle; UpgradeRefusal::LABELS.len()],
receipt_displacements: [CounterHandle; ReceiptWindowScope::LABELS.len()],
receipt_pool_runaway: [CounterHandle; SharedReceiptPool::LABELS.len()],
}
const MOUNT_KINDS: [MountKind; 3] = [MountKind::Tcp, MountKind::WebSocket, MountKind::Loopback];
const fn transport_slot(transport: MountKind) -> usize {
match transport {
MountKind::Tcp => 0,
MountKind::WebSocket => 1,
MountKind::Loopback => 2,
}
}
pub fn init() {
if SERVER_METRICS.get().is_some() {
return;
}
let Some(registry) = global_or_install() else {
return;
};
if let Some(metrics) = ServerMetrics::register(registry) {
let _ = SERVER_METRICS.set(metrics);
}
}
pub fn connection_spawned() {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.connections_active.increment();
}
}
pub fn connection_closed() {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.connections_active.decrement();
}
}
pub fn publish_accepted() {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.publishes_total.increment();
}
}
pub fn deliveries_recorded(count: u64) {
if count == 0 {
return;
}
if let Some(metrics) = SERVER_METRICS.get() {
metrics.deliveries_total.increment_by(count);
}
}
pub fn transport_deliveries_recorded(transport: MountKind, count: u64) {
if count == 0 {
return;
}
if let Some(metrics) = SERVER_METRICS.get() {
metrics.transport_deliveries[transport_slot(transport)].increment_by(count);
}
}
pub(crate) fn admission_refused(refusal: AdmissionRefusal) {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.admission_refusals[refusal.slot()].increment();
}
}
pub(crate) fn handshake_refused(refusal: &UpgradeRefusal) {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.handshake_refusals[refusal.slot()].increment();
}
}
pub fn subscription_shed() {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.sheds_total.increment();
}
}
pub(crate) fn receipt_entries_displaced(scope: ReceiptWindowScope, count: u64) {
if count == 0 {
return;
}
if let Some(metrics) = SERVER_METRICS.get() {
metrics.receipt_displacements[scope.slot()].increment_by(count);
}
}
pub(crate) fn receipt_pool_runaway_observed(pool: SharedReceiptPool) {
if let Some(metrics) = SERVER_METRICS.get() {
metrics.receipt_pool_runaway[pool.slot()].increment();
}
}
#[cfg(test)]
pub(crate) fn publishes_total_value() -> Option<u64> {
use liminal::metrics::MetricValue;
let registry = global_registry()?;
registry
.snapshot()
.metrics()
.iter()
.find(|metric| metric.name == PUBLISHES_TOTAL)
.and_then(|metric| match metric.value {
MetricValue::Counter(value) => Some(value),
MetricValue::Gauge(_) | MetricValue::Histogram(_) => None,
})
}
#[cfg(test)]
pub(crate) fn receipt_displacements_value(scope: ReceiptWindowScope) -> Option<u64> {
labelled_counter_value(
RECEIPT_DISPLACEMENTS_TOTAL,
SCOPE_LABEL,
ReceiptWindowScope::LABELS[scope.slot()],
)
}
#[cfg(test)]
pub(crate) fn receipt_pool_runaway_value(pool: SharedReceiptPool) -> Option<u64> {
labelled_counter_value(RECEIPT_POOL_RUNAWAY_TOTAL, POOL_LABEL, pool.label())
}
#[cfg(test)]
fn labelled_counter_value(name: &str, key: &str, label: &str) -> Option<u64> {
use liminal::metrics::MetricValue;
let registry = global_registry()?;
registry
.snapshot()
.metrics()
.iter()
.find(|metric| {
metric.name == name
&& metric
.labels
.iter()
.any(|(metric_key, value)| metric_key == key && value == label)
})
.and_then(|metric| match metric.value {
MetricValue::Counter(value) => Some(value),
MetricValue::Gauge(_) | MetricValue::Histogram(_) => None,
})
}
impl ServerMetrics {
fn register(registry: &MetricsRegistry) -> Option<Self> {
let connections_active = registry
.register_gauge(CONNECTIONS_ACTIVE, no_labels())
.ok()?;
let publishes_total = registry
.register_counter(PUBLISHES_TOTAL, no_labels())
.ok()?;
let deliveries_total = registry
.register_counter(DELIVERIES_TOTAL, no_labels())
.ok()?;
let mut transport_deliveries = Vec::with_capacity(MOUNT_KINDS.len());
for transport in MOUNT_KINDS {
transport_deliveries.push(
registry
.register_counter(
TRANSPORT_DELIVERIES_TOTAL,
[(TRANSPORT_LABEL, transport.as_str())],
)
.ok()?,
);
}
let transport_deliveries: [CounterHandle; MOUNT_KINDS.len()] =
transport_deliveries.try_into().ok()?;
let sheds_total = registry.register_counter(SHEDS_TOTAL, no_labels()).ok()?;
let admission_refusals = register_labelled(
registry,
ADMISSION_REFUSALS_TOTAL,
REASON_LABEL,
&AdmissionRefusal::LABELS,
)?;
let handshake_refusals = register_labelled(
registry,
HANDSHAKE_REFUSALS_TOTAL,
REASON_LABEL,
&UpgradeRefusal::LABELS,
)?;
let receipt_displacements = register_labelled(
registry,
RECEIPT_DISPLACEMENTS_TOTAL,
SCOPE_LABEL,
&ReceiptWindowScope::LABELS,
)?;
let receipt_pool_runaway = register_labelled(
registry,
RECEIPT_POOL_RUNAWAY_TOTAL,
POOL_LABEL,
&SharedReceiptPool::LABELS,
)?;
Some(Self {
connections_active,
publishes_total,
deliveries_total,
transport_deliveries,
sheds_total,
admission_refusals,
handshake_refusals,
receipt_displacements,
receipt_pool_runaway,
})
}
}
fn register_labelled<const N: usize>(
registry: &MetricsRegistry,
name: &'static str,
key: &'static str,
labels: &[&'static str; N],
) -> Option<[CounterHandle; N]> {
let mut handles = Vec::with_capacity(N);
for label in labels {
handles.push(registry.register_counter(name, [(key, *label)]).ok()?);
}
handles.try_into().ok()
}
const fn no_labels() -> std::iter::Empty<(&'static str, &'static str)> {
std::iter::empty()
}
fn global_or_install() -> Option<&'static MetricsRegistry> {
if let Some(registry) = global_registry() {
return Some(registry);
}
let _ = install_global_registry(MetricsRegistry::new());
global_registry()
}
#[cfg(test)]
mod tests {
use super::{
CONNECTIONS_ACTIVE, DELIVERIES_TOTAL, PUBLISHES_TOTAL, connection_spawned,
deliveries_recorded, init, publish_accepted,
};
use liminal::metrics::{global_registry, render};
#[test]
fn init_registers_the_three_server_families_on_the_global_registry()
-> Result<(), Box<dyn std::error::Error>> {
init();
connection_spawned();
publish_accepted();
deliveries_recorded(2);
let registry =
global_registry().ok_or("init must install and enable the global registry")?;
let exposition = render(®istry.snapshot());
assert!(exposition.contains(CONNECTIONS_ACTIVE));
assert!(exposition.contains(PUBLISHES_TOTAL));
assert!(exposition.contains(DELIVERIES_TOTAL));
Ok(())
}
}