Skip to content

Commit e4f2afd

Browse files
committed
[express] Add option to record HTTP headers.
1 parent f6898b9 commit e4f2afd

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/API.md

+2
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ Each integration also has its own list of default tags. These tags get automatic
136136
| http.url | The complete URL of the request. |
137137
| http.method | The HTTP method of the request. |
138138
| http.status_code | The HTTP status code of the response. |
139+
| http.headers.* | A recorded HTTP header. |
139140

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

142143
| Option | Default | Description |
143144
|------------------|---------------------------|----------------------------------------|
144145
| service | *Service name of the app* | The service name for this integration. |
145146
| 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. |
147+
| recordHeaders | `undefined` | An array of headers to include in the span metadata. |
146148

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

src/plugins/express.js

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ function createWrapMethod (tracer, config) {
4646
span.setTag(Tags.ERROR, true)
4747
}
4848

49+
if (config.recordHeaders) {
50+
config.recordHeaders.forEach(key => {
51+
key = key.toLowerCase()
52+
const value = req.headers[key]
53+
if (value) {
54+
span.setTag(`http.headers.${key}`, value)
55+
}
56+
})
57+
}
58+
4959
span.finish()
5060

5161
req._datadog.scope && req._datadog.scope.close()

test/plugins/express.spec.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ describe('Plugin', () => {
546546
beforeEach(() => {
547547
config = {
548548
service: 'custom',
549-
validateStatus: code => code < 400
549+
validateStatus: code => code < 400,
550+
recordHeaders: ['User-Agent']
550551
}
551552

552553
return agent.load(plugin, 'express', config)
@@ -602,6 +603,31 @@ describe('Plugin', () => {
602603
})
603604
})
604605
})
606+
607+
it('should include specified headers in metadata', done => {
608+
const app = express()
609+
610+
app.get('/user', (req, res) => {
611+
res.status(200).send()
612+
})
613+
614+
getPort().then(port => {
615+
agent
616+
.use(traces => {
617+
expect(traces[0][0].meta).to.have.property('http.headers.user-agent', 'test')
618+
})
619+
.then(done)
620+
.catch(done)
621+
622+
appListener = app.listen(port, 'localhost', () => {
623+
axios
624+
.get(`http://localhost:${port}/user`, {
625+
headers: { 'User-Agent': 'test' }
626+
})
627+
.catch(done)
628+
})
629+
})
630+
})
605631
})
606632
})
607633
})

0 commit comments

Comments
 (0)