Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support a custom base URL for the new relic provider #1053

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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