From 1cb3b0b503006a652e9fb9b73a4ae8a4e5e65044 Mon Sep 17 00:00:00 2001 From: Greg Dallavalle Date: Mon, 12 Feb 2024 12:34:55 -0600 Subject: [PATCH] client: support EU environments --- README.md | 2 ++ client/client.go | 12 +++++++++++- client/client_test.go | 12 +++++++++++- exporter/exporter.go | 5 +++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 79123072..ded8bd5f 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,10 @@ The `exporter` utility is built-in to the provider binary and requires certain e For the LIGHTSTEP_ENV variable: - public = app.lightstep.com +- eu-public = app.eu.lightstep.com - meta = app-meta.lightstep.com - staging = app-staging.lightstep.com +- eu-staging = app.eu-staging.lightstep.com ``` $ export LIGHTSTEP_API_KEY=.... diff --git a/client/client.go b/client/client.go index 38b06495..9a14c25b 100644 --- a/client/client.go +++ b/client/client.go @@ -78,7 +78,7 @@ func NewClient(apiKey string, orgName string, env string) *Client { return NewClientWithUserAgent(apiKey, orgName, env, fmt.Sprintf("%s/%s", DefaultUserAgent, version.ProviderVersion)) } -func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent string) *Client { +func resolveBaseURL(env string) string { // Let the user override the API base URL. // e.g. http://localhost:8080 envBaseURL := os.Getenv("LIGHTSTEP_API_BASE_URL") @@ -89,10 +89,20 @@ func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent baseURL = envBaseURL } else if env == "public" { baseURL = "https://api.lightstep.com" + } else if env == "eu-public" { + baseURL = "https://api.eu.lightstep.com" + } else if env == "eu-staging" { + baseURL = "https://api.eu-staging.lightstep.com" } else { baseURL = fmt.Sprintf("https://api-%v.lightstep.com", env) } + return baseURL +} + +func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent string) *Client { + baseURL := resolveBaseURL(env) + fullBaseURL := fmt.Sprintf("%s/public/v0.2/%v", baseURL, orgName) rateLimitStr := os.Getenv("LIGHTSTEP_API_RATE_LIMIT") diff --git a/client/client_test.go b/client/client_test.go index d8bd9410..f282ab78 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,8 +1,9 @@ package client import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestNew_public(t *testing.T) { @@ -24,3 +25,12 @@ func TestNew_env_var_provided_baseURL(t *testing.T) { c := NewClient("api-key", "org-name", "public") assert.Equal(t, "http://localhost:8080/public/v0.2/org-name", c.baseURL) } + +func Test_resolveBaseURL(t *testing.T) { + t.Parallel() + assert.Equal(t, "https://api.lightstep.com", resolveBaseURL("public")) + assert.Equal(t, "https://api-meta.lightstep.com", resolveBaseURL("meta")) + assert.Equal(t, "https://api.eu.lightstep.com", resolveBaseURL("eu-public")) + assert.Equal(t, "https://api.eu-staging.lightstep.com", resolveBaseURL("eu-staging")) + assert.Equal(t, "https://api-other.lightstep.com", resolveBaseURL("other")) +} diff --git a/exporter/exporter.go b/exporter/exporter.go index 13821515..d125c652 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -170,8 +170,9 @@ func Run(args ...string) error { // default to public API environment lightstepEnv := "public" - if len(os.Getenv("LIGHTSTEP_ENV")) > 0 { - lightstepEnv = os.Getenv("LIGHTSTEP_ENV") + userEnv := os.Getenv("LIGHTSTEP_ENV") + if len(userEnv) > 0 { + lightstepEnv = userEnv } if len(args) < 4 {