mirror of
https://github.com/lukaszraczylo/filepuff-mcp.git
synced 2026-10-21 06:07:12 +00:00
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:
@@ -33,8 +33,13 @@ func (c *Counter) Inc() {
|
||||
c.value.Add(1)
|
||||
}
|
||||
|
||||
// Add adds the given value to the counter.
|
||||
// Add adds the given value to the counter. Counters are monotonic, so a
|
||||
// negative delta (which would corrupt rate()-style calculations downstream) is
|
||||
// ignored rather than applied.
|
||||
func (c *Counter) Add(delta int64) {
|
||||
if delta < 0 {
|
||||
return
|
||||
}
|
||||
c.value.Add(delta)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user