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

enable client token support #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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({
Expand Down
11 changes: 5 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand All @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no better way to set the subdomain? Setting in based on the value of apiKey doesn't feel right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open to a separating the apiKey/clientToken opts, but the subdomains are dependent on the type of key you provide. I.e. sending a client token to http-intake does not result in logs

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itsfadnis is separating apiKey / clientToken to determine the subdomain ok with you?

I'm interested in this feature

this.api = `https://${subdomain}.logs.datadoghq.${tld}/v1/input/${opts.apiKey}`
}

/**
Expand Down