From 8afa8922198767972b0c2dbd1d6882ea49a3bce6 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 7 Apr 2021 11:24:47 -0400 Subject: [PATCH 1/2] fix: do not inject invalid span context --- .../src/context/propagation/HttpTraceContext.ts | 3 ++- .../test/context/HttpTraceContext.test.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts index 735e8d2e44..8b4dac8216 100644 --- a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts +++ b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts @@ -17,6 +17,7 @@ import { Context, getSpanContext, + isSpanContextValid, setSpanContext, SpanContext, TextMapGetter, @@ -73,7 +74,7 @@ export function parseTraceParent(traceParent: string): SpanContext | null { export class HttpTraceContext implements TextMapPropagator { inject(context: Context, carrier: unknown, setter: TextMapSetter) { const spanContext = getSpanContext(context); - if (!spanContext) return; + if (!spanContext || !isSpanContextValid(spanContext)) return; const traceParent = `${VERSION}-${spanContext.traceId}-${ spanContext.spanId diff --git a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts index a732a471f9..1f0816a9ce 100644 --- a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts +++ b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts @@ -78,6 +78,23 @@ describe('HttpTraceContext', () => { ); assert.deepStrictEqual(carrier[TRACE_STATE_HEADER], 'foo=bar,baz=qux'); }); + + it('should ignore invalid span context', () => { + const spanContext: SpanContext = { + traceId: '00000000000000000000000000000000', + spanId: '0000000000000000', + traceFlags: TraceFlags.NONE, + traceState: new TraceState('foo=bar,baz=qux'), + }; + + httpTraceContext.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + assert.deepStrictEqual(carrier[TRACE_PARENT_HEADER], undefined); + assert.deepStrictEqual(carrier[TRACE_STATE_HEADER], undefined); + }); }); describe('.extract()', () => { From 52dbc70a1fcfc473aaceb439f6dd426e6a74b5ab Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 8 Apr 2021 08:27:33 -0400 Subject: [PATCH 2/2] chore: review comments --- .../test/context/HttpTraceContext.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts index 1f0816a9ce..831a3899df 100644 --- a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts +++ b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts @@ -21,6 +21,8 @@ import { TraceFlags, getSpanContext, setSpanContext, + INVALID_TRACEID, + INVALID_SPANID, } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/api'; import * as assert from 'assert'; @@ -81,8 +83,8 @@ describe('HttpTraceContext', () => { it('should ignore invalid span context', () => { const spanContext: SpanContext = { - traceId: '00000000000000000000000000000000', - spanId: '0000000000000000', + traceId: INVALID_TRACEID, + spanId: INVALID_SPANID, traceFlags: TraceFlags.NONE, traceState: new TraceState('foo=bar,baz=qux'), }; @@ -92,8 +94,8 @@ describe('HttpTraceContext', () => { carrier, defaultTextMapSetter ); - assert.deepStrictEqual(carrier[TRACE_PARENT_HEADER], undefined); - assert.deepStrictEqual(carrier[TRACE_STATE_HEADER], undefined); + assert.strictEqual(carrier[TRACE_PARENT_HEADER], undefined); + assert.strictEqual(carrier[TRACE_STATE_HEADER], undefined); }); });