-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04720e4
commit a7a93f8
Showing
8 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"github.com/opentracing/opentracing-go" | ||
extensions "github.com/topfreegames/extensions/middleware" | ||
"github.com/topfreegames/podium/leaderboard/v2/enriching" | ||
"github.com/topfreegames/podium/leaderboard/v2/model" | ||
"time" | ||
) | ||
|
||
const ( | ||
enrichmentCacheGetTimingMilli = "enrichment_cache_get_duration_milliseconds" | ||
enrichmentCacheGets = "enrichment_cache_gets" | ||
enrichmentCacheHits = "enrichment_cache_gets" | ||
enrichmentCacheGetErrors = "enrichment_cache_get_errors" | ||
enrichmentCacheSetTimingMilli = "enrichment_cache_set_duration_milliseconds" | ||
enrichmentCacheSets = "enrichment_cache_sets" | ||
enrichmentCacheSetErrors = "enrichment_cache_set_errors" | ||
) | ||
|
||
type instrumentedCache struct { | ||
impl enriching.EnricherCache | ||
metricsReporter extensions.MetricsReporter | ||
} | ||
|
||
// NewInstrumentedCache returns an EnrichCache implementation wrapped | ||
// with metrics reporting and tracing | ||
func NewInstrumentedCache( | ||
impl enriching.EnricherCache, | ||
metricsReporter extensions.MetricsReporter, | ||
) enriching.EnricherCache { | ||
return &instrumentedCache{ | ||
impl: impl, | ||
metricsReporter: metricsReporter, | ||
} | ||
} | ||
|
||
func (c *instrumentedCache) Get( | ||
ctx context.Context, | ||
tenantID, | ||
leaderboardID string, | ||
members []*model.Member, | ||
) (map[string]map[string]string, bool, error) { | ||
start := time.Now() | ||
|
||
span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache", opentracing.Tags{ | ||
"tenant_id": tenantID, | ||
"leaderboard_id": leaderboardID, | ||
}) | ||
defer span.Finish() | ||
|
||
metadata, hit, err := c.impl.Get(ctx, tenantID, leaderboardID, members) | ||
|
||
c.metricsReporter.Increment(enrichmentCacheGets) | ||
c.metricsReporter.Timing(enrichmentCacheGetTimingMilli, time.Since(start)) | ||
|
||
if err != nil { | ||
span.SetTag("error", true) | ||
span.SetTag("error.message", err.Error()) | ||
|
||
c.metricsReporter.Increment(enrichmentCacheGetErrors) | ||
} | ||
|
||
if hit { | ||
c.metricsReporter.Increment(enrichmentCacheHits) | ||
} | ||
|
||
return metadata, hit, err | ||
} | ||
|
||
func (c *instrumentedCache) Set( | ||
ctx context.Context, | ||
tenantID, | ||
leaderboardID string, | ||
members []*model.Member, | ||
ttl time.Duration, | ||
) error { | ||
start := time.Now() | ||
|
||
span, ctx := opentracing.StartSpanFromContext(ctx, "podium.enriching_cache", opentracing.Tags{ | ||
"tenant_id": tenantID, | ||
"leaderboard_id": leaderboardID, | ||
"ttl": ttl, | ||
}) | ||
defer span.Finish() | ||
|
||
err := c.impl.Set(ctx, tenantID, leaderboardID, members, ttl) | ||
|
||
c.metricsReporter.Increment(enrichmentCacheSets) | ||
c.metricsReporter.Timing(enrichmentCacheSetTimingMilli, time.Since(start)) | ||
|
||
if err != nil { | ||
span.SetTag("error", true) | ||
span.SetTag("error.message", err.Error()) | ||
|
||
c.metricsReporter.Increment(enrichmentCacheSetErrors) | ||
} | ||
|
||
return err | ||
} | ||
|
||
var _ enriching.EnricherCache = &instrumentedCache{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
mock_extensions "github.com/topfreegames/extensions/middleware/mocks" | ||
mock_enriching "github.com/topfreegames/podium/leaderboard/v2/mocks" | ||
"github.com/topfreegames/podium/leaderboard/v2/model" | ||
) | ||
|
||
var _ = Describe("Instrumented enrich cache Get tests", func() { | ||
tenantID := "tenant-id" | ||
leaderboardID := "leaderboard-id" | ||
members := []*model.Member{ | ||
{ | ||
PublicID: "member1", | ||
}, | ||
} | ||
|
||
result := map[string]map[string]string{ | ||
"member1": { | ||
"field1": "value1", | ||
}, | ||
} | ||
|
||
It("should send metrics when Get is called successfully with hit", func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
impl := mock_enriching.NewMockEnricherCache(ctrl) | ||
metricsReporter := mock_extensions.NewMockMetricsReporter(ctrl) | ||
|
||
impl.EXPECT().Get(gomock.Any(), tenantID, leaderboardID, members).Return(result, true, nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheGets).Return(nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheHits).Return(nil) | ||
metricsReporter.EXPECT().Timing(enrichmentCacheGetTimingMilli, gomock.Any()).Return(nil) | ||
|
||
instrumentedCache := NewInstrumentedCache(impl, metricsReporter) | ||
res, hit, err := instrumentedCache.Get(context.Background(), tenantID, leaderboardID, members) | ||
|
||
Expect(res).To(Equal(result)) | ||
Expect(hit).To(BeTrue()) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("should send metrics when Get is called successfully with miss", func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
impl := mock_enriching.NewMockEnricherCache(ctrl) | ||
metricsReporter := mock_extensions.NewMockMetricsReporter(ctrl) | ||
|
||
impl.EXPECT().Get(gomock.Any(), tenantID, leaderboardID, members).Return(nil, false, nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheGets).Return(nil) | ||
metricsReporter.EXPECT().Timing(enrichmentCacheGetTimingMilli, gomock.Any()).Return(nil) | ||
|
||
instrumentedCache := NewInstrumentedCache(impl, metricsReporter) | ||
res, hit, err := instrumentedCache.Get(context.Background(), tenantID, leaderboardID, members) | ||
Expect(res).To(BeNil()) | ||
Expect(hit).To(BeFalse()) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("should send metrics when Get is called with error", func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
impl := mock_enriching.NewMockEnricherCache(ctrl) | ||
metricsReporter := mock_extensions.NewMockMetricsReporter(ctrl) | ||
|
||
impl.EXPECT().Get(gomock.Any(), tenantID, leaderboardID, members).Return(nil, false, errors.New("error")) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheGets).Return(nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheGetErrors).Return(nil) | ||
metricsReporter.EXPECT().Timing(enrichmentCacheGetTimingMilli, gomock.Any()).Return(nil) | ||
|
||
instrumentedCache := NewInstrumentedCache(impl, metricsReporter) | ||
res, hit, err := instrumentedCache.Get(context.Background(), tenantID, leaderboardID, members) | ||
|
||
Expect(res).To(BeNil()) | ||
Expect(hit).To(BeFalse()) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("Instrumented enrich cache Set tests", func() { | ||
tenantID := "tenant-id" | ||
leaderboardID := "leaderboard-id" | ||
members := []*model.Member{ | ||
{ | ||
PublicID: "member1", | ||
Metadata: map[string]string{ | ||
"field1": "value1", | ||
}, | ||
}, | ||
} | ||
|
||
It("should send metrics when Set is called successfully", func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
impl := mock_enriching.NewMockEnricherCache(ctrl) | ||
metricsReporter := mock_extensions.NewMockMetricsReporter(ctrl) | ||
|
||
impl.EXPECT().Set(gomock.Any(), tenantID, leaderboardID, members, gomock.Any()).Return(nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheSets).Return(nil) | ||
metricsReporter.EXPECT().Timing(enrichmentCacheSetTimingMilli, gomock.Any()).Return(nil) | ||
|
||
instrumentedCache := NewInstrumentedCache(impl, metricsReporter) | ||
err := instrumentedCache.Set(context.Background(), tenantID, leaderboardID, members, 0) | ||
|
||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("should send metrics when Set is called with error", func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
impl := mock_enriching.NewMockEnricherCache(ctrl) | ||
metricsReporter := mock_extensions.NewMockMetricsReporter(ctrl) | ||
|
||
impl.EXPECT().Set(gomock.Any(), tenantID, leaderboardID, members, gomock.Any()).Return(errors.New("error")) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheSets).Return(nil) | ||
metricsReporter.EXPECT().Increment(enrichmentCacheSetErrors).Return(nil) | ||
metricsReporter.EXPECT().Timing(enrichmentCacheSetTimingMilli, gomock.Any()).Return(nil) | ||
|
||
instrumentedCache := NewInstrumentedCache(impl, metricsReporter) | ||
err := instrumentedCache.Set(context.Background(), tenantID, leaderboardID, members, 0) | ||
|
||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) |
2 changes: 1 addition & 1 deletion
2
leaderboard/enriching/cloud_save_entities.go → leaderboard/enriching/cloud-save/entities.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.