Skip to content

Commit

Permalink
feat: support a custom base URL for the new relic provider
Browse files Browse the repository at this point in the history
enables use of the provider by people accessing new relic
via a proxy
  • Loading branch information
jemisonf committed Mar 29, 2021
1 parent e803db0 commit 4b495ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 13 additions & 4 deletions metricproviders/newrelic/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,22 @@ func NewNewRelicAPIClient(metric v1alpha1.Metric, kubeclientset kubernetes.Inter

apiKey := string(secret.Data["personal-api-key"])
accountID := string(secret.Data["account-id"])
region := "us"
if _, ok := secret.Data["region"]; ok {
region = string(secret.Data["region"])

newrelicOptions := []newrelic.ConfigOption{newrelic.ConfigPersonalAPIKey(apiKey), newrelic.ConfigUserAgent(userAgent)}

// support either base-url or region; default to us region if neither is used
if _, ok := secret.Data["base-url"]; ok {
newrelicOptions = append(newrelicOptions, newrelic.ConfigBaseURL(string(secret.Data["base-url"])))
} else {
region := "us"
if _, ok := secret.Data["region"]; ok {
region = string(secret.Data["region"])
}
newrelicOptions = append(newrelicOptions, newrelic.ConfigRegion(region))
}

if apiKey != "" && accountID != "" {
nrClient, err := newrelic.New(newrelic.ConfigPersonalAPIKey(apiKey), newrelic.ConfigRegion(region), newrelic.ConfigUserAgent(userAgent))
nrClient, err := newrelic.New(newrelicOptions...)
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions metricproviders/newrelic/newrelic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ func TestNewNewRelicAPIClient(t *testing.T) {
// client defaults to US when not set or set to something incorrect, does not error
assert.Nil(t, err)
})

t.Run("when a base-url is set", func(t *testing.T) {
tokenSecret.Data = map[string][]byte{
"personal-api-key": []byte("ABCDEFG01234"),
"account-id": []byte("12345"),
"base-url": []byte("example.com"),
}
_, err := NewNewRelicAPIClient(metric, fakeClient)

assert.Nil(t, err)
})
t.Run("with api token or account id missing missing", func(t *testing.T) {
tokenSecret.Data = map[string][]byte{
"personal-api-key": []byte("ABCDEFG01234"),
Expand Down

0 comments on commit 4b495ad

Please sign in to comment.