From e5617ced883f90c85ee9bbdd69d81faab8e8ae62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eloy=20Dur=C3=A1n?= Date: Thu, 30 Aug 2018 19:54:30 +0200 Subject: [PATCH] [express] Add option to record HTTP headers. --- docs/API.md | 2 ++ src/plugins/express.js | 9 +++++++++ test/plugins/express.spec.js | 28 +++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/API.md b/docs/API.md index b8c99f5c3ba..a4926718f97 100644 --- a/docs/API.md +++ b/docs/API.md @@ -136,6 +136,7 @@ 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. |
Configuration Options
@@ -143,6 +144,7 @@ Each integration also has its own list of default tags. These tags get automatic |------------------|---------------------------|----------------------------------------| | 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. |

graphql

diff --git a/src/plugins/express.js b/src/plugins/express.js index fd351d35465..49fbfae92a2 100644 --- a/src/plugins/express.js +++ b/src/plugins/express.js @@ -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 @@ -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() diff --git a/test/plugins/express.spec.js b/test/plugins/express.spec.js index f98be025dd9..ff1192ccb5f 100644 --- a/test/plugins/express.spec.js +++ b/test/plugins/express.spec.js @@ -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) @@ -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) + }) + }) + }) }) }) })