Skip to content

Commit d8d58fa

Browse files
authored
feature: allow specifying custom featureflag atributes (#264)
* featureflag: allow passing custom user attributes * Specify default attrs via config * Clearer name
1 parent fe31619 commit d8d58fa

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

featureflag/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ type Config struct {
1717

1818
// Set when using the Launch Darkly Relay proxy
1919
RelayHost string `json:"relay_host" yaml:"relay_host" mapstructure:"relay_host" split_words:"true"`
20+
21+
// DefaultUserAttrs are custom LaunchDarkly user attributes that are added to every
22+
// feature flag check
23+
DefaultUserAttrs map[string]string `json:"default_user_attrs" yaml:"default_user_attrs"`
2024
}

featureflag/featureflag.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import (
55

66
"github.com/sirupsen/logrus"
77

8+
"gopkg.in/launchdarkly/go-sdk-common.v1/ldvalue"
89
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
910
"gopkg.in/launchdarkly/go-server-sdk.v4/ldlog"
1011
)
1112

1213
type Client interface {
13-
Enabled(key, userID string) bool
14+
Enabled(key, userID string, attrs ...Attr) bool
1415
EnabledUser(key string, user ld.User) bool
1516

16-
Variation(key, defaultVal, userID string) string
17+
Variation(key, defaultVal, userID string, attrs ...Attr) string
1718
VariationUser(key string, defaultVal string, user ld.User) string
1819

1920
AllEnabledFlags(key string) []string
@@ -22,7 +23,8 @@ type Client interface {
2223

2324
type ldClient struct {
2425
*ld.LDClient
25-
log logrus.FieldLogger
26+
log logrus.FieldLogger
27+
defaultAttrs []Attr
2628
}
2729

2830
var _ Client = &ldClient{}
@@ -55,11 +57,16 @@ func NewClient(cfg *Config, logger logrus.FieldLogger) (Client, error) {
5557
if err != nil {
5658
logger.WithError(err).Error("Unable to construct LD client")
5759
}
58-
return &ldClient{inner, logger}, err
60+
61+
var defaultAttrs []Attr
62+
for k, v := range cfg.DefaultUserAttrs {
63+
defaultAttrs = append(defaultAttrs, StringAttr(k, v))
64+
}
65+
return &ldClient{inner, logger, defaultAttrs}, err
5966
}
6067

61-
func (c *ldClient) Enabled(key string, userID string) bool {
62-
return c.EnabledUser(key, ld.NewUser(userID))
68+
func (c *ldClient) Enabled(key string, userID string, attrs ...Attr) bool {
69+
return c.EnabledUser(key, c.userWithAttrs(userID, attrs))
6370
}
6471

6572
func (c *ldClient) EnabledUser(key string, user ld.User) bool {
@@ -70,8 +77,8 @@ func (c *ldClient) EnabledUser(key string, user ld.User) bool {
7077
return res
7178
}
7279

73-
func (c *ldClient) Variation(key, defaultVal, userID string) string {
74-
return c.VariationUser(key, defaultVal, ld.NewUser(userID))
80+
func (c *ldClient) Variation(key, defaultVal, userID string, attrs ...Attr) string {
81+
return c.VariationUser(key, defaultVal, c.userWithAttrs(userID, attrs))
7582
}
7683

7784
func (c *ldClient) VariationUser(key string, defaultVal string, user ld.User) string {
@@ -103,6 +110,26 @@ func (c *ldClient) AllEnabledFlagsUser(key string, user ld.User) []string {
103110
return flags
104111
}
105112

113+
func (c *ldClient) userWithAttrs(id string, attrs []Attr) ld.User {
114+
b := ld.NewUserBuilder(id)
115+
for _, attr := range c.defaultAttrs {
116+
b.Custom(attr.Name, attr.Value)
117+
}
118+
for _, attr := range attrs {
119+
b.Custom(attr.Name, attr.Value)
120+
}
121+
return b.Build()
122+
}
123+
124+
type Attr struct {
125+
Name string
126+
Value ldvalue.Value
127+
}
128+
129+
func StringAttr(name, value string) Attr {
130+
return Attr{Name: name, Value: ldvalue.String(value)}
131+
}
132+
106133
func configureLogger(ldLogger *ldlog.Loggers, log logrus.FieldLogger) {
107134
if log == nil {
108135
l := logrus.New()

featureflag/global.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func Init(conf Config, log logrus.FieldLogger) error {
3434
return nil
3535
}
3636

37-
func Enabled(key, userID string) bool {
38-
return GetGlobalClient().Enabled(key, userID)
37+
func Enabled(key, userID string, attrs ...Attr) bool {
38+
return GetGlobalClient().Enabled(key, userID, attrs...)
3939
}
4040

41-
func Variation(key, defaultVal, userID string) string {
42-
return GetGlobalClient().Variation(key, defaultVal, userID)
41+
func Variation(key, defaultVal, userID string, attrs ...Attr) string {
42+
return GetGlobalClient().Variation(key, defaultVal, userID, attrs...)
4343
}

featureflag/mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ type MockClient struct {
1111

1212
var _ Client = MockClient{}
1313

14-
func (c MockClient) Enabled(key, userID string) bool {
14+
func (c MockClient) Enabled(key, userID string, _ ...Attr) bool {
1515
return c.EnabledUser(key, ld.NewUser(userID))
1616
}
1717

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

22-
func (c MockClient) Variation(key string, defaultVal string, userID string) string {
22+
func (c MockClient) Variation(key string, defaultVal string, userID string, _ ...Attr) string {
2323
return c.VariationUser(key, defaultVal, ld.NewUser(userID))
2424
}
2525

0 commit comments

Comments
 (0)