Expand description
Traffic counter collection for moq-net sessions.
This module only collects: build a Registry, hand each session a
tier-scoped Handle via Registry::tier, and read the counters back
with Registry::snapshot (host-level rollup, e.g. a /metrics scrape) or
Registry::report (per-broadcast detail). Publishing the counters as MoQ
broadcasts lives in the moq-stats crate, which drains a Registry on an
interval and writes the JSON stats tracks.
Traffic is bucketed by an arbitrary Tier label chosen by business logic
(billing class, region, …) and, within a tier, by broadcast path and
Role (publisher = egress, subscriber = ingress). Connected sessions are
tracked separately per (tier, root, label), counting presence regardless of
whether any data flows. The label is a stats key the auth decider picks,
independent of what the session may access; the empty label holds sessions
without one. The root is the origin path the session is scoped to, so a
publisher can show each root only its own sessions; rows that differ only by
root share their label.
§Where counting happens
Counting lives in the model layer, not the wire loops. A session tags its
origin pair with a Session context (crate::Client::with_stats /
crate::Server::with_stats, which call origin::{Consumer, Producer}
with_stats); every derived handle (broadcast, announce, track, group, frame)
then attributes its reads (egress = publisher) and writes (ingress =
subscriber) through that context. So any protocol that drives the model gets
the full counter set for free, and an untagged handle pays nothing.
Per-counter semantics:
announces_started/announces_ended: cumulative broadcast announce/unannounce events on this(tier, role). Driven by the tagged announce stream on the egress side, and byorigin::Producer::routetransitions on the ingress side.announced_bytes: cumulative broadcast-name length summed over each model-visible announce and unannounce of this broadcast (the name, not the encoded message size, so hop/framing overhead isn’t charged, and the count is the same across protocol versions). Kept separate from thebytespayload counter.broadcasts_started/broadcasts_ended: per-(broadcast, context) egress subscription sentinel. The first active subscription a context opens for a broadcast bumpsbroadcasts_started; the last it closes bumpsbroadcasts_ended. Summed across contexts,broadcasts_started - broadcasts_endedis the number of distinct sessions currently subscribed (viewers on the egress side).subscriptions_started/subscriptions_ended: cumulative track-level subscriptions opened/dropped (egresstrack::Subscriber, ingresstrack::Producer).fetches: cumulative group fetches requested by a calling context, counted once per coalesced fetch at request time, a lite-07 subscription naming every group it reads included. A fetch that resolves toNotFoundstill counts. Separate fromsubscriptions_startedand the viewer refcount; fetched payload still flows intobytes/frames/groups.bytes/frames/groups: cumulative payload counters bumped as groups/frames are read (egress) or written (ingress) in the model. A session’s egressbytes/framescount only payload the peer acknowledged, or that a reliable link such as qmux’s took (seeSendStream::poll_acked), never what a reset or a lost session discarded: a frame’s bytes once an offset past them is acknowledged, and the frame with its last byte. A transport that cannot see acknowledgments counts the bytes the stream took instead. An in-process reader counts on read, unless it takes aBillto count only what it delivers onward.datagrams: cumulative datagrams of datagram tracks. A datagram belongs to no group, so it bumpsbytesbut notgroupsorframes. A session counts an egress datagram once the transport accepts it.inflated: cumulative bytes inflated from compressed groups for plain readers (in-process consumers, and egress that cannot carry compression). Every plain reader of a group shares one inflated copy, so each byte counts once, on the side of the reader whose read inflated it. Kept separate frombytes, which counts payload as stored or sent.requests(Presence): cumulative request/response admissions under the label and tier at admission.Handle::requestmeters their traffic without starting or ending a connected session.sent_bytes/received_bytes(Presence): cumulative payload bytes this node sent to, or received from, the sessions and requests under a label. Each session counts its own bytes, so a write touches no shared row; a readout sums them.sessions_started/sessions_ended(Presence): cumulative count of sessions connected/disconnected under a label on this tier. Driven byHandle::session(theSessioncontext); aSession::retagends the session on the old label and starts it on the new one.wire_tx/wire_rx(Presence): cumulative transport bytes, including overhead and retransmissions. The transport updates itscrate::transport::Wirecounters as it sends and receives, andRegistry::reportandRegistry::snapshotread each registeredWirewithout polling the session. A retag flushes its old tier and label; dropping the guard flushes its final nonnegative delta. Unsupported transports contribute zero.credit_stalls/credit_stalled_us(Presence): cumulative episodes, and microseconds, that the label’s connections spent with a write blocked on the peer’s connection-level credit (MAX_DATA), read from the samecrate::transport::Wirecounters aswire_tx.credit_held_readers_us/credit_held_evicted_us(Presence): cumulative microseconds the label’s connections had their whole receive window spent by the peer while eviction waited, on a reader behind or on an evicted reader yet to stop its stream, read from the same counters.wire_lost/cwnd_blocked_us/pacing_blocked_us(Presence): cumulative sent transport bytes declared lost, and microseconds the label’s connections had data ready to send held back by a full congestion window or by the pacer, read from the same counters. Loss lands on the label and tier current when QUIC declares it, so after a retag it need not be part of that row’swire_tx; summed over a node and a window longer than a retag, lost bytes overwire_txis the loss rate.routes_refused(Presence): cumulative routes and claims that lite-07 sessions under the label forwarded and this node refused, each withdrawn while the session stays open;Routes::refusedsplits it by reason.routes(Presence,Routes): what the label’s lite-07 sessions did on the route layer: route changes their peers caused and the broadcasts each rewrote into the origin, DEAD and DRAIN withdrawals sent and received, refusals by reason, and sessions closed for their route layer by kind.announces_peak/subscriptions_peak(Presence): the most announcements, or subscriptions, any one session under the label held at once, to compare against what bounds them. A peak never goes down; a rollup takes the largest rather than the sum.
Beside the counters, Registry::report gives each side the moment it began
holding the broadcast announced (Announced): its held announce guards last
rose from none, so overlapping announcements keep one start.
Counters are strictly monotonic (only fetch_add); a counter going
backwards across reads means the underlying entry was garbage collected
(see Registry::report) and re-created. Downstream consumers should
treat decreases as a fresh segment, summing across resets when computing
lifetime totals.
§Disabled stats
Registry::disabled builds a no-op registry: all counter bumps are
silently dropped and nothing is ever tracked. Registry::default /
Handle::default return one, so call sites can hold a Handle
unconditionally instead of threading an Option.
§Garbage collection
Registry::report refills a caller-owned Report with the current
per-broadcast detail and prunes
entries no longer referenced by any guard, so a publisher draining the
registry on an interval keeps it bounded. A registry that is never
drained accumulates one entry per broadcast path ever seen; call
Registry::report periodically if you enable a registry without
attaching a publisher. Registry::snapshot never prunes.
§Snapshot atomicity
Each counter readout loads *_ended atomics (with Acquire)
before their *_started counterparts (with Relaxed). The matching
end bumps in the RAII guards’ Drop impls use Release. With this
pairing the readout always satisfies started >= ended even on
weakly-ordered architectures (ARM, POWER): the Acquire load of
ended synchronizes-with the Release bump that produced the
observed value, making every write that happened-before that end
(including the matching start bump on whichever thread opened the
guard) visible to the reading thread. Started / payload counters can
then stay Relaxed because the visibility comes for free through
the ended pairing. The cost is a slight upward bias on the started
counts when a bump lands between the two loads, which never produces
a logically impossible (ended > started) readout for downstream.
§Cycles
A Registry built with excluded prefixes (Config::exclude) returns
empty handles (whose bumps no-op) for any path they match. The moq-stats
publisher excludes its own top-level subtree this way, breaking the feedback
loop where serving a stats broadcast would itself generate more stats
traffic. Config::exclude_rooted does the same for a subtree under each
session’s own root, where a relay presents that root’s stats.
Structs§
- Activity
- Connected sessions right now, split by whether media payload moved through them within the last 10 seconds, in either direction.
- Announced
- When each side of a
TrafficEntrybegan holding its broadcast announced: the moment the side’s held announcements last rose from none.Nonewhile the side holds none, so a broadcast unannounced and announced again starts over, while one whose announcements overlap (a publisher reconnecting before its old session closes) keeps its start. - Bill
- The payload of a track a reader counts as it delivers it onward, rather than as it reads it.
- Config
- Settings for a
Registry. Construct withConfig::newand chain thewith_*setters, then hand it toRegistry::new. - Content
- Payload-volume counters for content with the same delivery outcome.
- Failures
- Admitted sessions ending abnormally, with one cumulative count per kind.
- Handle
- Tier-scoped wrapper around
Registry. Whatcrate::Client::with_statsandcrate::Server::with_statsaccept. Cheap to clone. - Presence
- Request admissions and connected-session presence for a label on a tier, or their sum.
The session counters are cumulative connects and disconnects, so
sessions_started - sessions_endedis the current live session count. The peaks show how close one session came to the bounds on what it holds. - Refusal
- A refused session, as
Handle::refusecounts it. - Refusals
- Sessions refused before admission, with one cumulative count per kind.
- Registry
- Counter collection registry. Cheap to clone (
Arcinside for the shared state). One instance per relay; sessions get tier-scoped handles viaRegistry::tier. Themoq-statscrate drains it withRegistry::reportto publish the counters as MoQ broadcasts. - Report
- The per-broadcast detail
Registry::reportfills: one traffic entry per(broadcast, tier)and one session entry per(tier, root, label). Entries are unordered. Reuse one across drains to keep its capacity. - Route
Closes - Sessions closed for their route layer, by kind.
- Route
Refusals - Refused routes and claims by reason.
- Routes
- What a label’s moq-lite-07 sessions did on the route layer, cumulative.
- Session
- Traffic context for a connection or request, created by
Handle::sessionorHandle::request. - Session
Entry - One
(tier, root, label)row of aReport. - Session
Info - A connected session or admitted request, as
Handle::sessionandHandle::requestrecord it. - Snapshot
- A point-in-time, host-level rollup of a registry’s counters, returned
by
Registry::snapshot. - Tier
- Traffic-class label that selects which counter set a session’s bumps record
in, so a single
Registrycan split customer-facing, cluster-peer, regional, etc. traffic. Each tracked broadcast keeps a per-tier counter set on both its publisher and subscriber sides. - Totals
- Cumulative bytes and messages sent and received, returned per
Messageclass bySnapshot::control. - Traffic
- A cumulative traffic counter readout for one slice (a broadcast on a
(tier, role), or any sum of such slices). - Traffic
Entry - One
(broadcast, tier)row of aReport. - Wire
- Registered wire counters; dropping this adds their final reading and unregisters them.
- Withdrawals
- Route withdrawals by kind.
Enums§
- Failure
- The reason a session ended abnormally.
- Message
- The bounded class of a successfully encoded or decoded control message.
- Refusal
Kind - The reason a session was refused.
- Role
- Publisher (egress) vs subscriber (ingress) side of a broadcast, used as a
label on a
Snapshottraffic row. The internal bump paths track the side statically, so this only surfaces on the aggregate read side. - Route
Close - Why a session closed for its route layer.
- Route
Refusal - Why this node refused a route or claim a peer forwarded, keeping the session.
- Withdrawal
- A message that withdraws routes.