From a72e25accfb4b58a6514fe4c34a4d6beaa344b26 Mon Sep 17 00:00:00 2001 From: Ahrav Dutta Date: Fri, 8 Nov 2024 14:32:16 -0800 Subject: [PATCH] make collector a singleton --- pkg/sources/s3/metrics.go | 7 +++++++ pkg/sources/s3/s3.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/sources/s3/metrics.go b/pkg/sources/s3/metrics.go index 6ad8c7fce..9463a9a4d 100644 --- a/pkg/sources/s3/metrics.go +++ b/pkg/sources/s3/metrics.go @@ -29,6 +29,13 @@ type collector struct { bucketsPerRole *prometheus.GaugeVec } +var metricsInstance metricsCollector + +func init() { metricsInstance = newS3MetricsCollector() } + +// getMetricsCollector returns the singleton metrics collector instance.. +func getMetricsCollector() metricsCollector { return metricsInstance } + func newS3MetricsCollector() metricsCollector { return &collector{ objectsScanned: promauto.NewCounterVec(prometheus.CounterOpts{ diff --git a/pkg/sources/s3/s3.go b/pkg/sources/s3/s3.go index 151f66283..3d5beba32 100644 --- a/pkg/sources/s3/s3.go +++ b/pkg/sources/s3/s3.go @@ -99,7 +99,7 @@ func (s *Source) Init( if err != nil { return err } - s.metricsCollector = newS3MetricsCollector() + s.metricsCollector = getMetricsCollector() s.setMaxObjectSize(conn.GetMaxObjectSize())