diff --git a/pkg/experiment/local/client.go b/pkg/experiment/local/client.go index 8efe8d0..3308274 100644 --- a/pkg/experiment/local/client.go +++ b/pkg/experiment/local/client.go @@ -35,34 +35,40 @@ type Client struct { func Initialize(apiKey string, config *Config) *Client { initMutex.Lock() - client := clients[apiKey] + var usedKey string + if config == nil || config.DeploymentKey == "" { + usedKey = apiKey + } else { + usedKey = config.DeploymentKey + } + client := clients[usedKey] if client == nil { - if apiKey == "" { - panic("api key must be set") + if usedKey == "" { + panic("project api key or experiment deployment key must be set") } config = fillConfigDefaults(config) log := logger.New(config.Debug) var as *assignmentService - if config.AssignmentConfig != nil && config.AssignmentConfig.APIKey != "" { + if config.AssignmentConfig != nil && config.AssignmentConfig.APIKey != "" { amplitudeClient := amplitude.NewClient(config.AssignmentConfig.Config) as = &assignmentService{ amplitude: &litudeClient, - filter: newAssignmentFilter(config.AssignmentConfig.CacheCapacity), + filter: newAssignmentFilter(config.AssignmentConfig.CacheCapacity), } } client = &Client{ - log: log, - apiKey: apiKey, - config: config, - client: &http.Client{}, - poller: newPoller(), - flags: make(map[string]*evaluation.Flag), - flagsMutex: &sync.RWMutex{}, - engine: evaluation.NewEngine(log), + log: log, + apiKey: usedKey, + config: config, + client: &http.Client{}, + poller: newPoller(), + flags: make(map[string]*evaluation.Flag), + flagsMutex: &sync.RWMutex{}, + engine: evaluation.NewEngine(log), assignmentService: as, } client.log.Debug("config: %v", *config) - clients[apiKey] = client + clients[usedKey] = client } initMutex.Unlock() return client diff --git a/pkg/experiment/local/config.go b/pkg/experiment/local/config.go index 9c9cb9d..9a5bd09 100644 --- a/pkg/experiment/local/config.go +++ b/pkg/experiment/local/config.go @@ -11,6 +11,7 @@ type Config struct { FlagConfigPollerInterval time.Duration FlagConfigPollerRequestTimeout time.Duration AssignmentConfig *AssignmentConfig + DeploymentKey string } type AssignmentConfig struct { diff --git a/pkg/experiment/remote/client.go b/pkg/experiment/remote/client.go index 5ad832f..3850e16 100644 --- a/pkg/experiment/remote/client.go +++ b/pkg/experiment/remote/client.go @@ -29,15 +29,21 @@ type Client struct { func Initialize(apiKey string, config *Config) *Client { initMutex.Lock() - client := clients[apiKey] + var usedKey string + if config == nil || config.DeploymentKey == "" { + usedKey = apiKey + } else { + usedKey = config.DeploymentKey + } + client := clients[usedKey] if client == nil { - if apiKey == "" { - panic("api key must be set") + if usedKey == "" { + panic("project api key or experiment deployment key must be set") } config = fillConfigDefaults(config) client = &Client{ log: logger.New(config.Debug), - apiKey: apiKey, + apiKey: usedKey, config: config, client: &http.Client{}, } diff --git a/pkg/experiment/remote/config.go b/pkg/experiment/remote/config.go index aedd236..e5eafd2 100644 --- a/pkg/experiment/remote/config.go +++ b/pkg/experiment/remote/config.go @@ -3,10 +3,11 @@ package remote import "time" type Config struct { - Debug bool - ServerUrl string - FetchTimeout time.Duration - RetryBackoff *RetryBackoff + Debug bool + ServerUrl string + FetchTimeout time.Duration + RetryBackoff *RetryBackoff + DeploymentKey string } var DefaultConfig = &Config{