From 2110378ca7e77eb9c367c164b028930e3fbc8d50 Mon Sep 17 00:00:00 2001 From: anomit ghosh Date: Wed, 6 Nov 2024 01:48:09 +0530 Subject: [PATCH] fix: init metrics object with zero counts --- pkgs/service/msg_server.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkgs/service/msg_server.go b/pkgs/service/msg_server.go index 818307c..e843758 100644 --- a/pkgs/service/msg_server.go +++ b/pkgs/service/msg_server.go @@ -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 { @@ -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 {