Added debug assertions for negative values in counter and histograms#2757
Conversation
|
|
||
| impl AddAssign<f64> for Counter<f64> { | ||
| fn add_assign(&mut self, rhs: f64) { | ||
| debug_assert!(rhs >= 0.0, "Counter += called with negative value: {rhs}"); |
There was a problem hiding this comment.
if this is stripped off by compiler in release build, this is good. I don't know if any better solution than this.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2757 +/- ##
==========================================
- Coverage 88.06% 88.06% -0.01%
==========================================
Files 644 644
Lines 246791 246796 +5
==========================================
- Hits 217340 217332 -8
- Misses 28927 28940 +13
Partials 524 524
🚀 New features to boost your workflow:
|
|
Nice cleanup. One small consistency question: should |
Good catch! Since this PR is already in the merge queue, I'll address this in a follow-up PR covering Counter::new() and From. |
bc4cec8
Change Summary
Add
debug_assert!checks to enforce non-negative values inCounter<f64>andMmsc(Histogram bridge) instruments inotap_df_telemetry. Counters and Histogram-based instruments must only receive non-negative deltas for correctness. Their sums are exported as Prometheus counters, which require monotonicity.Three guards added:
Counter<f64>::add(v)— assertsv >= 0.0AddAssign<f64> for Counter<f64>— assertsrhs >= 0.0Mmsc::record(value)— assertsvalue >= 0.0These use
debug_assert!(zero cost in release builds) per the issue discussion.What issue does this PR close?
#2100
How are these changes tested?
test_mmsc_negative_valuestest (which validated now-invalid behavior) with three#[cfg(debug_assertions)] #[should_panic]tests that verify the assertions fire on negative input:test_mmsc_record_rejects_negativetest_counter_f64_add_rejects_negativetest_counter_f64_add_assign_rejects_negativeotap-df-telemetrycontinue to pass.Are there any user-facing changes?
No.
debug_assert!is stripped in release builds. In debug builds, passing a negative value toCounter<f64>::add(),Counter<f64> +=, orMmsc::record()will now panic with a descriptive message, catching incorrect usage during development.