From d33e11b52c61f007aac8f1c936f479060f4941db Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Wed, 10 Jan 2024 13:39:06 +0200 Subject: [PATCH] chore: unregister metrics on stop --- sync/metrics.go | 11 +++++++++-- sync/sync.go | 4 ++-- sync/sync_head.go | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/sync/metrics.go b/sync/metrics.go index 32b29f12..e6f01984 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -122,13 +122,13 @@ func (m *metrics) recordTotalSynced(totalSynced int) { }) } -func (m *metrics) recordSyncLoopStarted(ctx context.Context) { +func (m *metrics) syncingStarted(ctx context.Context) { m.observe(ctx, func(ctx context.Context) { m.syncLoopStarted.Add(ctx, 1) }) } -func (m *metrics) recordTrustedPeersOutOfSync(ctx context.Context) { +func (m *metrics) peersOutOufSync(ctx context.Context) { m.observe(ctx, func(ctx context.Context) { m.trustedPeersOutOfSync.Add(ctx, 1) }) @@ -159,3 +159,10 @@ func (m *metrics) observe(ctx context.Context, observeFn func(context.Context)) } observeFn(ctx) } + +func (m *metrics) Close() error { + if m == nil { + return nil + } + return m.syncReg.Unregister() +} diff --git a/sync/sync.go b/sync/sync.go index 46472c28..e41a304b 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -109,7 +109,7 @@ func (s *Syncer[H]) Start(ctx context.Context) error { // Stop stops Syncer. func (s *Syncer[H]) Stop(context.Context) error { s.cancel() - return nil + return s.metrics.Close() } // SyncWait blocks until ongoing sync is done. @@ -180,7 +180,7 @@ func (s *Syncer[H]) syncLoop() { for { select { case <-s.triggerSync: - s.metrics.recordSyncLoopStarted(s.ctx) + s.metrics.syncingStarted(s.ctx) s.sync(s.ctx) case <-s.ctx.Done(): return diff --git a/sync/sync_head.go b/sync/sync_head.go index 31a42691..02868543 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -102,7 +102,7 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, error) { log.Warnw("subjective initialization with an old header", "height", trustHead.Height()) } log.Warn("trusted peer is out of sync") - s.metrics.recordTrustedPeersOutOfSync(s.ctx) + s.metrics.peersOutOufSync(s.ctx) return trustHead, nil }