Skip to content

Commit 2e78930

Browse files
authored
Support DD_ env var prefix (#137)
Allow environment variables to be prefixed with either `DATADOG_` (old behavior) or `DD_` (new) in order to use the same configuration names as the Datadog Agent. Fixes #135.
1 parent 488adc7 commit 2e78930

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ There's also a longer [tutorial](https://dbader.org/blog/monitoring-your-nodejs-
5050

5151
### Datadog API key
5252

53-
Make sure the `DATADOG_API_KEY` environment variable is set to your Datadog
54-
API key (you can also set it via the `apiKey` option in code). You can find the API key under [Integrations > APIs](https://app.datadoghq.com/account/settings#api). *Please note the API key is different from an **application key**. For more details, see [Datadog’s “API and Application Keys” docs](https://docs.datadoghq.com/account_management/api-app-keys/).*
53+
Make sure the `DATADOG_API_KEY` or `DD_API_KEY` environment variable is set to your Datadog API key (you can also set it via the `apiKey` option in code). You can find the API key under [Integrations > APIs](https://app.datadoghq.com/account/settings#api). *Please note the API key is different from an **application key**. For more details, see [Datadog’s “API and Application Keys” docs](https://docs.datadoghq.com/account_management/api-app-keys/).*
5554

5655
### Module setup
5756

@@ -121,10 +120,11 @@ Where `options` is an object and can contain the following:
121120
* Defaults to `datadoghq.com`.
122121
* See more details on setting your site at:
123122
https://docs.datadoghq.com/getting_started/site/#access-the-datadog-site
124-
* You can also set this via the `DATADOG_SITE` environment variable.
123+
* You can also set this via the `DATADOG_SITE` or `DD_SITE` environment variable.
125124
* `apiKey`: Sets the Datadog API key. (optional)
126125
* It's usually best to keep this in an environment variable.
127-
Datadog-metrics looks for the API key in `DATADOG_API_KEY` by default.
126+
Datadog-metrics looks for the API key in the `DATADOG_API_KEY` or
127+
`DD_API_KEY` environment variable by default.
128128
* You must either set this option or the environment variable. An API key
129129
is required to send metrics.
130130
* Make sure not to confuse this with your _application_ key! For more
@@ -339,6 +339,8 @@ Contributions are always welcome! For more info on how to contribute or develop
339339
* The `flush()` method now returns a promise.
340340
* The `report(series)` method on any custom reporters should now return a promise. For now, datadog-metrics will use the old callback-based behavior if the method signature has callbacks listed after `series` argument.
341341

342+
* Environment variables can now be prefixed with *either* `DATADOG_` or `DD_` (previously, only `DATADOG_` worked) in order to match configuration with the Datadog agent. For example, you can set your API key via `DATADOG_API_KEY` or `DD_API_KEY`.
343+
342344
**Bug Fixes:**
343345

344346
* Support setting the `site` option via the `DATADOG_SITE` environment variable. The `apiHost` option was renamed to `site` in v0.11.0, but the `DATADOG_API_HOST` environment variable was accidentally left as-is. The old environment variable name is now deprecated, and will be removed at the same time as the `apiHost` option is removed.

lib/reporters.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ class DatadogReporter {
3939
}
4040
}
4141

42-
apiKey = apiKey || process.env.DATADOG_API_KEY;
43-
this.site = site || process.env.DATADOG_SITE || process.env.DATADOG_API_HOST;
42+
apiKey = apiKey || process.env.DATADOG_API_KEY || process.env.DD_API_KEY;
43+
this.site = site || process.env.DATADOG_SITE || process.env.DD_SITE || process.env.DATADOG_API_HOST;
4444

4545
if (!apiKey) {
46-
throw new Error('DATADOG_API_KEY environment variable not set');
46+
throw new Error(
47+
'Datadog API key not found. You must specify one via a ' +
48+
'configuration option or the DATADOG_API_KEY (or DD_API_KEY) ' +
49+
'environment variable.'
50+
);
4751
}
4852

4953
const configuration = datadogApiClient.client.createConfiguration({
@@ -104,10 +108,11 @@ class DatadogReporter {
104108
if (error.code === 403) {
105109
throw new AuthorizationError(
106110
'Your Datadog API key is not authorized to send ' +
107-
'metrics. Check to make sure the DATADOG_API_KEY ' +
108-
'environment variable or the `apiKey` init option is set ' +
109-
'to a valid API key for your Datadog account, and ' +
110-
'that it is not an *application* key. For more, see: ' +
111+
'metrics. Check to make sure the DATADOG_API_KEY or ' +
112+
'DD_API_KEY environment variable or the `apiKey` init ' +
113+
'option is set to a valid API key for your Datadog ' +
114+
'account, and that it is not an *application* key. ' +
115+
'For more, see: ' +
111116
'https://docs.datadoghq.com/account_management/api-app-keys/',
112117
{ cause: error }
113118
);

test/reporters_tests.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,27 @@ describe('DatadogReporter', function() {
2929
let originalEnv = Object.entries(process.env);
3030

3131
afterEach(() => {
32-
for (const [key, value] of originalEnv) {
33-
process.env[key] = value;
34-
}
32+
process.env = Object.fromEntries(originalEnv);
3533
});
3634

3735
it('creates a DatadogReporter', () => {
3836
const instance = new DatadogReporter('abc', '123', 'datadoghq.eu');
3937
instance.should.be.an.instanceof(DatadogReporter);
4038
});
4139

42-
it('reads the API key from environment if not specified', () => {
40+
it('reads the API key from DATADOG_API_KEY environment if not specified', () => {
4341
process.env.DATADOG_API_KEY = 'abc';
4442
const instance = new DatadogReporter();
4543
instance.should.be.an.instanceof(DatadogReporter);
4644
});
4745

48-
it('throws if no API key is set', () => {
49-
delete process.env.DATADOG_API_KEY;
46+
it('reads the API key from DD_API_KEY environment if not specified', () => {
47+
process.env.DD_API_KEY = 'abc';
48+
const instance = new DatadogReporter();
49+
instance.should.be.an.instanceof(DatadogReporter);
50+
});
5051

52+
it('throws if no API key is set', () => {
5153
(() => new DatadogReporter()).should.throw(/DATADOG_API_KEY/);
5254
});
5355
});

0 commit comments

Comments
 (0)