From c880abb31386ed871bb2092726467558baa0b4f8 Mon Sep 17 00:00:00 2001 From: tyiuhc Date: Thu, 1 Feb 2024 11:31:53 -0800 Subject: [PATCH 1/3] feat: Enable use of project API key for default deployments --- pkg/experiment/local/client.go | 32 +++++++++++++++++++------------- pkg/experiment/local/config.go | 1 + pkg/experiment/remote/client.go | 14 ++++++++++---- pkg/experiment/remote/config.go | 9 +++++---- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pkg/experiment/local/client.go b/pkg/experiment/local/client.go index 8efe8d0..66d020f 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.DeploymentKey == "" { + usedKey = apiKey + } else { + usedKey = config.DeploymentKey + } + client := clients[usedKey] if client == nil { - if apiKey == "" { + if usedKey == "" { panic("api 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..06deb25 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.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{ From 7636747b141a317df3aae54ddf0d3f017f21a930 Mon Sep 17 00:00:00 2001 From: tyiuhc Date: Fri, 2 Feb 2024 13:36:49 -0800 Subject: [PATCH 2/3] update panic message --- pkg/experiment/local/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/experiment/local/client.go b/pkg/experiment/local/client.go index 66d020f..2c258ec 100644 --- a/pkg/experiment/local/client.go +++ b/pkg/experiment/local/client.go @@ -44,7 +44,7 @@ func Initialize(apiKey string, config *Config) *Client { client := clients[usedKey] if client == nil { if usedKey == "" { - panic("api key must be set") + panic("project api key or experiment deployment key must be set") } config = fillConfigDefaults(config) log := logger.New(config.Debug) From dde24dae982b4076e466feb7a2eb9d0a5e44f57c Mon Sep 17 00:00:00 2001 From: tyiuhc Date: Fri, 2 Feb 2024 13:47:17 -0800 Subject: [PATCH 3/3] fix client initialization --- pkg/experiment/local/client.go | 2 +- pkg/experiment/remote/client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/experiment/local/client.go b/pkg/experiment/local/client.go index 2c258ec..3308274 100644 --- a/pkg/experiment/local/client.go +++ b/pkg/experiment/local/client.go @@ -36,7 +36,7 @@ type Client struct { func Initialize(apiKey string, config *Config) *Client { initMutex.Lock() var usedKey string - if config.DeploymentKey == "" { + if config == nil || config.DeploymentKey == "" { usedKey = apiKey } else { usedKey = config.DeploymentKey diff --git a/pkg/experiment/remote/client.go b/pkg/experiment/remote/client.go index 06deb25..3850e16 100644 --- a/pkg/experiment/remote/client.go +++ b/pkg/experiment/remote/client.go @@ -30,7 +30,7 @@ type Client struct { func Initialize(apiKey string, config *Config) *Client { initMutex.Lock() var usedKey string - if config.DeploymentKey == "" { + if config == nil || config.DeploymentKey == "" { usedKey = apiKey } else { usedKey = config.DeploymentKey