Skip to content

Commit

Permalink
feat: support a custom base URL for the new relic provider (#1053)
Browse files Browse the repository at this point in the history
Signed-off-by: Fischer Jemison <fjemison@newrelic.com>
  • Loading branch information
jemisonf authored Apr 1, 2021
1 parent e803db0 commit 7509478
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/analysis/newrelic.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ data:
account-id: <newrelic-account-id>
region: "us" # optional, defaults to "us" if not set. Only set to "eu" if you use EU New Relic
```

To use the New Relic metric provider from behind a proxy, provide a `base-url-rest` key pointing to the base URL of the New Relic REST API for your proxy, and a `base-url-nerdgraph` key pointing to the base URL for NerdGraph for your proxy:

```yaml
apiVersion: v1
kind: Secret
metadata:
name: newrelic
type: Opaque
data:
personal-api-key: <newrelic-personal-api-key>
account-id: <newrelic-account-id>
region: "us" # optional, defaults to "us" if not set. Only set to "eu" if you use EU New Relic
base-url-rest: <your-base-url>
base-url-nerdgraph: <your-base-url>
```
16 changes: 15 additions & 1 deletion metricproviders/newrelic/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,27 @@ func NewNewRelicAPIClient(metric v1alpha1.Metric, kubeclientset kubernetes.Inter

apiKey := string(secret.Data["personal-api-key"])
accountID := string(secret.Data["account-id"])

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

region := "us"
if _, ok := secret.Data["region"]; ok {
region = string(secret.Data["region"])
}
newrelicOptions = append(newrelicOptions, newrelic.ConfigRegion(region))

// base URL for the new relic REST API
if _, ok := secret.Data["base-url-rest"]; ok {
newrelicOptions = append(newrelicOptions, newrelic.ConfigBaseURL(string(secret.Data["base-url-rest"])))
}

// base URL for the nerdgraph (graphQL) API
if _, ok := secret.Data["base-url-nerdgraph"]; ok {
newrelicOptions = append(newrelicOptions, newrelic.ConfigNerdGraphBaseURL(string(secret.Data["base-url-nerdgraph"])))
}

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
12 changes: 12 additions & 0 deletions metricproviders/newrelic/newrelic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ 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-rest": []byte("example.com/api/v2"),
"base-url-nerdgraph": []byte("example.com/query"),
}
_, 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 7509478

Please sign in to comment.