From 50deef00971530092ebcfe8f4bf26b8c76b7fc9e Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 28 Oct 2024 18:57:44 +0000 Subject: [PATCH] cache: Allow setting cache metrics prefix Custom prefix allows using multiple instances of the cache in an application with unique metrics. Signed-off-by: Sunny --- cache/cache.go | 2 +- cache/cache_test.go | 4 +++- cache/lru.go | 2 +- cache/lru_test.go | 9 ++++++--- cache/metrics.go | 12 +++++++----- cache/metrics_test.go | 2 +- cache/store.go | 13 +++++++++++-- 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 4f7c6ecf..e0cb132a 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -85,7 +85,7 @@ func New[T any](capacity int, opts ...Options) (*Cache[T], error) { } if opt.registerer != nil { - c.metrics = newCacheMetrics(opt.registerer) + c.metrics = newCacheMetrics(opt.metricsPrefix, opt.registerer) } C := &Cache[T]{cache: c} diff --git a/cache/cache_test.go b/cache/cache_test.go index e1474dfc..5d5d3c6c 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -149,6 +149,7 @@ func Test_Cache_Set(t *testing.T) { reg := prometheus.NewPedanticRegistry() cache, err := New[string](1, WithMetricsRegisterer(reg), + WithMetricsPrefix("gotk_"), WithCleanupInterval(10*time.Millisecond)) g.Expect(err).ToNot(HaveOccurred()) @@ -202,7 +203,7 @@ func Test_Cache_Set(t *testing.T) { func Test_Cache_Get(t *testing.T) { g := NewWithT(t) reg := prometheus.NewPedanticRegistry() - cache, err := New[string](5, WithMetricsRegisterer(reg)) + cache, err := New[string](5, WithMetricsRegisterer(reg), WithMetricsPrefix("gotk_")) g.Expect(err).ToNot(HaveOccurred()) // Reconciling object label values for cache event metric. @@ -265,6 +266,7 @@ func Test_Cache_Delete(t *testing.T) { reg := prometheus.NewPedanticRegistry() cache, err := New[string](5, WithMetricsRegisterer(reg), + WithMetricsPrefix("gotk_"), WithCleanupInterval(1*time.Millisecond)) g.Expect(err).ToNot(HaveOccurred()) diff --git a/cache/lru.go b/cache/lru.go index 96ddb727..0a209c18 100644 --- a/cache/lru.go +++ b/cache/lru.go @@ -94,7 +94,7 @@ func NewLRU[T any](capacity int, opts ...Options) (*LRU[T], error) { } if opt.registerer != nil { - lru.metrics = newCacheMetrics(opt.registerer) + lru.metrics = newCacheMetrics(opt.metricsPrefix, opt.registerer) } return lru, nil diff --git a/cache/lru_test.go b/cache/lru_test.go index f89fba2b..57b93c63 100644 --- a/cache/lru_test.go +++ b/cache/lru_test.go @@ -103,7 +103,8 @@ func Test_LRU_Set(t *testing.T) { g := NewWithT(t) reg := prometheus.NewPedanticRegistry() cache, err := NewLRU[string](1, - WithMetricsRegisterer(reg)) + WithMetricsRegisterer(reg), + WithMetricsPrefix("gotk_")) g.Expect(err).ToNot(HaveOccurred()) // Add an object representing an expiring token @@ -149,7 +150,8 @@ func Test_LRU_Get(t *testing.T) { g := NewWithT(t) reg := prometheus.NewPedanticRegistry() cache, err := NewLRU[string](5, - WithMetricsRegisterer(reg)) + WithMetricsRegisterer(reg), + WithMetricsPrefix("gotk_")) g.Expect(err).ToNot(HaveOccurred()) // Reconciling object label values for cache event metric. @@ -209,7 +211,8 @@ func Test_LRU_Delete(t *testing.T) { g := NewWithT(t) reg := prometheus.NewPedanticRegistry() cache, err := NewLRU[string](5, - WithMetricsRegisterer(reg)) + WithMetricsRegisterer(reg), + WithMetricsPrefix("gotk_")) g.Expect(err).ToNot(HaveOccurred()) // Add an object representing an expiring token diff --git a/cache/metrics.go b/cache/metrics.go index bf0cbed8..f4a6e3f8 100644 --- a/cache/metrics.go +++ b/cache/metrics.go @@ -17,6 +17,8 @@ limitations under the License. package cache import ( + "fmt" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -41,32 +43,32 @@ type cacheMetrics struct { } // newcacheMetrics returns a new cacheMetrics. -func newCacheMetrics(reg prometheus.Registerer) *cacheMetrics { +func newCacheMetrics(prefix string, reg prometheus.Registerer) *cacheMetrics { labels := []string{"event_type", "kind", "name", "namespace"} return &cacheMetrics{ cacheEventsCounter: promauto.With(reg).NewCounterVec( prometheus.CounterOpts{ - Name: "gotk_cache_events_total", + Name: fmt.Sprintf("%scache_events_total", prefix), Help: "Total number of cache retrieval events for a Gitops Toolkit resource reconciliation.", }, labels, ), cacheItemsGauge: promauto.With(reg).NewGauge( prometheus.GaugeOpts{ - Name: "gotk_cached_items", + Name: fmt.Sprintf("%scached_items", prefix), Help: "Total number of items in the cache.", }, ), cacheRequestsCounter: promauto.With(reg).NewCounterVec( prometheus.CounterOpts{ - Name: "gotk_cache_requests_total", + Name: fmt.Sprintf("%scache_requests_total", prefix), Help: "Total number of cache requests partioned by success or failure.", }, []string{"status"}, ), cacheEvictionCounter: promauto.With(reg).NewCounter( prometheus.CounterOpts{ - Name: "gotk_cache_evictions_total", + Name: fmt.Sprintf("%scache_evictions_total", prefix), Help: "Total number of cache evictions.", }, ), diff --git a/cache/metrics_test.go b/cache/metrics_test.go index c2291687..76a4a5ba 100644 --- a/cache/metrics_test.go +++ b/cache/metrics_test.go @@ -28,7 +28,7 @@ import ( func TestCacheMetrics(t *testing.T) { g := NewWithT(t) reg := prometheus.NewPedanticRegistry() - m := newCacheMetrics(reg) + m := newCacheMetrics("gotk_", reg) g.Expect(m).ToNot(BeNil()) // CounterVec is a collection of counters and is not exported until it has counters in it. diff --git a/cache/store.go b/cache/store.go index 464cba3e..d0dce133 100644 --- a/cache/store.go +++ b/cache/store.go @@ -44,8 +44,9 @@ type Expirable[T any] interface { } type storeOptions struct { - interval time.Duration - registerer prometheus.Registerer + interval time.Duration + registerer prometheus.Registerer + metricsPrefix string } // Options is a function that sets the store options. @@ -66,3 +67,11 @@ func WithMetricsRegisterer(r prometheus.Registerer) Options { return nil } } + +// WithMetricsPrefix sets the metrics prefix for the cache metrics. +func WithMetricsPrefix(prefix string) Options { + return func(o *storeOptions) error { + o.metricsPrefix = prefix + return nil + } +}