From 675eec91f34bd689191b6eeb9a5bf4dbd466237e Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 30 Jan 2024 11:37:06 +0300 Subject: [PATCH] [#938] shard: Update only changed counters If metric value hasn't changed, but we update metric, then non existed metric will apear with zero value. Signed-off-by: Dmitrii Stepanov --- pkg/local_object_storage/shard/shard.go | 46 +++++++++++++++++-------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/pkg/local_object_storage/shard/shard.go b/pkg/local_object_storage/shard/shard.go index 136f5463a..e8f1978d2 100644 --- a/pkg/local_object_storage/shard/shard.go +++ b/pkg/local_object_storage/shard/shard.go @@ -433,9 +433,9 @@ func (s *Shard) updateMetrics(ctx context.Context) { return } - s.cfg.metricsWriter.SetObjectCounter(physical, cc.Phy) - s.cfg.metricsWriter.SetObjectCounter(logical, cc.Logic) - s.cfg.metricsWriter.SetObjectCounter(user, cc.User) + s.setObjectCounterBy(physical, cc.Phy) + s.setObjectCounterBy(logical, cc.Logic) + s.setObjectCounterBy(user, cc.User) cnrList, err := s.metaBase.Containers(ctx) if err != nil { @@ -453,11 +453,11 @@ func (s *Shard) updateMetrics(ctx context.Context) { zap.Error(err)) continue } - s.metricsWriter.AddToContainerSize(cnrList[i].EncodeToString(), int64(size)) + s.addToContainerSize(cnrList[i].EncodeToString(), int64(size)) totalPayload += size } - s.metricsWriter.AddToPayloadSize(int64(totalPayload)) + s.addToPayloadSize(int64(totalPayload)) contCount, err := s.metaBase.ContainerCounters(ctx) if err != nil { @@ -465,9 +465,9 @@ func (s *Shard) updateMetrics(ctx context.Context) { return } for contID, count := range contCount.Counts { - s.metricsWriter.SetContainerObjectsCount(contID.EncodeToString(), physical, count.Phy) - s.metricsWriter.SetContainerObjectsCount(contID.EncodeToString(), logical, count.Logic) - s.metricsWriter.SetContainerObjectsCount(contID.EncodeToString(), user, count.User) + s.setContainerObjectsCount(contID.EncodeToString(), physical, count.Phy) + s.setContainerObjectsCount(contID.EncodeToString(), logical, count.Logic) + s.setContainerObjectsCount(contID.EncodeToString(), user, count.User) } } @@ -487,35 +487,53 @@ func (s *Shard) incObjectCounter(cnrID cid.ID, isUser bool) { } func (s *Shard) decObjectCounterBy(typ string, v uint64) { - if s.cfg.metricsWriter != nil { + if s.cfg.metricsWriter != nil && v > 0 { s.cfg.metricsWriter.AddToObjectCounter(typ, -int(v)) } } +func (s *Shard) setObjectCounterBy(typ string, v uint64) { + if s.cfg.metricsWriter != nil && v > 0 { + s.cfg.metricsWriter.SetObjectCounter(typ, v) + } +} + func (s *Shard) decContainerObjectCounter(byCnr map[cid.ID]meta.ObjectCounters) { if s.cfg.metricsWriter == nil { return } for cnrID, count := range byCnr { - s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), physical, count.Phy) - s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), logical, count.Logic) - s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), user, count.User) + if count.Phy > 0 { + s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), physical, count.Phy) + } + if count.Logic > 0 { + s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), logical, count.Logic) + } + if count.User > 0 { + s.cfg.metricsWriter.SubContainerObjectsCount(cnrID.EncodeToString(), user, count.User) + } } } func (s *Shard) addToContainerSize(cnr string, size int64) { - if s.cfg.metricsWriter != nil { + if s.cfg.metricsWriter != nil && size != 0 { s.cfg.metricsWriter.AddToContainerSize(cnr, size) } } func (s *Shard) addToPayloadSize(size int64) { - if s.cfg.metricsWriter != nil { + if s.cfg.metricsWriter != nil && size != 0 { s.cfg.metricsWriter.AddToPayloadSize(size) } } +func (s *Shard) setContainerObjectsCount(cnr string, typ string, v uint64) { + if s.cfg.metricsWriter != nil && v > 0 { + s.metricsWriter.SetContainerObjectsCount(cnr, typ, v) + } +} + func (s *Shard) IncErrorCounter() { if s.cfg.metricsWriter != nil { s.cfg.metricsWriter.IncErrorCounter() -- 2.45.2