Skip to content

Commit

Permalink
Allow setting Datadog site (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 authored Nov 24, 2021
1 parent 10303fa commit b2ba87f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
IMPROVEMENTS:
* agent: Allow disabling specific policy sources [[GH-544](https://github.com/hashicorp/nomad-autoscaler/pull/544)]
* agent: Dispense `fixed-value`, `pass-through`, and `threshold` strategy plugins by default [[GH-536](https://github.com/hashicorp/nomad-autoscaler/pull/536)]
* plugins/apm/datadog: Add support for custom server site [[GH-548](https://github.com/hashicorp/nomad-autoscaler/pull/548)]
* plugins/apm/prometheus: Add support for basic auth and custom headers [[GH-522](https://github.com/hashicorp/nomad-autoscaler/pull/522)]
* plugins/target/nomad: Reduce log level for active deployments error messages [[GH-542](https://github.com/hashicorp/nomad-autoscaler/pull/542)]
* policy: Prevent scaling cluster to zero when using the Nomad APM [[GH-534](https://github.com/hashicorp/nomad-autoscaler/pull/534)]
Expand Down
13 changes: 13 additions & 0 deletions plugins/builtin/apm/datadog/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const (
// pluginName is the name of the plugin
pluginName = "datadog"

// configKeySite is used to change the Datadog site
configKeySite = "site"

configKeyClientAPIKey = "dd_api_key"
configKeyClientAPPKey = "dd_app_key"

Expand Down Expand Up @@ -88,6 +91,16 @@ func (a *APMPlugin) SetConfig(config map[string]string) error {
datadogAuthAPPKey: {Key: a.config[configKeyClientAPPKey]},
},
)

// set Datadog site if provided
if a.config[configKeySite] != "" {
ctx = context.WithValue(ctx,
datadog.ContextServerVariables,
map[string]string{
"site": a.config[configKeySite],
})
}

a.clientCtx = ctx
configuration := datadog.NewConfiguration()
client := datadog.NewAPIClient(configuration)
Expand Down
57 changes: 36 additions & 21 deletions plugins/builtin/apm/datadog/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestAPMPlugin_SetConfig(t *testing.T) {
keyEnvVar string
appEnvVar string
expectOutput error
expectedContextKey interface{}
expectedContextValue interface{}
name string
}{
Expand Down Expand Up @@ -45,60 +46,74 @@ func TestAPMPlugin_SetConfig(t *testing.T) {
},

{
inputConfig: map[string]string{"dd_api_key": "fake-api-key", "dd_app_key": "some-app"},
keyEnvVar: "",
appEnvVar: "",
expectOutput: nil,
inputConfig: map[string]string{"dd_api_key": "fake-api-key", "dd_app_key": "some-app"},
keyEnvVar: "",
appEnvVar: "",
expectOutput: nil,
expectedContextKey: datadog.ContextAPIKeys,
expectedContextValue: map[string]datadog.APIKey{
"apiKeyAuth": {Key: "fake-api-key"},
"appKeyAuth": {Key: "some-app"},
},
name: "all required config parameters set by config map",
},
{
inputConfig: map[string]string{"dd_api_key": "fake-api-key", "dd_app_key": "some-app"},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "env-var-some-app",
expectOutput: nil,
inputConfig: map[string]string{"dd_api_key": "fake-api-key", "dd_app_key": "some-app"},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "env-var-some-app",
expectOutput: nil,
expectedContextKey: datadog.ContextAPIKeys,
expectedContextValue: map[string]datadog.APIKey{
"apiKeyAuth": {Key: "fake-api-key"},
"appKeyAuth": {Key: "some-app"},
},
name: "all required config parameters set by both config map and env vars",
},
{
inputConfig: map[string]string{},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "env-var-some-app",
expectOutput: nil,
inputConfig: map[string]string{},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "env-var-some-app",
expectOutput: nil,
expectedContextKey: datadog.ContextAPIKeys,
expectedContextValue: map[string]datadog.APIKey{
"apiKeyAuth": {Key: "env-var-fake-api-key"},
"appKeyAuth": {Key: "env-var-some-app"},
},
name: "all required config parameters set by env vars",
},
{
inputConfig: map[string]string{"dd_api_key": "fake-api-key"},
keyEnvVar: "",
appEnvVar: "env-var-some-app",
expectOutput: nil,
inputConfig: map[string]string{"dd_api_key": "fake-api-key"},
keyEnvVar: "",
appEnvVar: "env-var-some-app",
expectOutput: nil,
expectedContextKey: datadog.ContextAPIKeys,
expectedContextValue: map[string]datadog.APIKey{
"apiKeyAuth": {Key: "fake-api-key"},
"appKeyAuth": {Key: "env-var-some-app"},
},
name: "key set by config map, app set by env var",
},
{
inputConfig: map[string]string{"dd_app_key": "some-app"},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "",
expectOutput: nil,
inputConfig: map[string]string{"dd_app_key": "some-app"},
keyEnvVar: "env-var-fake-api-key",
appEnvVar: "",
expectOutput: nil,
expectedContextKey: datadog.ContextAPIKeys,
expectedContextValue: map[string]datadog.APIKey{
"apiKeyAuth": {Key: "env-var-fake-api-key"},
"appKeyAuth": {Key: "some-app"},
},
name: "app set by config map, key set by env var",
},
{
inputConfig: map[string]string{"site": "app.datadoghq.eu"},
expectOutput: nil,
expectedContextKey: datadog.ContextServerVariables,
expectedContextValue: map[string]string{
"site": "app.datadoghq.eu",
},
name: "site set by config map",
},
}

for _, tc := range testCases {
Expand All @@ -121,7 +136,7 @@ func TestAPMPlugin_SetConfig(t *testing.T) {
// non-nil context then we should have a non-nil client and vice
// versa.
if tc.expectedContextValue != nil {
assert.Equal(t, tc.expectedContextValue, apmPlugin.clientCtx.Value(datadog.ContextAPIKeys), tc.name)
assert.Equal(t, tc.expectedContextValue, apmPlugin.clientCtx.Value(tc.expectedContextKey), tc.name)
assert.NotNil(t, apmPlugin.client, tc.name)
} else {
assert.Nil(t, apmPlugin.clientCtx, tc.name)
Expand Down

0 comments on commit b2ba87f

Please sign in to comment.