Skip to content

Commit

Permalink
Enable cloud save and enrich if config provided
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Sep 26, 2023
1 parent 04720e4 commit 1c49cea
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 50 deletions.
36 changes: 11 additions & 25 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,43 +219,29 @@ func (app *App) loadConfiguration() error {
}

func (app *App) configureEnrichment() {
// If a specific Redis instance is not provided, use the same one as the rest of the service.
if app.ParsedConfig.Enrichment.Cache.Addr == "" {
host := app.Config.GetString("redis.host")
port := app.Config.GetInt("redis.port")
password := app.Config.GetString("redis.password")

app.ParsedConfig.Enrichment.Cache = config.Cache{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: password,
}
}

redisClient := redis.NewClient(&redis.Options{
Addr: app.ParsedConfig.Enrichment.Cache.Addr,
Password: app.ParsedConfig.Enrichment.Cache.Password,
})

enrichCache := enrichercache.NewEnricherRedisCache(redisClient)
enricher := enriching.NewEnricher(
enriching.WithLogger(app.Logger),
enriching.WithWebhookUrls(app.ParsedConfig.Enrichment.WebhookUrls),
enriching.WithWebhookTimeout(app.ParsedConfig.Enrichment.WebhookTimeout),
enriching.WithCloudSaveUrl(app.ParsedConfig.Enrichment.CloudSave.Url),
enriching.WithCloudSaveDisabled(app.ParsedConfig.Enrichment.CloudSave.Disabled),
enriching.WithCloudSaveEnabled(app.ParsedConfig.Enrichment.CloudSave.Enabled),
)
wrapped := enriching.NewInstrumentedEnricher(enricher, app.DDStatsD)
app.Enricher = enriching.NewInstrumentedEnricher(enricher, app.DDStatsD)

if !app.ParsedConfig.Enrichment.Cache.Disabled {
wrapped = enrichercache.NewCachedEnricher(
if app.ParsedConfig.Enrichment.Cache.Addr != "" {
redisClient := redis.NewClient(&redis.Options{
Addr: app.ParsedConfig.Enrichment.Cache.Addr,
Password: app.ParsedConfig.Enrichment.Cache.Password,
})

enrichCache := enrichercache.NewEnricherRedisCache(redisClient)
app.Enricher = enrichercache.NewCachedEnricher(
enrichCache,
enricher,
app.Enricher,
enrichercache.WithLogger(app.Logger),
enrichercache.WithTTL(app.ParsedConfig.Enrichment.Cache.TTL),
)
}

app.Enricher = wrapped
}

// OnErrorHandler handles panics
Expand Down
7 changes: 2 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ type (
}

Cache struct {
// Disabled indicates whether the cache should be used.
Disabled bool `mapstructure:"disabled"`

// Add is the address for the cache.
Addr string `mapstructure:"addr"`

Expand All @@ -61,8 +58,8 @@ type (
}

CloudSaveConfig struct {
// Enabled indicates whether the Cloud Save service should be used for enrichment.
Disabled map[string]bool `mapstructure:"disabled"`
// Enabled indicates whether the Cloud Save service should be used for enrichment for each tenant.
Enabled map[string]bool `mapstructure:"disabled"`

// URL is the URL to call the Cloud Save service.
Url string `mapstructure:"url"`
Expand Down
3 changes: 1 addition & 2 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ extensions:
enrichment:
webhook_urls:
cache:
disabled: false
ttl: 24h
addrs: ""
username: ""
webhook_timeout: 500ms
cloud_save:
url:
disabled:
enabled:
3 changes: 1 addition & 2 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ enrichment:
dummy_tenant_id: "localhost:8080/"
webhook_timeout: 500ms
cache:
disabled: false
ttl: 24h
addrs: "localhost:6739"
username: ""
cloud_save:
url: "localhost:8888/"
disabled:
enabled:
10 changes: 5 additions & 5 deletions leaderboard/enriching/enrich_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (

cloudSaveConfig struct {
// Enabled indicates whether the Cloud Save service should be used for enrichment.
disabled map[string]bool
enabled map[string]bool

// URL is the URL to call the Cloud Save service.
url string
Expand All @@ -30,7 +30,7 @@ type (
func newDefaultEnrichConfig() enrichmentConfig {
return enrichmentConfig{
cloudSave: cloudSaveConfig{
disabled: map[string]bool{},
enabled: map[string]bool{},
},
webhookUrls: map[string]string{},
webhookTimeout: 500 * time.Millisecond,
Expand Down Expand Up @@ -60,10 +60,10 @@ func WithWebhookTimeout(timeout time.Duration) EnricherOptions {
}
}

// WithCloudSaveDisabled sets the map of disabled Cloud Save for each tenantID.
func WithCloudSaveDisabled(disabled map[string]bool) EnricherOptions {
// WithCloudSaveEnabled sets the map of enabled Cloud Save for each tenantID.
func WithCloudSaveEnabled(enabled map[string]bool) EnricherOptions {
return func(impl *enricherImpl) {
impl.config.cloudSave.disabled = disabled
impl.config.cloudSave.enabled = enabled
}
}

Expand Down
18 changes: 11 additions & 7 deletions leaderboard/enriching/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewEnricher(
}

// Enrich enriches the members list with some metadata.
// By default, it will call the Cloud Save service, unless it's disabled for the tenantID or if there's a webhook for the tenantID.
// By default, it will call the Cloud Save service, unless it's enabled for the tenantID or if there's a webhook for the tenantID.
// If there's a webhook configured for the tenantID, it will be called instead.
func (e *enricherImpl) Enrich(
ctx context.Context,
Expand All @@ -63,9 +63,9 @@ func (e *enricherImpl) Enrich(
)

tenantUrl, webHookExists := e.config.webhookUrls[tenantID]
cloudSaveDisabled := e.config.cloudSave.disabled[tenantID]
cloudSaveEnabled := e.config.cloudSave.enabled[tenantID]

if !webHookExists && cloudSaveDisabled {
if !webHookExists && !cloudSaveEnabled {
return members, nil
}

Expand All @@ -78,7 +78,9 @@ func (e *enricherImpl) Enrich(
}

return members, nil
} else if !cloudSaveDisabled {
}

if cloudSaveEnabled {
e.logger.Debug(fmt.Sprintf("no webhook configured for tentantID '%s'. will call Cloud Save.", tenantID))
members, err := e.enrichWithCloudSave(ctx, tenantID, members)

Expand All @@ -90,7 +92,7 @@ func (e *enricherImpl) Enrich(
return members, nil
}

l.Debug(fmt.Sprintf("no webhook configured for tentantID '%s' and cloud save disabled. Skipping enrichment.", tenantID))
l.Debug(fmt.Sprintf("no webhook configured for tentantID '%s' and cloud save enabled. Skipping enrichment.", tenantID))

return members, nil
}
Expand Down Expand Up @@ -162,8 +164,8 @@ func (e *enricherImpl) enrichWithCloudSave(ctx context.Context, tenantID string,
zap.String("tenantID", tenantID),
)

if e.config.cloudSave.disabled[tenantID] {
e.logger.Debug(fmt.Sprintf("cloud save enrich disabled for tenant %s. Skipping enrichment.", tenantID))
if e.config.cloudSave.enabled[tenantID] {
e.logger.Debug(fmt.Sprintf("cloud save enrich enabled for tenant %s. Skipping enrichment.", tenantID))
return members, nil
}

Expand Down Expand Up @@ -194,6 +196,8 @@ func (e *enricherImpl) enrichWithCloudSave(ctx context.Context, tenantID string,
return nil, fmt.Errorf("could not build cloud save url: %w", errors.Join(ErrEnrichmentInternal, err))
}

l.Debug(fmt.Sprintf("calling cloud save endpoint '%s'", url))

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("could not create request: %w", errors.Join(ErrEnrichmentInternal, err))
Expand Down
15 changes: 11 additions & 4 deletions leaderboard/enriching/enricher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ var _ = Describe("Enricher tests", func() {
leaderboardID := "leaderboardID"
tenantID := "tenantID"

It("should not enrich if no webhook url is configured and cloud save service is disabled", func() {
It("should not enrich if no webhook url is configured and cloud save service is not enabled", func() {
cache := mock_enriching.NewMockEnricherCache(gomock.NewController(GinkgoT()))

enrich := NewEnricher(
WithCloudSaveDisabled(map[string]bool{tenantID: true}),
)
enrich := NewEnricher()

members := []*model.Member{
{
Expand Down Expand Up @@ -62,6 +60,9 @@ var _ = Describe("Enricher tests", func() {

enrich := NewEnricher(
WithCloudSaveUrl(server.URL),
WithCloudSaveEnabled(map[string]bool{
tenantID: true,
}),
)

members := []*model.Member{
Expand All @@ -87,6 +88,9 @@ var _ = Describe("Enricher tests", func() {

enrich := NewEnricher(
WithCloudSaveUrl(server.URL),
WithCloudSaveEnabled(map[string]bool{
tenantID: true,
}),
)

members := []*model.Member{
Expand All @@ -112,6 +116,9 @@ var _ = Describe("Enricher tests", func() {

enrich := NewEnricher(
WithCloudSaveUrl(server.URL),
WithCloudSaveEnabled(map[string]bool{
tenantID: true,
}),
)

members := []*model.Member{
Expand Down

0 comments on commit 1c49cea

Please sign in to comment.