Skip to content

Add statsd metrics output ability - #334

Merged
fcostaoliveira merged 26 commits into
redis:masterfrom
jeremyplichta:feature/statsd-metrics
Feb 24, 2026
Merged

fcostaoliveira merged 26 commits into
redis:masterfrom
jeremyplichta:feature/statsd-metrics

Conversation

@jeremyplichta

@jeremyplichta jeremyplichta commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor

This pull request introduces real-time metrics streaming and visualization for memtier_benchmark using StatsD, Graphite, and Grafana. Users can now stream live benchmark metrics to a StatsD server, visualize them in Grafana dashboards, and compare multiple benchmark runs. The PR includes code changes, documentation, and all necessary configuration for an out-of-the-box monitoring stack.

Real-Time Metrics Streaming and Visualization:

  • Added StatsD integration to memtier_benchmark, allowing live metrics to be sent to a StatsD server. New command-line options (--statsd-host, --statsd-port, --statsd-prefix, --statsd-run-label, --graphite-port) enable configuration of metrics streaming. Defaults are set for port, prefix, run label, and Graphite HTTP port. (memtier_benchmark.cpp, config_init_defaults, argument parsing) [1] [2] [3] [4] [5]

  • Added new source files statsd.cpp and statsd.h to the build system for StatsD support. (Makefile.am)

Monitoring Stack and Dashboard Provisioning:

  • Introduced a ready-to-use Docker Compose setup (docker-compose.statsd.yml) for running Graphite (with StatsD receiver) and Grafana, including volume mounts and provisioning for dashboards and datasources.

  • Added provisioning files for Grafana dashboards and Graphite datasource (grafana/provisioning/dashboards/dashboard.yml, grafana/provisioning/datasources/graphite.yml). [1] [2]

  • Included a pre-built Grafana dashboard (grafana/dashboards/memtier.json) for visualizing key metrics such as ops/sec, latency, throughput, connections, errors, and progress, with support for comparing multiple benchmark runs via run labels.

  • Added StatsD configuration for Graphite with a 1-second flush interval for real-time updates. (graphite/statsd-config.js)

Testing:

  • Added tests/test_statsd.py for StatsD integration testing.

Documentation and Usage Guides:

  • Updated README.md with a quick start for real-time metrics, StatsD options, and links to detailed guides.

  • Added a comprehensive guide (docs/REALTIME_METRICS.md) explaining setup, usage, metrics reference, dashboard features, troubleshooting, and architecture for real-time metrics visualization.


Note

Medium Risk
Touches the benchmark’s hot-path stats collection and adds new concurrent histogram aggregation plus network I/O, which could affect performance or introduce subtle race/overhead issues. Changes are gated behind --statsd-host and covered by new unit/integration tests, reducing rollout risk.

Overview
Adds optional real-time metrics streaming from memtier_benchmark to StatsD, including new CLI flags (--statsd-host/port/prefix/run-label and --graphite-port) and per-second emission of throughput, latency (avg + configured percentiles), connection/error, and progress metrics plus Graphite start/end event annotations.

Introduces a new statsd_client implementation and thread-safe instantaneous histogram aggregation to compute percentiles across threads, alongside an out-of-the-box Graphite+Grafana docker-compose stack (with provisioning and a prebuilt dashboard), new StatsD-focused tests, and a CI job that runs the integration against the docker-compose stack.

Written by Cursor Bugbot for commit ebb3939. This will update automatically on new commits. Configure here.

jeremyplichta and others added 11 commits January 21, 2026 16:57
- Add statsd.h and statsd.cpp implementing a simple UDP StatsD client
- Add --statsd-host, --statsd-port, --statsd-prefix command-line options
- Integrate StatsD metric emission into the main benchmark loop
- Add Docker Compose setup with Graphite/StatsD and Grafana
- Add Grafana provisioning files and pre-configured dashboard

Metrics exported:
- ops_sec / ops_sec_avg: Operations per second
- bytes_sec / bytes_sec_avg: Throughput in bytes/sec
- latency_ms / latency_avg_ms: Latency in milliseconds
- connections: Active connection count
- progress_pct: Benchmark progress percentage
- connection_errors: Connection error count (when > 0)

Usage:
  ./memtier_benchmark -s <redis> --statsd-host=localhost --statsd-port=8125
  docker-compose -f docker-compose.statsd.yml up -d
  Open http://localhost:3000 for Grafana (admin/admin)
