fix(metrics): ignore negative Counter.Add to keep counters monotonic

Counter.Add applied any delta, so a negative value would decrement a counter that must be monotonic, corrupting rate()-style downstream math. Ignore delta<0. Adds TestCounterRejectsNegativeAdd.
This commit is contained in:
2026-06-20 00:33:32 +01:00
parent debe79e33e
commit 61b04b46cd
2 changed files with 16 additions and 1 deletions
+10
View File
@@ -71,6 +71,16 @@ func TestCounter(t *testing.T) {
}
}
// Counters are monotonic: a negative Add must be ignored, not applied.
func TestCounterRejectsNegativeAdd(t *testing.T) {
c := NewCounter("mono_counter", "test", nil)
c.Add(5)
c.Add(-3)
if got := c.Value(); got != 5 {
t.Errorf("negative Add should be ignored: Value() = %d, want 5", got)
}
}
func TestCounterConcurrency(t *testing.T) {
c := NewCounter("concurrent_counter", "test", nil)
var wg sync.WaitGroup