Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #120

Closed
wants to merge 1 commit into from
Closed

WIP #120

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/rs/cors v1.7.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.10.1
github.com/statsig-io/go-sdk v1.26.0
github.com/stretchr/testify v1.8.4
github.com/twmb/franz-go v1.17.0
github.com/twmb/franz-go/pkg/kmsg v1.8.0
Expand Down Expand Up @@ -99,8 +100,10 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/statsig-io/ip3country-go v0.2.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/ua-parser/uap-go v0.0.0-20211112212520-00c877edfe0f // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.20.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,10 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
github.com/statsig-io/go-sdk v1.26.0 h1:HaPwN1+mDFdCnvqDbo8cr3obIlqmrShR9J2ogxw3kZs=
github.com/statsig-io/go-sdk v1.26.0/go.mod h1:Pej0D6R75gTHj7FdS6pbXQ7ayF0HL1cwOgiz5zDNdyc=
github.com/statsig-io/ip3country-go v0.2.0 h1:4z4ovVCx7GnQAKJC753bjcOgxLQJFsrDdcCKda4I2U8=
github.com/statsig-io/ip3country-go v0.2.0/go.mod h1:PKuA/VSpe4puBXw3BNGAHyP8IOZOiXAh/xIz+iYYoMQ=
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -1961,6 +1965,8 @@ github.com/twmb/franz-go v1.17.0 h1:hawgCx5ejDHkLe6IwAtFWwxi3OU4OztSTl7ZV5rwkYk=
github.com/twmb/franz-go v1.17.0/go.mod h1:NreRdJ2F7dziDY/m6VyspWd6sNxHKXdMZI42UfQ3GXM=
github.com/twmb/franz-go/pkg/kmsg v1.8.0 h1:lAQB9Z3aMrIP9qF9288XcFf/ccaSxEitNA1CDTEIeTA=
github.com/twmb/franz-go/pkg/kmsg v1.8.0/go.mod h1:HzYEb8G3uu5XevZbtU0dVbkphaKTHk0X68N5ka4q6mU=
github.com/ua-parser/uap-go v0.0.0-20211112212520-00c877edfe0f h1:A+MmlgpvrHLeUP8dkBVn4Pnf5Bp5Yk2OALm7SEJLLE8=
github.com/ua-parser/uap-go v0.0.0-20211112212520-00c877edfe0f/go.mod h1:OBcG9bn7sHtXgarhUEb3OfCnNsgtGnkVf41ilSZ3K3E=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
8 changes: 8 additions & 0 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
logger "github.com/scribd/go-sdk/pkg/logger"
"github.com/scribd/go-sdk/pkg/pubsub"
server "github.com/scribd/go-sdk/pkg/server"
"github.com/scribd/go-sdk/pkg/statsig"
tracking "github.com/scribd/go-sdk/pkg/tracking"
)

Expand All @@ -25,6 +26,7 @@ type Config struct {
PubSub *pubsub.Config
Cache *cache.Config
AWS *aws.Config
Statsig *statsig.Config
}

// NewConfig returns a new Config instance
Expand Down Expand Up @@ -77,6 +79,11 @@ func NewConfig() (*Config, error) {
errGroup = wrapErrors(errGroup, fmt.Errorf("aws config err: %w", err))
}

statsigConfig, err := statsig.NewConfig()
if err != nil {
errGroup = wrapErrors(errGroup, fmt.Errorf("statsig config err: %w", err))
}

config.App = appConfig
config.Database = dbConfig
config.Instrumentation = instrumentationConfig
Expand All @@ -86,6 +93,7 @@ func NewConfig() (*Config, error) {
config.PubSub = pubsubConfig
config.Cache = cacheConfig
config.AWS = awsConfig
config.Statsig = statsigConfig

return config, errGroup
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/context/statsig/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package statsig

import (
"context"
"fmt"

statsigsdk "github.com/statsig-io/go-sdk"
)

type ctxStatsigUserMarker struct{}

type ctxStatsig struct {
user statsigsdk.User
}

var (
ctxStatsigUserKey = &ctxStatsigUserMarker{}
)

// cmnt me
func Extract(ctx context.Context) (statsigsdk.User, error) {
r, ok := ctx.Value(ctxStatsigUserKey).(*ctxStatsig)
if !ok || r == nil {
return statsigsdk.User{}, fmt.Errorf("unable to get the statsig user")
}

return r.user, nil
}

// cmnt me
func ToContext(ctx context.Context, u statsigsdk.User) context.Context {
r := &ctxStatsig{
user: u,
}
return context.WithValue(ctx, ctxStatsigUserKey, r)
}
38 changes: 38 additions & 0 deletions pkg/statsig/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package statsig

import (
"fmt"
"os"
"time"

cbuilder "github.com/scribd/go-sdk/internal/pkg/configuration/builder"
)

// Config stores the configuration for the statsig.
type Config struct {
StatsigDSN string `mapstructure:"dsn"`
LocalMode bool `mapstructure:"local_mode"`
ConfigSyncInterval time.Duration `mapstructure:"config_sync_interval"`
IDListSyncInterval time.Duration `mapstructure:"id_list_sync_interval"`

environment string
}

// NewConfig returns a new StatsigConfig instance.
func NewConfig() (*Config, error) {
config := &Config{}
config.environment = os.Getenv("APP_ENV")

viperBuilder := cbuilder.New("statsig")

vConf, err := viperBuilder.Build()
if err != nil {
return config, err
}

if err = vConf.Unmarshal(config); err != nil {
return config, fmt.Errorf("unable to decode into struct: %s", err.Error())
}

return config, nil
}
46 changes: 46 additions & 0 deletions pkg/statsig/statsig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package statsig

import (
statsigsdk "github.com/statsig-io/go-sdk"
)

type (
GetFeatureFlagFunc func(user statsigsdk.User) bool
GetExperimentFunc func(user statsigsdk.User) statsigsdk.DynamicConfig
)

func Initialize(c *Config) {
opts := &statsigsdk.Options{
Environment: statsigsdk.Environment{Tier: c.environment},
}

if c.LocalMode {
opts.LocalMode = true
}

if c.ConfigSyncInterval > 0 {
opts.ConfigSyncInterval = c.ConfigSyncInterval
}

if c.IDListSyncInterval > 0 {
opts.IDListSyncInterval = c.IDListSyncInterval
}

statsigsdk.InitializeWithOptions(c.StatsigDSN, opts)
}

func GetExperiment(gate string) GetExperimentFunc {
return func(user statsigsdk.User) statsigsdk.DynamicConfig {
return statsigsdk.GetExperiment(user, gate)
}
}

func GetFeatureFlag(flag string) GetFeatureFlagFunc {
return func(user statsigsdk.User) bool {
return statsigsdk.GetGate(user, flag).Value
}
}

func Shutdown() {
statsigsdk.Shutdown()
}
Loading