Skip to main content

Module stats

Module stats 

Source
Expand description

Backpressure and traffic metrics.

StatsRecorder is a cheap-to-clone handle over atomic counters — global (all connections/rooms) and per-room. It is the single hook for the numbers an operator needs under load:

  • connections — accepted sockets (integrator accept loop)
  • rooms — live room count (wired into crate::room::RoomManager)
  • messages/bytes in/out — traffic volume; “in” is recorded by the integrator’s read loop (ws-kit provides building blocks, not the read loop), “out” is recorded by room broadcasts and by hub callers
  • drops — messages that hit a closed/empty broadcast channel (WsError::BroadcastFull), i.e. backpressure that lost data

Read state via StatsRecorder::snapshot — an immutable Stats struct for logging, health endpoints, or assertion in tests.

With the optional metrics feature (off by default) the same counters are additionally emitted through the metrics facade (ws_kit_* counters/gauges), exportable to Prometheus/OTLP with any exporter — the same pattern the breaker crate uses.

§Example

use ws_kit::stats::StatsRecorder;

let stats = StatsRecorder::new();
stats.inc_connections();
stats.record_message_in(128);
let snap = stats.snapshot();
assert_eq!(snap.connections, 1);
assert_eq!(snap.messages_in, 1);
assert_eq!(snap.bytes_in, 128);

Structs§

RoomStats
Traffic counters for a single room.
Stats
Point-in-time snapshot of global and per-room counters.
StatsRecorder
Cheap-to-clone handle over global + per-room atomic counters.