Skip to content

Commit

Permalink
configurable tracing header w/ aws signed request
Browse files Browse the repository at this point in the history
- fixes #3719
- see #3796 for prior attempt
  • Loading branch information
tlhunter committed Nov 30, 2023
1 parent 92ce16b commit ced812e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,14 @@ declare namespace plugins {
* @default code => code < 500
*/
validateStatus?: (code: number) => boolean;

/**
* Enable injection of tracing headers into requests signed with AWS IAM headers.
* Disable this if you get AWS signature errors (HTTP 403).
*
* @default false
*/
enableTracingWithAmazonSignature?: boolean;
}

/** @hidden */
Expand Down
14 changes: 13 additions & 1 deletion packages/datadog-plugin-http/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HttpClientPlugin extends ClientPlugin {
span._spanContext._trace.record = false
}

if (!(hasAmazonSignature(options) || !this.config.propagationFilter(uri))) {
if (this.shouldInjectTraceHeaders(options, uri)) {
this.tracer.inject(span, HTTP_HEADERS, options.headers)
}

Expand All @@ -71,6 +71,18 @@ class HttpClientPlugin extends ClientPlugin {
return message.currentStore
}

shouldInjectTraceHeaders (options, uri) {
if (hasAmazonSignature(options) && !this.config.enableTracingWithAmazonSignature) {
return false
}

if (!this.config.propagationFilter(uri)) {
return false
}

return true
}

bindAsyncStart ({ parentStore }) {
return parentStore
}
Expand Down
46 changes: 46 additions & 0 deletions packages/datadog-plugin-http/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,52 @@ describe('Plugin', () => {
})
})

describe('with config enableTracingWithAmazonSignature enabled', () => {
let config

beforeEach(() => {
config = {
enableTracingWithAmazonSignature: true
}

return agent.load('http', config)
.then(() => {
http = require(protocol)
express = require('express')
})
})

it('should inject tracing header into AWS signed request', done => {
const app = express()

app.get('/', (req, res) => {
try {
expect(req.get('x-datadog-trace-id')).to.be.a('string')
expect(req.get('x-datadog-parent-id')).to.be.a('string')

res.status(200).send()

done()
} catch (e) {
done(e)
}
})

getPort().then(port => {
appListener = server(app, port, () => {
const req = http.request({
port,
headers: {
Authorization: 'AWS4-HMAC-SHA256 ...'
}
})

req.end()
})
})
})
})

describe('with validateStatus configuration', () => {
let config

Expand Down

0 comments on commit ced812e

Please sign in to comment.