Skip to content

Conversation

@lowhung
Copy link
Owner

@lowhung lowhung commented Dec 24, 2025

Summary

This PR adds automatic rate computation (messages/second) to the SDK.

Changes

  • Track previous count and timestamp for each topic in ReadState and WriteState
  • Compute messages/second rate on each collect() call
  • Rate is None on first collection (no baseline available)
  • Requires at least 10ms between samples to avoid division issues

Testing

  • Added comprehensive tests for rate computation
  • Tests verify rate is None on first collection
  • Tests verify rate is computed correctly on subsequent collections
  • Tests verify edge cases (zero delta, short elapsed time)

Usage

let instrumentor = Instrumentor::new();
let handle = instrumentor.register("my-service");

// Record some messages
handle.record_read("events", 100);

// First collection - rate will be None
let snapshot1 = instrumentor.collect();

// Wait and record more
std::thread::sleep(Duration::from_secs(1));
handle.record_read("events", 100);

// Second collection - rate will be ~100 msg/s
let snapshot2 = instrumentor.collect();

- Track previous count and timestamp for each topic
- Compute messages/second rate on each collection
- Rate is None on first collection (no baseline)
- Requires at least 10ms between samples to avoid division issues
- Adds comprehensive tests for rate computation
@lowhung lowhung merged commit 67b68cb into main Dec 24, 2025
5 checks passed
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.

2 participants