Skip to content

Commit 5dc76ab

Browse files
authored
add missing shared config for clientIpHeader (#5473)
* add missing shared config for clientIpHeader
1 parent 3082b98 commit 5dc76ab

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

packages/datadog-plugin-koa/test/index.spec.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ describe('Plugin', () => {
2525
})
2626

2727
describe('without configuration', () => {
28-
before(() => agent.load(['koa', 'http'], [{}, { client: false }]))
28+
before(() => agent.load(
29+
['koa', 'http'],
30+
[{}, { client: false }],
31+
{
32+
// this is needed to test the client IP header configuration and must be done before loading the tracer
33+
// initially since we can't change the tracer config after it's loaded
34+
clientIpEnabled: true,
35+
clientIpHeader: 'x-custom-client-ip-header'
36+
})
37+
)
2938

3039
after(() => agent.close({ ritmReset: false }))
3140

@@ -216,6 +225,58 @@ describe('Plugin', () => {
216225
})
217226
})
218227

228+
it('should handle client IP header configuration', done => {
229+
const app = new Koa()
230+
231+
app.use(async (ctx) => {
232+
ctx.body = ''
233+
})
234+
235+
appListener = app.listen(0, 'localhost', () => {
236+
const port = appListener.address().port
237+
238+
agent
239+
.use(traces => {
240+
const spans = sort(traces[0])
241+
expect(spans[0].meta).to.have.property('http.client_ip', '8.8.8.8')
242+
})
243+
.then(done)
244+
.catch(done)
245+
246+
axios.get(`http://localhost:${port}/user`, {
247+
headers: {
248+
'x-custom-client-ip-header': '8.8.8.8'
249+
}
250+
}).catch(done)
251+
})
252+
})
253+
254+
it('should not add client IP tag when header is missing or a different header is used', done => {
255+
const app = new Koa()
256+
257+
app.use(async (ctx) => {
258+
ctx.body = ''
259+
})
260+
261+
appListener = app.listen(0, 'localhost', () => {
262+
const port = appListener.address().port
263+
264+
agent
265+
.use(traces => {
266+
const spans = sort(traces[0])
267+
expect(spans[0].meta).to.not.have.property('http.client_ip')
268+
})
269+
.then(done)
270+
.catch(done)
271+
272+
axios.get(`http://localhost:${port}/user`, {
273+
headers: {
274+
'x-other-custom-client-ip-header': '8.8.8.8'
275+
}
276+
}).catch(done)
277+
})
278+
})
279+
219280
withVersions('koa', 'koa-route', routerVersion => {
220281
let koaRouter
221282

packages/dd-trace/src/plugin_manager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ module.exports = class PluginManager {
133133
dbmPropagationMode,
134134
dsmEnabled,
135135
clientIpEnabled,
136+
clientIpHeader,
136137
memcachedCommandEnabled,
137138
ciVisibilityTestSessionName,
138139
ciVisAgentlessLogSubmissionEnabled,
@@ -148,6 +149,7 @@ module.exports = class PluginManager {
148149
site,
149150
url,
150151
headers: headerTags || [],
152+
clientIpHeader,
151153
ciVisibilityTestSessionName,
152154
ciVisAgentlessLogSubmissionEnabled,
153155
isTestDynamicInstrumentationEnabled,

packages/dd-trace/test/plugins/util/web.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,39 @@ describe('plugins/util/web', () => {
206206
})
207207
})
208208

209+
it('should add custom client ip tag to the span when it is configured', () => {
210+
req.headers['X-Forwad-For'] = '8.8.8.8'
211+
212+
config.clientIpEnabled = true
213+
config.clientIpHeader = 'X-Forwad-For'
214+
215+
web.normalizeConfig(config)
216+
web.instrument(tracer, config, req, res, 'test.request', span => {
217+
const tags = span.context()._tags
218+
219+
res.end()
220+
221+
expect(tags).to.include({
222+
[HTTP_CLIENT_IP]: '8.8.8.8'
223+
})
224+
})
225+
})
226+
227+
it('should not add custom client ip tag to the span when it is not configured', () => {
228+
req.headers['X-Forwad-For'] = '8.8.8.8'
229+
230+
config.clientIpEnabled = true
231+
232+
web.normalizeConfig(config)
233+
web.instrument(tracer, config, req, res, 'test.request', span => {
234+
const tags = span.context()._tags
235+
236+
res.end()
237+
238+
expect(tags).to.not.have.property(HTTP_CLIENT_IP)
239+
})
240+
})
241+
209242
it('should not add client ip tag to the span when disabled', () => {
210243
req.headers['x-forwarded-for'] = '8.8.8.8'
211244

0 commit comments

Comments
 (0)