Use null UID to reference the default datasource instead of a variable
that requires manual configuration.
- Set StatsD flush interval to 1 second (was 10 seconds)
- Configure Graphite storage schema with 1-second retention
- Add timer percentiles (50, 90, 95, 99, 99.9)
- Add --statsd-run-label option to label benchmark runs (default: 'default')
- Run label is included in metric path: memtier.<run_label>.ops_sec
- Add Graphite events API integration for start/end annotations
- Update Grafana dashboard with run_label template variable
- Enable multi-select for overlay comparisons of different runs
- Add annotations to show benchmark start/end on timeline

Usage:
  ./memtier_benchmark -s redis --statsd-host=localhost --statsd-run-label=asm_scaling
  ./memtier_benchmark -s redis --statsd-host=localhost --statsd-run-label=orig_scaling

In Grafana, use the 'Run Label' dropdown to select which runs to display.
The previous regex required a trailing dot after the run label which
didn't match the full metric paths returned by Graphite. Updated the
query to fetch full paths and fixed the regex to extract the
second-to-last segment as the run label.
- Simplified query to stats.gauges.memtier.* (returns run labels directly)
- Removed regex since Graphite returns just the segment name
- Updated datasource UID to match the auto-provisioned Graphite datasource
- Changed aliasByNode to show both run_label and metric name (indices 3,4)
- Set spanNulls to false so gaps appear when no data is being sent
- Legends now show 'asm_scaling.ops_sec' instead of just 'asm_scaling'
- Set spanNulls to 2000ms threshold to connect nearby points (fixes dots issue)
- Send zero values for all gauges when benchmark completes (fixes stale data)
- Graphs now show connected lines during runs and drop to 0 when finished
- Create docs/REALTIME_METRICS.md with comprehensive usage guide:
  - Quick start instructions for Docker Compose setup
  - Command-line options reference with examples
  - Multi-run comparison feature prominently documented with examples
  - Metrics reference table (ops_sec, latency, throughput, etc.)
  - Grafana dashboard usage with overlay capabilities
  - Troubleshooting section for common issues
  - Architecture diagram and external StatsD integration

- Update README.md with new Real-Time Metrics section:
  - Quick start example for immediate use
  - StatsD options table documenting all --statsd-* arguments
  - Link to detailed guide for advanced usage

- Remove STATSD_IMPLEMENTATION_CONTEXT.md (internal dev notes)
- Fix StatsD config mount path to /opt/statsd/config/udp.js
- Configure Graphite storage schema for 1-second resolution
- Set Grafana minimum dashboard refresh interval to 1 second
@fcostaoliveira
fcostaoliveira self-requested a review January 28, 2026 17:41
- Calculate p50, p99, p99.9 from HDR histograms
- Add get_inst_totals_histogram() to expose instantaneous histograms
- Add aggregate_inst_histogram() to aggregate histograms across threads
- Send percentiles as gauge metrics to StatsD every second
- Update Grafana dashboard to display new percentile metrics
@benoitdion

Copy link
Copy Markdown
Member

cursor review

Comment thread statsd.cpp Resolved
fcostaoliveira and others added 3 commits February 23, 2026 17:27
- Update statsd.cpp/h with reviewed improvements
- Expand REALTIME_METRICS.md documentation
- Refine docker-compose, grafana, and graphite configs
- Add tests/test_statsd.py for StatsD integration testing

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Bring upstream RedisLabs master changes into the StatsD feature branch.
Resolved conflicts in client.h, run_stats.h, run_stats.cpp, and
memtier_benchmark.cpp, preserving StatsD additions alongside upstream
improvements.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- Fix .clang-format to be compatible with clang-format v11
  (remove ReferenceAlignment, PackConstructorInitializers, replace
  Never/Empty enum values with boolean equivalents)
- Run make format across all C++ source files
- Add tests/.env/ to .gitignore

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread statsd.cpp Resolved
Comment thread memtier_benchmark.cpp Resolved
1. Grafana UID mismatch: add uid to datasource provisioning so
   dashboard panels resolve the hardcoded P1D261A8554D2DA69 UID.

2. Buffer overread in statsd_client::event(): guard send() with the
   same truncation check already used in send_metric().

3. Hardcoded Graphite port 80: default to 8080 (docker-compose host
   port) and expose --graphite-port option for in-Docker usage.

