Skip to content

Commit

Permalink
adds enrichment config for viper; adds logger (#88)
Browse files Browse the repository at this point in the history
* add enrichment config and logger to enrichment
  • Loading branch information
gussf committed Jul 28, 2023
1 parent 1c81e1a commit da754bc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
13 changes: 7 additions & 6 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ package api
import (
"context"
"fmt"
"github.com/topfreegames/podium/config"
"github.com/topfreegames/podium/leaderboard/v2/enriching"
"google.golang.org/grpc/credentials/insecure"
"net"
"net/http"
"os"
Expand All @@ -24,6 +21,10 @@ import (
"syscall"
"time"

"github.com/topfreegames/podium/config"
"github.com/topfreegames/podium/leaderboard/v2/enriching"
"google.golang.org/grpc/credentials/insecure"

"google.golang.org/protobuf/encoding/protojson"

"github.com/getsentry/raven-go"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (app *App) configure() error {

app.configureJaeger()

app.configureEnrichment()
app.configureEnrichment(app.Logger)

err = app.configureStatsD()
if err != nil {
Expand Down Expand Up @@ -215,8 +216,8 @@ func (app *App) loadConfiguration() error {
return nil
}

func (app *App) configureEnrichment() {
app.Enricher = enriching.NewEnricher(app.ParsedConfig.Enrichment)
func (app *App) configureEnrichment(logger *zap.Logger) {
app.Enricher = enriching.NewEnricher(app.ParsedConfig.Enrichment, app.Logger)
}

// OnErrorHandler handles panics
Expand Down
3 changes: 2 additions & 1 deletion api/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ package api
import (
"context"
"fmt"
"google.golang.org/grpc/metadata"
"math"
"strings"

"google.golang.org/grpc/metadata"

lmodel "github.com/topfreegames/podium/leaderboard/v2/model"
"github.com/topfreegames/podium/leaderboard/v2/service"
"go.uber.org/zap"
Expand Down
3 changes: 3 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ extensions:
prefix: podium.
tags_prefix: ""
rate: 1

enrichment:
webhook_urls:
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/getsentry/raven-go v0.0.0-20170918144728-1452f6376ddb
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gosuri/uiprogress v0.0.0-20160202012259-a9f819bfc744
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
Expand Down Expand Up @@ -44,7 +45,6 @@ require (
github.com/go-pg/pg v8.0.7+incompatible // indirect
github.com/go-redis/redis v6.13.2+incompatible // indirect
github.com/go-redis/redis/v8 v8.8.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f // indirect
github.com/gorilla/mux v1.6.1 // indirect
github.com/gosuri/uilive v0.0.0-20160202011846-efb88ccd0599 // indirect
Expand Down Expand Up @@ -77,7 +77,6 @@ require (
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230724170836-66ad5b6ff146 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
Expand Down Expand Up @@ -363,8 +362,6 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
Expand Down Expand Up @@ -473,7 +470,6 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
16 changes: 13 additions & 3 deletions leaderboard/enriching/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"encoding/json"
"errors"
"fmt"
podium_leaderboard_webhooks_v1 "github.com/topfreegames/podium/leaderboard/v2/enriching/proto/webhook/v1"
"github.com/topfreegames/podium/leaderboard/v2/model"
"net/http"
"net/url"
"strings"

podium_leaderboard_webhooks_v1 "github.com/topfreegames/podium/leaderboard/v2/enriching/proto/webhook/v1"
"github.com/topfreegames/podium/leaderboard/v2/model"
"go.uber.org/zap"
)

const enrichURL = "/leaderboards/enrich"
Expand All @@ -22,18 +24,25 @@ type EnrichmentConfig struct {

type enricherImpl struct {
config EnrichmentConfig
lg *zap.Logger
}

// NewEnricher returns a new Enricher implementation.
func NewEnricher(config EnrichmentConfig) Enricher {
func NewEnricher(config EnrichmentConfig, logger *zap.Logger) Enricher {
return &enricherImpl{
config: config,
lg: logger,
}
}

func (e *enricherImpl) Enrich(tenantID, leaderboardID string, members []*model.Member) ([]*model.Member, error) {
tenantUrl, exists := e.config.WebhookUrls[tenantID]
if !exists {
e.lg.Info(fmt.Sprintf("tenantID '%s' enrichment webhook url not found", tenantID))
return members, nil
}

if len(members) == 0 {
return members, nil
}

Expand All @@ -55,6 +64,7 @@ func (e *enricherImpl) Enrich(tenantID, leaderboardID string, members []*model.M

req.Header.Set("Content-Type", "application/json")

e.lg.Info(fmt.Sprintf("calling enrichment webhook '%s' for tenantID '%s'", webhookUrl, tenantID))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions leaderboard/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/onsi/gomega v1.11.0
github.com/satori/go.uuid v1.2.0
github.com/spf13/viper v1.7.1
go.uber.org/zap v1.10.0
google.golang.org/genproto/googleapis/api v0.0.0-20230724170836-66ad5b6ff146
google.golang.org/grpc v1.56.2
google.golang.org/protobuf v1.31.0
Expand All @@ -34,6 +35,8 @@ require (
go.opentelemetry.io/otel v0.19.0 // indirect
go.opentelemetry.io/otel/metric v0.19.0 // indirect
go.opentelemetry.io/otel/trace v0.19.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion leaderboard/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
Expand Down Expand Up @@ -172,6 +171,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -234,8 +234,11 @@ go.opentelemetry.io/otel/oteltest v0.19.0 h1:YVfA0ByROYqTwOxqHVZYZExzEpfZor+MU1r
go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA=
go.opentelemetry.io/otel/trace v0.19.0 h1:1ucYlenXIDA1OlHVLDZKX0ObXV5RLaq06DtUKz5e5zc=
go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down

0 comments on commit da754bc

Please sign in to comment.