Skip to content

Commit

Permalink
fix: init metrics object with zero counts
Browse files Browse the repository at this point in the history
  • Loading branch information
anomit committed Nov 5, 2024
1 parent baf9d15 commit 2110378
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkgs/service/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,17 @@ func (s *server) getOrCreateEpochMetrics(epochID uint64) *epochMetrics {
s.currentEpoch.Store(epochID)

// Get or create metrics for this epoch
metrics, _ := s.metrics.LoadOrStore(epochID, &epochMetrics{})
metrics, loaded := s.metrics.LoadOrStore(epochID, &epochMetrics{
received: atomic.Uint64{},
succeeded: atomic.Uint64{},
})

// Initialize the counters to 0 only if this is a new metrics object
if !loaded {
em := metrics.(*epochMetrics)
em.received.Store(0)
em.succeeded.Store(0)
}

// Cleanup old epochs
s.metrics.Range(func(key, value interface{}) bool {
Expand All @@ -179,7 +189,7 @@ func (s *server) getOrCreateEpochMetrics(epochID uint64) *epochMetrics {
return true
})

return metrics.(*epochMetrics)
return em
}

func (s *server) GetMetrics() map[uint64]struct {
Expand Down

0 comments on commit 2110378

Please sign in to comment.