From 3d2e18bad59a2eabf9ef0b0d43e552d8bfe7654a Mon Sep 17 00:00:00 2001 From: Viacheslav Panasovets Date: Wed, 26 Oct 2022 00:26:12 +0200 Subject: [PATCH] Fix time.Since() in defer. Wrap in anonymous function (#11489) Function arguments in defer evaluated during definition of defer, not during execution Signed-off-by: Slavik Panasovets Signed-off-by: Slavik Panasovets --- discovery/refresh/refresh.go | 5 ++++- tsdb/db.go | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/discovery/refresh/refresh.go b/discovery/refresh/refresh.go index 043b2f7ba..919567a53 100644 --- a/discovery/refresh/refresh.go +++ b/discovery/refresh/refresh.go @@ -114,7 +114,10 @@ func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { now := time.Now() - defer d.duration.Observe(time.Since(now).Seconds()) + defer func() { + d.duration.Observe(time.Since(now).Seconds()) + }() + tgs, err := d.refreshf(ctx) if err != nil { d.failures.Inc() diff --git a/tsdb/db.go b/tsdb/db.go index 9adff0b9d..dbf8e5034 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -1939,7 +1939,9 @@ func (db *DB) CleanTombstones() (err error) { defer db.cmtx.Unlock() start := time.Now() - defer db.metrics.tombCleanTimer.Observe(time.Since(start).Seconds()) + defer func() { + db.metrics.tombCleanTimer.Observe(time.Since(start).Seconds()) + }() cleanUpCompleted := false // Repeat cleanup until there is no tombstones left.