From 95bd099c77da124c5af2805a048b4fb94430ed40 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 9 Mar 2026 16:51:46 +0200 Subject: [PATCH] fix(coderd/agentapi/metadatabatcher): use clock.Since instead of time.Since in flush (#22841) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `flush` method sets `start := b.clock.Now()` but later computes duration with `time.Since(start)` instead of `b.clock.Since(start)` for the `FlushDuration` metric and the debug log. Line 352 already uses `b.clock.Since(start)` correctly — this makes the rest consistent. Test output before fix: ``` flush complete count=100 elapsed=19166h12m30.265728663s reason=scheduled ``` After fix: ``` flush complete count=100 elapsed=0s reason=scheduled ``` --- coderd/agentapi/metadatabatcher/metadata_batcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/agentapi/metadatabatcher/metadata_batcher.go b/coderd/agentapi/metadatabatcher/metadata_batcher.go index c5322d5976..25b09d2dcd 100644 --- a/coderd/agentapi/metadatabatcher/metadata_batcher.go +++ b/coderd/agentapi/metadatabatcher/metadata_batcher.go @@ -387,9 +387,9 @@ func (b *Batcher) flush(ctx context.Context, reason string) { b.Metrics.BatchSize.Observe(float64(count)) b.Metrics.MetadataTotal.Add(float64(count)) b.Metrics.BatchesTotal.WithLabelValues(reason).Inc() - b.Metrics.FlushDuration.WithLabelValues(reason).Observe(time.Since(start).Seconds()) + elapsed = b.clock.Since(start) + b.Metrics.FlushDuration.WithLabelValues(reason).Observe(elapsed.Seconds()) - elapsed = time.Since(start) b.log.Debug(ctx, "flush complete", slog.F("count", count), slog.F("elapsed", elapsed),