4. Unsynchronized histogram aggregation: protect
   inst_m_totals_latency_histogram with a pthread_mutex, locking
   during hdr_reset (roll_cur_stats) and during aggregation reads
   (copy_inst_histogram), preventing data races with worker threads.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread run_stats.cpp Resolved
fcostaoliveira and others added 2 commits February 23, 2026 18:11
The CI was using ubuntu-latest's default clang-format (v18) which
formatted differently from our local v11. Pin both CI and local to
clang-format v21 (via LLVM apt repo) and restore the v13+ .clang-format
keys that were previously removed for v11 compatibility.

Reformatted: client.cpp, item.cpp, obj_gen.cpp, shard_connection.cpp

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread statsd.cpp Resolved
Comment thread run_stats.h Resolved
Mutex copy UB (introduced by our fix redis#4):
  Replace bare pthread_mutex_t with reinit_mutex_t wrapper that defines
  correct copy/move semantics - copies always initialize a fresh mutex
  rather than duplicating state. This lets the compiler auto-generate
  correct copy constructor for run_stats with zero hot-path overhead.
  Removes the hand-written ~run_stats() destructor (wrapper handles it).

Unused methods (statsd_client):
  Remove counter(), histogram(), and get_run_label() - defined but never
  called anywhere in the codebase.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread memtier_benchmark.cpp Resolved
%.1f truncates 99.99 to 100.0 and causes name collisions for any
percentile with more than one decimal digit. Switch to %g which gives
the shortest exact representation with no trailing zeros, then replace
'.' with '_'. Simplifies the integer/float branch into a single path.

Examples: 50->latency_p50, 99.9->latency_p99_9, 99.99->latency_p99_99

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@filipecosta90

Copy link
Copy Markdown
Collaborator

cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Keeps all Graphite config (statsd-config.js, storage-schemas.conf)
together under graphite/ rather than cluttering the repo root.
Update docker-compose.statsd.yml mount path accordingly.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread storage-schemas.conf Resolved
fcostaoliveira and others added 3 commits February 24, 2026 12:27
New `test-statsd` job spins up the Graphite+StatsD stack via
docker-compose.statsd.yml, waits for Graphite to become ready, then runs
`test_statsd` with `STATSD_HOST=localhost` so the end-to-end Graphite
integration path is exercised on every PR.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator

cursor review

Comment thread statsd.cpp
m_run_label, data);
} else {
json_len = snprintf(json, sizeof(json) - 1, "{\"what\":\"%s\",\"tags\":\"run:%s\"}", what, m_run_label);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

JSON injection in event annotations

Medium Severity

The event() function constructs JSON payloads by directly interpolating the what, data, and tags parameters without escaping special characters. If these strings contain double quotes, backslashes, newlines, or other JSON control characters, the resulting JSON will be malformed, causing Graphite to reject the event annotation or potentially parse it incorrectly.

Fix in Cursor Fix in Web

Comment thread statsd.cpp Resolved
Comment thread statsd.cpp Resolved
Comment thread statsd.cpp Resolved
Comment thread run_stats.cpp Resolved
Comment thread memtier_benchmark.cpp Resolved
fcostaoliveira and others added 2 commits February 24, 2026 14:29
- Remove m_graphite_port=80 override in init() that broke --graphite-port CLI option
- Use hdr_record_value_capped_atomic() for inst_m_totals_latency_histogram
  writes from worker threads to eliminate data race with main thread reads
- Replace unconditional assertTrue(True) in test_statsd.py with real
  assertions (assertEqual, assertGreater) and add temp dir cleanup
- Sanitize --statsd-prefix input like run_label to prevent invalid
  Graphite metric paths
- Fix snprintf sizeof(buf)-1 pattern (snprintf already null-terminates)
- Fix docs: metrics interval is ~1s not ~200ms, add --graphite-port to
  CLI options table
- CI: fail on Graphite readiness timeout, trim unnecessary build deps

Co-Authored-By: Claude Opus 4.6 <[email protected]>
RLTest's assertion methods have signature (value, ..., depth=0, message=None).
Passing the message string as a positional arg was being interpreted as the
depth parameter, causing exceptions. Use message= keyword for all assertions.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator

cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread memtier_benchmark.cpp Resolved

@fcostaoliveira fcostaoliveira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @jeremyplichta and @DanEidelman! really cool feature

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.

5 participants