Skip to content

Commit

Permalink
fix(deps)!: update module gopkg.in/launchdarkly/go-sdk-common.v1 to v2 (
Browse files Browse the repository at this point in the history
#310)

* fix(deps): update module gopkg.in/launchdarkly/go-sdk-common.v1 to v2

* Adopt new interfaces

Co-authored-by: Renovate Bot <[email protected]>
Co-authored-by: Marcus Weiner <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2022
1 parent 85bf974 commit 5dd1f98
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 61 deletions.
4 changes: 2 additions & 2 deletions featureflag/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package featureflag

import (
"github.com/netlify/netlify-commons/util"
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-server-sdk.v5/interfaces"
)

type Config struct {
Key string `json:"key" yaml:"key"`
RequestTimeout util.Duration `json:"request_timeout" yaml:"request_timeout" mapstructure:"request_timeout" split_words:"true" default:"5s"`
Enabled bool `json:"enabled" yaml:"enabled" default:"false"`

updateProcessorFactory ld.UpdateProcessorFactory
updateProcessorFactory interfaces.DataSourceFactory

// Drop telemetry events (not needed in local-dev/CI environments)
DisableEvents bool `json:"disable_events" yaml:"disable_events" mapstructure:"disable_events" split_words:"true"`
Expand Down
71 changes: 40 additions & 31 deletions featureflag/featureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import (

"github.com/sirupsen/logrus"

"gopkg.in/launchdarkly/go-sdk-common.v1/ldvalue"
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldlog"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldlog"
"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
ld "gopkg.in/launchdarkly/go-server-sdk.v5"
"gopkg.in/launchdarkly/go-server-sdk.v5/interfaces"
"gopkg.in/launchdarkly/go-server-sdk.v5/interfaces/flagstate"
"gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
)

type Client interface {
Enabled(key, userID string, attrs ...Attr) bool
EnabledUser(key string, user ld.User) bool
EnabledUser(key string, user lduser.User) bool

Variation(key, defaultVal, userID string, attrs ...Attr) string
VariationUser(key string, defaultVal string, user ld.User) string
VariationUser(key string, defaultVal string, user lduser.User) string

AllEnabledFlags(key string) []string
AllEnabledFlagsUser(key string, user ld.User) []string
AllEnabledFlagsUser(key string, user lduser.User) []string
}

type ldClient struct {
Expand All @@ -30,27 +34,25 @@ type ldClient struct {
var _ Client = &ldClient{}

func NewClient(cfg *Config, logger logrus.FieldLogger) (Client, error) {
config := ld.DefaultConfig
config := ld.Config{}

if !cfg.Enabled {
config.Offline = true
}

if cfg.updateProcessorFactory != nil {
config.UpdateProcessorFactory = cfg.updateProcessorFactory
config.SendEvents = false
config.DataSource = cfg.updateProcessorFactory
config.Events = ldcomponents.NoEvents()
}

configureLogger(&config.Loggers, logger)
config.Logging = configureLogger(logger)

if cfg.RelayHost != "" {
config.BaseUri = cfg.RelayHost
config.StreamUri = cfg.RelayHost
config.EventsUri = cfg.RelayHost
config.ServiceEndpoints = ldcomponents.RelayProxyEndpoints(cfg.RelayHost)
}

if cfg.DisableEvents {
config.SendEvents = false
config.Events = ldcomponents.NoEvents()
}

inner, err := ld.MakeCustomClient(cfg.Key, config, cfg.RequestTimeout.Duration)
Expand All @@ -69,7 +71,7 @@ func (c *ldClient) Enabled(key string, userID string, attrs ...Attr) bool {
return c.EnabledUser(key, c.userWithAttrs(userID, attrs))
}

func (c *ldClient) EnabledUser(key string, user ld.User) bool {
func (c *ldClient) EnabledUser(key string, user lduser.User) bool {
res, err := c.BoolVariation(key, user, false)
if err != nil {
c.log.WithError(err).WithField("key", key).Error("Failed to load feature flag")
Expand All @@ -81,7 +83,7 @@ func (c *ldClient) Variation(key, defaultVal, userID string, attrs ...Attr) stri
return c.VariationUser(key, defaultVal, c.userWithAttrs(userID, attrs))
}

func (c *ldClient) VariationUser(key string, defaultVal string, user ld.User) string {
func (c *ldClient) VariationUser(key string, defaultVal string, user lduser.User) string {
res, err := c.StringVariation(key, user, defaultVal)
if err != nil {
c.log.WithError(err).WithField("key", key).Error("Failed to load feature flag")
Expand All @@ -90,28 +92,25 @@ func (c *ldClient) VariationUser(key string, defaultVal string, user ld.User) st
}

func (c *ldClient) AllEnabledFlags(key string) []string {
return c.AllEnabledFlagsUser(key, ld.NewUser(key))
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
}

func (c *ldClient) AllEnabledFlagsUser(key string, user ld.User) []string {
res := c.AllFlagsState(user, ld.DetailsOnlyForTrackedFlags)
func (c *ldClient) AllEnabledFlagsUser(key string, user lduser.User) []string {
res := c.AllFlagsState(user, flagstate.OptionDetailsOnlyForTrackedFlags())
flagMap := res.ToValuesMap()

var flags []string
for flag, value := range flagMap {
switch value.(type) {
case bool:
if value == true {
flags = append(flags, flag)
}
if value.BoolValue() {
flags = append(flags, flag)
}
}

return flags
}

func (c *ldClient) userWithAttrs(id string, attrs []Attr) ld.User {
b := ld.NewUserBuilder(id)
func (c *ldClient) userWithAttrs(id string, attrs []Attr) lduser.User {
b := lduser.NewUserBuilder(id)
for _, attr := range c.defaultAttrs {
b.Custom(attr.Name, attr.Value)
}
Expand All @@ -130,18 +129,28 @@ func StringAttr(name, value string) Attr {
return Attr{Name: name, Value: ldvalue.String(value)}
}

func configureLogger(ldLogger *ldlog.Loggers, log logrus.FieldLogger) {
func configureLogger(log logrus.FieldLogger) interfaces.LoggingConfigurationFactory {
if log == nil {
l := logrus.New()
l.SetOutput(ioutil.Discard)
log = l
}
log = log.WithField("component", "launch_darkly")

ldLogger.SetBaseLoggerForLevel(ldlog.Debug, &wrapLog{log.Debugln, log.Debugf})
ldLogger.SetBaseLoggerForLevel(ldlog.Info, &wrapLog{log.Infoln, log.Infof})
ldLogger.SetBaseLoggerForLevel(ldlog.Warn, &wrapLog{log.Warnln, log.Warnf})
ldLogger.SetBaseLoggerForLevel(ldlog.Error, &wrapLog{log.Errorln, log.Errorf})
return &logCreator{log: log}
}

type logCreator struct {
log logrus.FieldLogger
}

func (c *logCreator) CreateLoggingConfiguration(b interfaces.BasicConfiguration) (interfaces.LoggingConfiguration, error) {
logger := ldlog.NewDefaultLoggers()
logger.SetBaseLoggerForLevel(ldlog.Debug, &wrapLog{c.log.Debugln, c.log.Debugf})
logger.SetBaseLoggerForLevel(ldlog.Info, &wrapLog{c.log.Infoln, c.log.Infof})
logger.SetBaseLoggerForLevel(ldlog.Warn, &wrapLog{c.log.Warnln, c.log.Warnf})
logger.SetBaseLoggerForLevel(ldlog.Error, &wrapLog{c.log.Errorln, c.log.Errorf})
return ldcomponents.Logging().Loggers(logger).CreateLoggingConfiguration(b)
}

type wrapLog struct {
Expand Down
4 changes: 2 additions & 2 deletions featureflag/featureflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldfiledata"
"gopkg.in/launchdarkly/go-server-sdk.v5/ldfiledata"
)

func TestOfflineClient(t *testing.T) {
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestMockClient(t *testing.T) {
}

func TestAllEnabledFlags(t *testing.T) {
fileSource := ldfiledata.NewFileDataSourceFactory(ldfiledata.FilePaths("./fixtures/flags.yml"))
fileSource := ldfiledata.DataSource().FilePaths("./fixtures/flags.yml")
cfg := Config{
Key: "ABCD",
RequestTimeout: util.Duration{time.Second},
Expand Down
14 changes: 7 additions & 7 deletions featureflag/mock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package featureflag

import (
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
)

type MockClient struct {
Expand All @@ -12,18 +12,18 @@ type MockClient struct {
var _ Client = MockClient{}

func (c MockClient) Enabled(key, userID string, _ ...Attr) bool {
return c.EnabledUser(key, ld.NewUser(userID))
return c.EnabledUser(key, lduser.NewUser(userID))
}

func (c MockClient) EnabledUser(key string, _ ld.User) bool {
func (c MockClient) EnabledUser(key string, _ lduser.User) bool {
return c.BoolVars[key]
}

func (c MockClient) Variation(key string, defaultVal string, userID string, _ ...Attr) string {
return c.VariationUser(key, defaultVal, ld.NewUser(userID))
return c.VariationUser(key, defaultVal, lduser.NewUser(userID))
}

func (c MockClient) VariationUser(key string, defaultVal string, _ ld.User) string {
func (c MockClient) VariationUser(key string, defaultVal string, _ lduser.User) string {
res, ok := c.StringVars[key]
if !ok {
return defaultVal
Expand All @@ -32,10 +32,10 @@ func (c MockClient) VariationUser(key string, defaultVal string, _ ld.User) stri
}

func (c MockClient) AllEnabledFlags(key string) []string {
return c.AllEnabledFlagsUser(key, ld.NewUser(key))
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
}

func (c MockClient) AllEnabledFlagsUser(key string, _ ld.User) []string {
func (c MockClient) AllEnabledFlagsUser(key string, _ lduser.User) []string {
var res []string
for key, value := range c.BoolVars {
if value {
Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require (
github.com/DataDog/datadog-go v4.8.3+incompatible
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/armon/go-metrics v0.3.10
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/bugsnag/bugsnag-go/v2 v2.1.2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi v4.0.2+incompatible
Expand All @@ -15,8 +14,6 @@ require (
github.com/joho/godotenv v1.4.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/kr/text v0.2.0 // indirect
github.com/launchdarkly/eventsource v1.6.1 // indirect
github.com/launchdarkly/go-test-helpers v1.2.0 // indirect
github.com/nats-io/nats-streaming-server v0.15.1 // indirect
github.com/nats-io/nats.go v1.8.1
github.com/nats-io/stan.go v0.5.0
Expand All @@ -42,9 +39,8 @@ require (
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
gopkg.in/DataDog/dd-trace-go.v1 v1.34.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/ghodss/yaml.v1 v1.0.0 // indirect
gopkg.in/launchdarkly/go-sdk-common.v1 v1.0.0-20200401173443-991b2f427a01
gopkg.in/launchdarkly/go-server-sdk.v4 v4.0.0-20200729232655-2a44fb361895
gopkg.in/launchdarkly/go-sdk-common.v2 v2.5.0
gopkg.in/launchdarkly/go-server-sdk.v5 v5.8.1
gopkg.in/segmentio/analytics-go.v3 v3.1.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
Expand Down
Loading

0 comments on commit 5dd1f98

Please sign in to comment.