Skip to content

Commit

Permalink
cache: Allow setting cache metrics prefix
Browse files Browse the repository at this point in the history
Custom prefix allows using multiple instances of the cache in an
application with unique metrics.

Signed-off-by: Sunny <[email protected]>
  • Loading branch information
darkowlzz committed Nov 7, 2024
1 parent 8451ce1 commit eb427c1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 3 additions & 1 deletion cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions cache/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions cache/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.",
},
),
Expand Down
2 changes: 1 addition & 1 deletion cache/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions cache/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}

0 comments on commit eb427c1

Please sign in to comment.