diff --git a/README.md b/README.md index 954336a..47dc762 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ $ yarn add datadog-winston ``` ## Options -- **apiKey**: Your datadog api key *[required]* +- **apiKey**: Your datadog api key or client token *[required]* - **hostname**: The machine/server hostname - **service**: The name of the application or service generating the logs - **ddsource**: The technology from which the logs originated diff --git a/__tests__/index.js b/__tests__/index.js index 14f9eed..bc333ec 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -36,17 +36,32 @@ describe('DatadogTransport#log(info, callback)', () => { opts: { intakeRegion: 'eu' } + }, + { + case: 'transfers logs to the browser intake', + uri: 'https://browser-http-intake.logs.datadoghq.com', + opts: { + apiKey: 'pubXXX' + } } ] .forEach(testCase => { it(testCase.case, async () => { + const opts = Object.assign({}, { + apiKey: 'apikey', + service: 'service', + ddsource: 'ddsource', + ddtags: 'env:production', + hostname: 'hostname' + }, testCase.opts ? testCase.opts : {}) + const scope = nock(testCase.uri, { reqheaders: { 'content-type': 'application/json' } }).post( - '/v1/input/apikey', + `/v1/input/${opts.apiKey}`, JSON.stringify({ dd: { trace_id: 'abc', @@ -61,14 +76,6 @@ describe('DatadogTransport#log(info, callback)', () => { hostname: 'hostname' }).reply(204) - const opts = Object.assign({}, { - apiKey: 'apikey', - service: 'service', - ddsource: 'ddsource', - ddtags: 'env:production', - hostname: 'hostname' - }, testCase.opts ? testCase.opts : {}) - const transport = new DatadogTransport(opts) const callback = jest.fn() await transport.log({ diff --git a/lib/index.js b/lib/index.js index 167f16c..3d3012b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,7 +11,7 @@ module.exports = class DatadogTransport extends Transport { * Constructor for the Datadog Transport responsible for making * HTTP requests whenever log messages are received * @param {!Object} opts Transport options - * @param {string} opts.apiKey The Datadog API key + * @param {string} opts.apiKey The Datadog API key or Client token * @param {string} [intakeRegion] The intake region to be used */ constructor (opts = {}) { @@ -21,11 +21,10 @@ module.exports = class DatadogTransport extends Transport { throw new Error('Missing required option: `apiKey`') } this.opts = opts - if (this.opts.intakeRegion === 'eu') { - this.api = `https://http-intake.logs.datadoghq.eu/v1/input/${opts.apiKey}` - } else { - this.api = `https://http-intake.logs.datadoghq.com/v1/input/${opts.apiKey}` - } + + const tld = opts.intakeRegion === 'eu' ? 'eu' : 'com' + const subdomain = opts.apiKey.startsWith('pub') ? 'browser-http-intake' : 'http-intake' + this.api = `https://${subdomain}.logs.datadoghq.${tld}/v1/input/${opts.apiKey}` } /**