Skip to content

Commit

Permalink
[express] Add option to record HTTP headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Sep 1, 2018
1 parent aa60786 commit e5617ce
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ Each integration also has its own list of default tags. These tags get automatic
| http.url | The complete URL of the request. |
| http.method | The HTTP method of the request. |
| http.status_code | The HTTP status code of the response. |
| http.headers.* | A recorded HTTP header. |

<h5 id="express-config">Configuration Options</h5>

| Option | Default | Description |
|------------------|---------------------------|----------------------------------------|
| service | *Service name of the app* | The service name for this integration. |
| validateStatus | `code => code < 500` | Callback function to determine if there was an error. It should take a status code as its only parameter and return `true` for success or `false` for errors. |
| recordHeaders | `[]` | An array of headers to include in the span metadata. |

<h3 id="graphql">graphql</h3>

Expand Down
9 changes: 9 additions & 0 deletions src/plugins/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const pathToRegExp = require('path-to-regexp')
const OPERATION_NAME = 'express.request'

function createWrapMethod (tracer, config) {
const recordHeaders = config.recordHeaders ? config.recordHeaders.map(key => key.toLowerCase()) : []

const validateStatus = typeof config.validateStatus === 'function'
? config.validateStatus
: code => code < 500
Expand Down Expand Up @@ -46,6 +48,13 @@ function createWrapMethod (tracer, config) {
span.setTag(Tags.ERROR, true)
}

recordHeaders.forEach(key => {
const value = req.headers[key]
if (value) {
span.setTag(`http.headers.${key}`, value)
}
})

span.finish()

req._datadog.scope && req._datadog.scope.close()
Expand Down
28 changes: 27 additions & 1 deletion test/plugins/express.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ describe('Plugin', () => {
beforeEach(() => {
config = {
service: 'custom',
validateStatus: code => code < 400
validateStatus: code => code < 400,
recordHeaders: ['User-Agent']
}

return agent.load(plugin, 'express', config)
Expand Down Expand Up @@ -602,6 +603,31 @@ describe('Plugin', () => {
})
})
})

it('should include specified headers in metadata', done => {
const app = express()

app.get('/user', (req, res) => {
res.status(200).send()
})

getPort().then(port => {
agent
.use(traces => {
expect(traces[0][0].meta).to.have.property('http.headers.user-agent', 'test')
})
.then(done)
.catch(done)

appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/user`, {
headers: { 'User-Agent': 'test' }
})
.catch(done)
})
})
})
})
})
})
Expand Down

0 comments on commit e5617ce

Please sign in to comment.