Skip to content

Commit 7fc2f98

Browse files
authored
fix logging when extracting from w3c traceparent (#5227)
1 parent f97f991 commit 7fc2f98

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

packages/dd-trace/src/opentracing/propagation/text_map.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ const b3HeaderExpr = /^(([0-9a-f]{16}){1,2}-[0-9a-f]{16}(-[01d](-[0-9a-f]{16})?)
3232
const baggageExpr = new RegExp(`^${baggagePrefix}(.+)$`)
3333
const tagKeyExpr = /^_dd\.p\.[\x21-\x2b\x2d-\x7e]+$/ // ASCII minus spaces and commas
3434
const tagValueExpr = /^[\x20-\x2b\x2d-\x7e]*$/ // ASCII minus commas
35-
const ddKeys = [traceKey, spanKey, samplingKey, originKey]
36-
const b3Keys = [b3TraceKey, b3SpanKey, b3ParentKey, b3SampledKey, b3FlagsKey, b3HeaderKey]
37-
const logKeys = ddKeys.concat(b3Keys)
3835
const traceparentExpr = /^([a-f0-9]{2})-([a-f0-9]{32})-([a-f0-9]{16})-([a-f0-9]{2})(-.*)?$/i
3936
const traceparentKey = 'traceparent'
37+
const tracestateKey = 'tracestate'
38+
const ddKeys = [traceKey, spanKey, samplingKey, originKey]
39+
const b3Keys = [b3TraceKey, b3SpanKey, b3ParentKey, b3SampledKey, b3FlagsKey, b3HeaderKey]
40+
const w3cKeys = [traceparentKey, tracestateKey]
41+
const logKeys = ddKeys.concat(b3Keys, w3cKeys)
4042
// Origin value in tracestate replaces '~', ',' and ';' with '_"
4143
const tracestateOriginFilter = /[^\x20-\x2b\x2d-\x3a\x3c-\x7d]/g
4244
// Tag keys in tracestate replace ' ', ',' and '=' with '_'
@@ -77,7 +79,12 @@ class TextMapPropagator {
7779
extractCh.publish({ spanContext, carrier })
7880
}
7981

80-
log.debug(() => `Extract from carrier: ${JSON.stringify(pick(carrier, logKeys))}.`)
82+
log.debug(() => {
83+
const keys = JSON.stringify(pick(carrier, logKeys))
84+
const styles = this._config.tracePropagationStyle.extract.join(', ')
85+
86+
return `Extract from carrier (${styles}): ${keys}.`
87+
})
8188

8289
return spanContext
8390
}

packages/dd-trace/test/opentracing/propagation/text_map.spec.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('TextMapPropagator', () => {
2121
let textMap
2222
let baggageItems
2323
let config
24+
let log
2425

2526
const createContext = (params = {}) => {
2627
const trace = { started: [], finished: [], tags: {} }
@@ -40,7 +41,12 @@ describe('TextMapPropagator', () => {
4041
}
4142

4243
beforeEach(() => {
43-
TextMapPropagator = require('../../../src/opentracing/propagation/text_map')
44+
log = {
45+
debug: sinon.spy()
46+
}
47+
TextMapPropagator = proxyquire('../src/opentracing/propagation/text_map', {
48+
'../../log': log
49+
})
4450
config = new Config({ tagsHeaderMaxLength: 512 })
4551
propagator = new TextMapPropagator(config)
4652
textMap = {
@@ -782,6 +788,18 @@ describe('TextMapPropagator', () => {
782788
expect(first._links[0].attributes.context_headers).to.equal('datadog')
783789
})
784790

791+
it('should log extraction', () => {
792+
const carrier = textMap
793+
794+
propagator.extract(carrier)
795+
796+
expect(log.debug).to.have.been.called
797+
expect(log.debug.firstCall.args[0]()).to.equal([
798+
'Extract from carrier (datadog, tracecontext, baggage):',
799+
'{"x-datadog-trace-id":"123","x-datadog-parent-id":"456"}.'
800+
].join(' '))
801+
})
802+
785803
describe('with B3 propagation as multiple headers', () => {
786804
beforeEach(() => {
787805
config.tracePropagationStyle.extract = ['b3multi']
@@ -857,6 +875,19 @@ describe('TextMapPropagator', () => {
857875

858876
expect(spanContext).to.be.null
859877
})
878+
879+
it('should log extraction', () => {
880+
textMap['x-b3-traceid'] = '0000000000000123'
881+
textMap['x-b3-spanid'] = '0000000000000456'
882+
883+
propagator.extract(textMap)
884+
885+
expect(log.debug).to.have.been.called
886+
expect(log.debug.firstCall.args[0]()).to.equal([
887+
'Extract from carrier (b3multi):',
888+
'{"x-b3-traceid":"0000000000000123","x-b3-spanid":"0000000000000456"}.'
889+
].join(' '))
890+
})
860891
})
861892

862893
describe('with B3 propagation as a single header', () => {
@@ -991,6 +1022,17 @@ describe('TextMapPropagator', () => {
9911022
spanId: id('456', 16)
9921023
}))
9931024
})
1025+
1026+
it('should log extraction', () => {
1027+
textMap.b3 = '0000000000000123-0000000000000456'
1028+
1029+
propagator.extract(textMap)
1030+
1031+
expect(log.debug).to.have.been.called
1032+
expect(log.debug.firstCall.args[0]()).to.equal(
1033+
`Extract from carrier (b3 single header): {"b3":"${textMap.b3}"}.`
1034+
)
1035+
})
9941036
})
9951037

9961038
describe('With traceparent propagation as single header', () => {
@@ -1119,6 +1161,21 @@ describe('TextMapPropagator', () => {
11191161
expect(carrier['x-datadog-tags']).to.include('_dd.p.dm=-0')
11201162
expect(spanContext._trace.tags['_dd.p.dm']).to.eql('-0')
11211163
})
1164+
1165+
it('should log extraction', () => {
1166+
const traceparent = textMap.traceparent = '00-1111aaaa2222bbbb3333cccc4444dddd-5555eeee6666ffff-01'
1167+
const tracestate = textMap.tracestate = 'other=bleh,dd=t.foo_bar_baz_:abc_!@#$%^&*()_+`-~;s:2;o:foo;t.dm:-4'
1168+
1169+
config.tracePropagationStyle.extract = ['tracecontext']
1170+
1171+
propagator.extract(textMap)
1172+
1173+
expect(log.debug).to.have.been.called
1174+
expect(log.debug.firstCall.args[0]()).to.equal([
1175+
'Extract from carrier (tracecontext):',
1176+
`{"traceparent":"${traceparent}","tracestate":"${tracestate}"}.`
1177+
].join(' '))
1178+
})
11221179
})
11231180
})
11241181
})

0 commit comments

Comments
 (0)