Skip to content

Commit

Permalink
Support DD_ env var prefix (#137)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mr0grog authored Dec 4, 2024
1 parent 488adc7 commit 2e78930
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ There's also a longer [tutorial](https://dbader.org/blog/monitoring-your-nodejs-

### Datadog API key

Make sure the `DATADOG_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/).*
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/).*

### Module setup

Expand Down Expand Up @@ -121,10 +120,11 @@ Where `options` is an object and can contain the following:
* Defaults to `datadoghq.com`.
* See more details on setting your site at:
https://docs.datadoghq.com/getting_started/site/#access-the-datadog-site
* You can also set this via the `DATADOG_SITE` environment variable.
* You can also set this via the `DATADOG_SITE` or `DD_SITE` environment variable.
* `apiKey`: Sets the Datadog API key. (optional)
* It's usually best to keep this in an environment variable.
Datadog-metrics looks for the API key in `DATADOG_API_KEY` by default.
Datadog-metrics looks for the API key in the `DATADOG_API_KEY` or
`DD_API_KEY` environment variable by default.
* You must either set this option or the environment variable. An API key
is required to send metrics.
* Make sure not to confuse this with your _application_ key! For more
Expand Down Expand Up @@ -339,6 +339,8 @@ Contributions are always welcome! For more info on how to contribute or develop
* The `flush()` method now returns a promise.
* 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.

* 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`.

**Bug Fixes:**

* 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.
Expand Down
19 changes: 12 additions & 7 deletions lib/reporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ class DatadogReporter {
}
}

apiKey = apiKey || process.env.DATADOG_API_KEY;
this.site = site || process.env.DATADOG_SITE || process.env.DATADOG_API_HOST;
apiKey = apiKey || process.env.DATADOG_API_KEY || process.env.DD_API_KEY;
this.site = site || process.env.DATADOG_SITE || process.env.DD_SITE || process.env.DATADOG_API_HOST;

if (!apiKey) {
throw new Error('DATADOG_API_KEY environment variable not set');
throw new Error(
'Datadog API key not found. You must specify one via a ' +
'configuration option or the DATADOG_API_KEY (or DD_API_KEY) ' +
'environment variable.'
);
}

const configuration = datadogApiClient.client.createConfiguration({
Expand Down Expand Up @@ -104,10 +108,11 @@ class DatadogReporter {
if (error.code === 403) {
throw new AuthorizationError(
'Your Datadog API key is not authorized to send ' +
'metrics. Check to make sure the DATADOG_API_KEY ' +
'environment variable or the `apiKey` init option is set ' +
'to a valid API key for your Datadog account, and ' +
'that it is not an *application* key. For more, see: ' +
'metrics. Check to make sure the DATADOG_API_KEY or ' +
'DD_API_KEY environment variable or the `apiKey` init ' +
'option is set to a valid API key for your Datadog ' +
'account, and that it is not an *application* key. ' +
'For more, see: ' +
'https://docs.datadoghq.com/account_management/api-app-keys/',
{ cause: error }
);
Expand Down
14 changes: 8 additions & 6 deletions test/reporters_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ describe('DatadogReporter', function() {
let originalEnv = Object.entries(process.env);

afterEach(() => {
for (const [key, value] of originalEnv) {
process.env[key] = value;
}
process.env = Object.fromEntries(originalEnv);
});

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

it('reads the API key from environment if not specified', () => {
it('reads the API key from DATADOG_API_KEY environment if not specified', () => {
process.env.DATADOG_API_KEY = 'abc';
const instance = new DatadogReporter();
instance.should.be.an.instanceof(DatadogReporter);
});

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

it('throws if no API key is set', () => {
(() => new DatadogReporter()).should.throw(/DATADOG_API_KEY/);
});
});
Expand Down

0 comments on commit 2e78930

Please sign in to comment.