-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce statsig package from SDK along with config
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package statsig | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
assert "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewConfig(t *testing.T) { | ||
t.Run("RunningInTestEnvironment", func(t *testing.T) { | ||
expected := "test" | ||
actual := os.Getenv("APP_ENV") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
testCases := []struct { | ||
name string | ||
dsn string | ||
localMode bool | ||
configSyncInterval time.Duration | ||
idListSyncInterval time.Duration | ||
wantError bool | ||
}{ | ||
{ | ||
name: "default", | ||
dsn: "", | ||
localMode: false, | ||
configSyncInterval: 0, | ||
idListSyncInterval: 0, | ||
wantError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
c, err := NewConfig() | ||
if tc.wantError { | ||
assert.Error(t, err) | ||
} | ||
|
||
assert.Equal(t, c.StatsigDSN, tc.dsn) | ||
assert.Equal(t, c.LocalMode, tc.localMode) | ||
assert.Equal(t, c.ConfigSyncInterval, tc.configSyncInterval) | ||
assert.Equal(t, c.IDListSyncInterval, tc.idListSyncInterval) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package statsig | ||
|
||
import ( | ||
statsig "github.com/statsig-io/go-sdk" | ||
) | ||
|
||
func Initialize(c *Config) { | ||
opts := &statsig.Options{ | ||
Environment: statsig.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 | ||
} | ||
|
||
statsig.InitializeWithOptions(c.StatsigDSN, opts) | ||
} | ||
|
||
func GetExperiment(user statsig.User, experiment string) statsig.DynamicConfig { | ||
return statsig.GetExperiment(user, experiment) | ||
} | ||
|
||
func GetFeatureFlag(user statsig.User, flag string) bool { | ||
return statsig.CheckGate(user, flag) | ||
} | ||
|
||
func Shutdown() { | ||
statsig.Shutdown() | ||
} |