diff options
author | Jakob Borg <jakob@kastelo.net> | 2023-09-20 09:04:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-20 09:04:47 +0200 |
commit | 051cbdc71339ddf9c49b58a32e172718cf76a67b (patch) | |
tree | c337eeebacce93f916d2feae4d847858c7e415c3 | |
parent | 58d1f3a4718ef8f86bfae1b9ed61dda8d7a26a59 (diff) |
lib/fs, lib/model: Be careful about potentially negative durations (fixes #9112) (#9113)v1.25.0-rc.2
I don't really understand under what circumstances, but sometimes these
calls panic with a "panic: counter cannot decrease in value" because the
value passed to Add() was negative.
-rw-r--r-- | lib/fs/metrics.go | 4 | ||||
-rw-r--r-- | lib/model/util.go | 8 |
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/fs/metrics.go b/lib/fs/metrics.go index ffde777215..a97660ce09 100644 --- a/lib/fs/metrics.go +++ b/lib/fs/metrics.go @@ -92,7 +92,9 @@ func (m *metricsFS) account(op string) func(bytes int) { t0 := time.Now() root := m.next.URI() return func(bytes int) { - metricTotalOperationSeconds.WithLabelValues(root, op).Add(time.Since(t0).Seconds()) + if dur := time.Since(t0).Seconds(); dur > 0 { + metricTotalOperationSeconds.WithLabelValues(root, op).Add(dur) + } metricTotalOperationsCount.WithLabelValues(root, op).Inc() if bytes >= 0 { metricTotalBytesCount.WithLabelValues(root, op).Add(float64(bytes)) diff --git a/lib/model/util.go b/lib/model/util.go index 2364c5e80c..54d33c5808 100644 --- a/lib/model/util.go +++ b/lib/model/util.go @@ -158,7 +158,9 @@ func inWritableDir(fn func(string) error, targetFs fs.Filesystem, path string, i func addTimeUntilCancelled(ctx context.Context, counter prometheus.Counter) { t0 := time.Now() defer func() { - counter.Add(time.Since(t0).Seconds()) + if dur := time.Since(t0).Seconds(); dur > 0 { + counter.Add(dur) + } }() ticker := time.NewTicker(time.Second) @@ -167,7 +169,9 @@ func addTimeUntilCancelled(ctx context.Context, counter prometheus.Counter) { for { select { case t := <-ticker.C: - counter.Add(t.Sub(t0).Seconds()) + if dur := t.Sub(t0).Seconds(); dur > 0 { + counter.Add(dur) + } t0 = t case <-ctx.Done(): return |