Skip to content

Commit

Permalink
Protect some lines in text_map.js
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Oct 24, 2024
1 parent aff335d commit 25f4965
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/dd-trace/src/opentracing/propagation/text_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class TextMapPropagator {
}

inject (spanContext, carrier) {
if (!spanContext || !carrier) return

this._injectBaggageItems(spanContext, carrier)
this._injectDatadog(spanContext, carrier)
this._injectB3MultipleHeaders(spanContext, carrier)
Expand Down Expand Up @@ -383,7 +385,7 @@ class TextMapPropagator {
return null
}
const matches = headerValue.trim().match(traceparentExpr)
if (matches.length) {
if (matches?.length) {
const [version, traceId, spanId, flags, tail] = matches.slice(1)
const traceparent = { version }
const tracestate = TraceState.fromString(carrier.tracestate)
Expand Down
16 changes: 16 additions & 0 deletions packages/dd-trace/test/opentracing/propagation/text_map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ describe('TextMapPropagator', () => {
}
})

it('should not crash without spanContext', () => {
const carrier = {}
propagator.inject(null, carrier)
})

it('should not crash without carrier', () => {
const spanContext = createContext()
propagator.inject(spanContext, null)
})

it('should inject the span context into the carrier', () => {
const carrier = {}
const spanContext = createContext()
Expand Down Expand Up @@ -492,6 +502,12 @@ describe('TextMapPropagator', () => {
expect(first._spanId.toString(16)).to.equal(spanId)
})

it('should not crash with invalid traceparent', () => {
textMap.traceparent = 'invalid'

propagator.extract(textMap)
})

it('should always extract tracestate from tracecontext when trace IDs match', () => {
textMap.traceparent = '00-0000000000000000000000000000007B-0000000000000456-01'
textMap.tracestate = 'other=bleh,dd=t.foo_bar_baz_:abc_!@#$%^&*()_+`-~;s:2;o:foo;t.dm:-4'
Expand Down

0 comments on commit 25f4965

Please sign in to comment.