Metrics
vix::realtime::Metrics is a thread-safe collector for Realtime runtime metrics.
It records information such as:
active rooms
active sessions
commands
events
snapshots
replay
session resume
presence
transport traffic
errors
durationsMetrics are observational. They do not affect authoritative room state, command ordering, or persistence.
Create a metrics collector
vix::realtime::Metrics metrics;A new collector starts with all values at zero.
Record a metric
For example, record a successfully opened room:
metrics.record_room_opened();Record a created session:
metrics.record_session_created();Record persisted events:
metrics.record_events_persisted(2);Metrics are explicit
Creating a Metrics object does not automatically instrument the Realtime runtime.
For example:
vix::realtime::Metrics metrics;
server.start();does not automatically populate every counter in metrics.
The application or integration layer records the operations it wants to observe.
server.start();
metrics.record_room_opened();This makes Metrics an explicit collector rather than a hidden global monitoring system.
Capture a snapshot
Read the current metrics with:
auto snapshot =
metrics.snapshot();MetricsSnapshot contains a point-in-time copy of the collected values.
For example:
auto snapshot =
metrics.snapshot();
auto rooms =
snapshot.activeRooms;
auto events =
snapshot.eventsPersisted;The snapshot is suitable for monitoring and health reporting.
Because metrics may continue changing while the snapshot is being assembled, it should not be treated as a transactional view of the runtime.
Gauges
Some metrics describe the current runtime state.
These are gauges:
| Metric | Meaning |
|---|---|
activeRooms | Rooms currently managed |
activeSessions | Logical sessions currently managed |
attachedConnections | Sessions with attached connections |
queuedCommands | Commands currently waiting in room queues |
activePresence | Presence records currently considered active |
Set a gauge directly:
metrics.set_active_rooms(5);Or increment it:
metrics.increment_active_rooms();and decrement it:
metrics.decrement_active_rooms();Gauge decrements never go below zero.
Room metrics
Record successful room lifecycle operations with:
metrics.record_room_opened();
metrics.record_room_closed();The snapshot exposes:
snapshot.roomsOpened;
snapshot.roomsClosed;These are cumulative counters since the latest reset.
Session metrics
Record logical session lifecycle operations with:
metrics.record_session_created();
metrics.record_session_closed();Read them with:
snapshot.sessionsCreated;
snapshot.sessionsClosed;Current session count is tracked separately through:
snapshot.activeSessions;The distinction is:
activeSessions
current number of sessions
sessionsCreated
total sessions created since resetConnection metrics
Record connection attachment:
metrics.record_connection_attached();and detachment:
metrics.record_connection_detached();The snapshot contains:
snapshot.connectionsAttached;
snapshot.connectionsDetached;
snapshot.attachedConnections;attachedConnections is the current gauge.
The other two values are cumulative counters.
Command metrics
Record a command added to a room queue with:
metrics.record_command_enqueued();Record the final command result with:
metrics.record_command_result(
vix::realtime::CommandStatus::Accepted);The snapshot contains:
commandsEnqueued
commandsProcessed
commandsAccepted
commandsRejected
commandsIgnoredFor example:
auto accepted =
snapshot.commandsAccepted;Every call to record_command_result() increments commandsProcessed and the counter corresponding to the supplied status.
Command duration
A command duration can be recorded at the same time:
metrics.record_command_result(
vix::realtime::CommandStatus::Accepted,
std::chrono::microseconds{250});The snapshot tracks:
commandDurationCount
commandDurationTotalMicros
commandDurationMaxMicrosConvenience methods provide the average and maximum:
auto average =
snapshot.average_command_duration();
auto maximum =
snapshot.maximum_command_duration();If no command duration has been recorded, the average is zero.
Event metrics
Record persisted authoritative events with:
metrics.record_events_persisted(3);Read the total with:
snapshot.eventsPersisted;Event delivery can also be recorded:
metrics.record_event_dispatch(
3,
2,
1);The arguments represent:
3 selected recipients
2 successful deliveries
1 failed deliveryThe snapshot then tracks:
eventDispatches
eventRecipients
eventDeliveriesSucceeded
eventDeliveriesFailedEvent delivery success rate
Calculate the delivery success rate with:
double rate =
snapshot.event_delivery_success_rate();For example:
8 successful
2 failed
success rate = 0.8When no event delivery has been attempted, the method returns:
1.0Snapshot metrics
Record successful snapshot creation with:
metrics.record_snapshot_created();Record snapshot restoration with:
metrics.record_snapshot_restored();The snapshot contains:
snapshot.snapshotsCreated;
snapshot.snapshotsRestored;A duration can also be supplied:
metrics.record_snapshot_created(
std::chrono::microseconds{500});Read duration statistics with:
snapshot.average_snapshot_duration();
snapshot.maximum_snapshot_duration();Creation and restoration durations contribute to the same snapshot-duration aggregates.
Replay metrics
Record one successful replay with:
metrics.record_replay(
10,
2048);The arguments mean:
10 events replayed
2048 serialized bytes processedThe snapshot contains:
replayOperations
replayEventsApplied
replayBytesA replay duration can also be recorded:
metrics.record_replay(
10,
2048,
std::chrono::microseconds{800});Read timing information with:
snapshot.average_replay_duration();
snapshot.maximum_replay_duration();Session resume metrics
Record a successful resume attempt with:
metrics.record_resume_attempt(true);Record a failed attempt with:
metrics.record_resume_attempt(false);The snapshot contains:
resumeAttempts
resumeSucceeded
resumeFailedCalculate the success rate with:
double rate =
snapshot.resume_success_rate();When no resume attempt has been recorded, the method returns:
1.0Presence metrics
Record logical presence joins with:
metrics.record_presence_join();and leaves with:
metrics.record_presence_leave();The snapshot contains:
snapshot.presenceJoins;
snapshot.presenceLeaves;
snapshot.activePresence;activePresence is a gauge and must be maintained separately when the application wants to expose the current active presence count.
Transport metrics
Record a received transport message with its size:
metrics.record_transport_received(512);Record a sent message:
metrics.record_transport_sent(256);The snapshot tracks:
transportMessagesReceived
transportBytesReceived
transportMessagesSent
transportBytesSentFor example:
auto bytes =
snapshot.transportBytesReceived;Protocol errors
Record an invalid protocol message with:
metrics.record_protocol_error();Read the cumulative value with:
snapshot.protocolErrors;Runtime errors
Record a runtime error with:
metrics.record_error();Read the value with:
snapshot.errors;Check whether any runtime or protocol error has been recorded:
if (snapshot.has_errors())
{
// At least one error was recorded.
}has_errors() returns true when either:
errors > 0or:
protocolErrors > 0Reset metrics
Reset every gauge and cumulative counter with:
metrics.reset();After reset:
auto snapshot =
metrics.snapshot();all collected values start again from zero.
Resetting metrics does not affect:
rooms
sessions
events
snapshots
presence
connectionsIt only clears the metrics collector.
Thread safety
Metrics is safe to update from multiple threads.
For example, different runtime integrations can share one collector:
auto metrics =
std::make_shared<
vix::realtime::Metrics>();Metrics updates are observational and do not synchronize authoritative application operations.
Counters saturate at the maximum std::uint64_t value instead of wrapping.
Metrics and health
A metrics collector can be supplied to HealthMonitor.
auto metrics =
std::make_shared<
vix::realtime::Metrics>();
vix::realtime::HealthMonitor monitor{
server,
metrics};Health reports can then include the metrics snapshot and use recorded error counters when evaluating health.
See Health for health reporting.
Main model
The metrics flow is:
runtime operation
|
v
Metrics::record_*()
|
v
Metrics
|
v
snapshot()
|
v
MetricsSnapshotThe important distinction is:
Realtime runtime
performs application operations
Metrics
observes explicitly recorded operationsMetrics provide counters, gauges, rates, and durations without becoming part of authoritative room behavior.
Continue with Health for runtime health inspection.