Skip to content

Commit

Permalink
chore: unregister metrics on stop
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jan 10, 2024
1 parent 8749c91 commit d33e11b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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()
}
4 changes: 2 additions & 2 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit d33e11b

Please sign in to comment.