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 <slavik@google.com>

Signed-off-by: Slavik Panasovets <slavik@google.com>
This commit is contained in:
Viacheslav Panasovets 2022-10-26 00:26:12 +02:00 committed by GitHub
parent 51a44e6657
commit 3d2e18bad5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -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()

View file

@ -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.