Skip to content

[dev] Add INFO COMMANDSTATS tracking with per-command success rates#1728

Merged
Mathos1432 merged 5 commits intodevfrom
users/matrembl/commandstats-dev
Apr 24, 2026
Merged

[dev] Add INFO COMMANDSTATS tracking with per-command success rates#1728
Mathos1432 merged 5 commits intodevfrom
users/matrembl/commandstats-dev

Conversation

@Mathos1432
Copy link
Copy Markdown
Contributor

Why is this change being made?

Garnet lacks Redis-compatible command usage statistics. Users and monitoring tools that rely on INFO COMMANDSTATS get no data, making it harder to understand workload composition and identify failing commands. This cherry-picks #1702 (merged to main) onto dev.

What changed?

  • libs/server/Metrics/CommandStats.cs — New per-command statistics structure (calls, failed_calls, rejected_calls) array-indexed by RespCommand for O(1) access
  • libs/server/Resp/RespServerSession.cs — Per-session command stats field, counter increments in ProcessMessages loop, commandErrorWritten flag for error detection, history handoff on dispose
  • libs/server/Resp/RespServerSessionOutput.cs — WriteError overloads set commandErrorWritten flag
  • libs/server/Resp/Objects/ObjectStoreUtils.cs — AbortWithErrorMessage sets commandErrorWritten flag
  • libs/server/Metrics/GarnetServerMonitor.cs — History storage from disposed sessions, on-demand aggregation via Servers property, COMMANDSTATS reset support, dispose fix for when Start() was never called, resetEventFlags uses Enum.GetValues to fix pre-existing KeyNotFoundException
  • libs/server/Metrics/GarnetServerMetrics.cs — globalCommandStats and historyCommandStats fields
  • libs/server/Metrics/Info/GarnetInfoMetrics.cs — PopulateCommandStatsInfo with on-demand aggregation, canonical command names via RespCommandsInfo.GetRespCommandName()
  • libs/server/Servers/GarnetServerOptions.cs — CommandStatsMonitor bool property
  • libs/server/StoreWrapper.cs — Monitor creation includes CommandStatsMonitor and LatencyMonitor (fixes pre-existing NRE when only --latency-monitor enabled)
  • libs/host/Configuration/Options.cs — --commandstats-monitor CLI flag
  • libs/host/Configuration/Redis/RedisOptions.cs — commandstats-tracking Redis config alias
  • libs/host/defaults.conf — CommandStatsMonitor default entry
  • libs/common/Metrics/InfoMetricsType.cs — COMMANDSTATS enum value
  • libs/server/SessionParseStateExtensions.cs — COMMANDSTATS parser case
  • test/Garnet.test/RespCommandStatsTests.cs — 8 test cases covering disabled state, call tracking, failed calls, usec fields, multiple commands, success rate with periodic sampling, INFO SERVER status, Redis format
  • test/Garnet.test/RespInfoTests.cs — Extended InfoSectionOptionsTest to validate all 6 excluded sections are absent from bare INFO
  • test/Garnet.test/TestUtils.cs — commandStatsMonitor parameter for test server setup

How was this validated?

  • All 12 new/updated tests pass on net8.0 and net10.0 (8 CommandStats + 4 InfoSection tests)
  • BDN benchmarks show acceptable overhead: PING ~7.8%, SET/GET 0-4% (counter increments only, no Stopwatch)
  • Existing test suite unaffected (CI green on Add INFO COMMANDSTATS tracking with per-command success rates #1702)
  • Integration pipeline: [link]

Implements Redis-compatible INFO COMMANDSTATS support, gated behind
--commandstats-monitor config flag. Tracks per-command calls, failed_calls,
and rejected_calls using lightweight counter increments (no Stopwatch).

Key design decisions:
- Array-indexed by RespCommand enum for O(1) access
- Per-session counters (single-writer, no locking on hot path)
- On-demand aggregation from active sessions + disposed session history
- No latency tracking (usec=0) to avoid Stopwatch overhead
- Monitor dispose handles case where Start() was never called (EmbeddedServer)
- Include LatencyMonitor in monitor creation condition to fix pre-existing NRE
- Use Enum.GetValues for resetEventFlags to fix pre-existing KeyNotFoundException

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Mathos1432 Mathos1432 marked this pull request as ready for review April 22, 2026 20:43
Copilot AI review requested due to automatic review settings April 22, 2026 20:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Redis-compatible INFO COMMANDSTATS support to Garnet, enabling per-command observability (calls / failed_calls / rejected_calls) with low overhead and opt-in tracking.

Changes:

  • Introduces CommandStats data structure (array-indexed by RespCommand) and wires per-session tracking + history aggregation.
  • Extends INFO plumbing to support a new COMMANDSTATS section (excluded from default INFO) and exposes enablement via CLI/config.
  • Adds unit tests validating disabled behavior, call/failure tracking, formatting, and INFO section exclusions.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/Garnet.test/TestUtils.cs Adds commandStatsMonitor parameter to test server creation.
test/Garnet.test/RespInfoTests.cs Extends INFO section exclusion assertions to include additional sections + Commandstats.
test/Garnet.test/RespCommandStatsTests.cs Adds coverage for COMMANDSTATS output and tracking behavior.
libs/server/StoreWrapper.cs Ensures GarnetServerMonitor is created when commandstats/latency monitors are enabled.
libs/server/SessionParseStateExtensions.cs Adds parser support for COMMANDSTATS section token.
libs/server/Servers/GarnetServerOptions.cs Adds CommandStatsMonitor option to server configuration.
libs/server/Resp/RespServerSessionOutput.cs Marks commandErrorWritten when WriteError helpers are used.
libs/server/Resp/RespServerSession.cs Adds per-session CommandStats and increments calls/failed/rejected counters.
libs/server/Resp/Objects/ObjectStoreUtils.cs Marks commandErrorWritten for abort/error helper path.
libs/server/Metrics/Info/GarnetInfoMetrics.cs Adds INFO COMMANDSTATS section generation + aggregation logic.
libs/server/Metrics/GarnetServerMonitor.cs Adds history aggregation for commandstats, reset support, and Start/Dispose fix.
libs/server/Metrics/GarnetServerMetrics.cs Adds global + history commandstats fields to global metrics.
libs/server/Metrics/CommandStats.cs Implements the per-command counters and aggregation/reset helpers.
libs/host/defaults.conf Adds default config entry for CommandStatsMonitor.
libs/host/Configuration/Redis/RedisOptions.cs Adds commandstats-tracking config alias mapping.
libs/host/Configuration/Options.cs Adds --commandstats-monitor CLI flag and wires it into server options.
libs/common/Metrics/InfoMetricsType.cs Adds COMMANDSTATS enum value for INFO sections.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/Garnet.test/RespCommandStatsTests.cs
Comment thread libs/server/Resp/RespServerSession.cs
Comment thread libs/server/Metrics/CommandStats.cs
@Mathos1432 Mathos1432 changed the title Add INFO COMMANDSTATS tracking with per-command success rates [dev] Add INFO COMMANDSTATS tracking with per-command success rates Apr 23, 2026
@Mathos1432 Mathos1432 merged commit 2997b98 into dev Apr 24, 2026
23 checks passed
@Mathos1432 Mathos1432 deleted the users/matrembl/commandstats-dev branch April 24, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants