Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize metrics on MetricCache creation #223

Merged
merged 1 commit into from Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/cache/metric.go
Expand Up @@ -20,10 +20,14 @@ type MetricCache[T any] struct {

// NewMetric creates a new cache with metrics and a given cache storage
func NewMetric[T any](metrics metrics.MetricsInterface, cache CacheInterface[T]) *MetricCache[T] {
return &MetricCache[T]{
metricCache := &MetricCache[T]{
metrics: metrics,
cache: cache,
}

metricCache.updateMetrics(cache)

return metricCache
}

// Get obtains a value from cache and also records metrics
Expand Down
31 changes: 29 additions & 2 deletions lib/cache/metric_test.go
Expand Up @@ -17,8 +17,11 @@ func TestNewMetric(t *testing.T) {
// Given
ctrl := gomock.NewController(t)

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)
metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

// When
cache := NewMetric[any](metrics, cache1)
Expand All @@ -45,10 +48,10 @@ func TestMetricGet(t *testing.T) {
codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Get(ctx, "my-key").Return(cacheValue, nil)
cache1.EXPECT().GetCodec().Return(codec1)
cache1.EXPECT().GetCodec().Return(codec1).MinTimes(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).AnyTimes()
metrics.EXPECT().RecordFromCodec(codec1).MinTimes(1)

cache := NewMetric[any](metrics, cache1)

Expand Down Expand Up @@ -110,10 +113,13 @@ func TestMetricSet(t *testing.T) {
Hello: "world",
}

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Set(ctx, "my-key", value).Return(nil)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -130,10 +136,13 @@ func TestMetricDelete(t *testing.T) {

ctx := context.Background()

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Delete(ctx, "my-key").Return(nil)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -152,10 +161,13 @@ func TestMetricDeleteWhenError(t *testing.T) {

expectedErr := errors.New("unable to delete key")

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Delete(ctx, "my-key").Return(expectedErr)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -172,10 +184,13 @@ func TestMetricInvalidate(t *testing.T) {

ctx := context.Background()

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Invalidate(ctx).Return(nil)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -194,10 +209,13 @@ func TestMetricInvalidateWhenError(t *testing.T) {

expectedErr := errors.New("unexpected error while invalidating data")

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Invalidate(ctx).Return(expectedErr)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -214,10 +232,13 @@ func TestMetricClear(t *testing.T) {

ctx := context.Background()

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Clear(ctx).Return(nil)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -236,10 +257,13 @@ func TestMetricClearWhenError(t *testing.T) {

expectedErr := errors.New("unexpected error while clearing cache")

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().Clear(ctx).Return(expectedErr)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)

metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand All @@ -254,8 +278,11 @@ func TestMetricGetType(t *testing.T) {
// Given
ctrl := gomock.NewController(t)

codec1 := codec.NewMockCodecInterface(ctrl)
cache1 := NewMockSetterCacheInterface[any](ctrl)
cache1.EXPECT().GetCodec().Return(codec1).Times(1)
metrics := metrics.NewMockMetricsInterface(ctrl)
metrics.EXPECT().RecordFromCodec(codec1).Times(1)

cache := NewMetric[any](metrics, cache1)

Expand Down