From e6a05def9ee21cf9197869fb8c87f12dbbb7506b Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Sat, 16 Jan 2021 16:33:45 +0100 Subject: [PATCH 1/3] chore(http-instrumentation): reduce complexity of traceparent header parsing --- .../context/propagation/HttpTraceContext.ts | 40 +++++-------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts index 5957b2228d8..3532a9095d3 100644 --- a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts +++ b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts @@ -30,12 +30,7 @@ export const TRACE_PARENT_HEADER = 'traceparent'; export const TRACE_STATE_HEADER = 'tracestate'; const VERSION = '00'; -const VERSION_PART_COUNT = 4; // Version 00 only allows the specific 4 fields. - -const VERSION_REGEX = /^(?!ff)[\da-f]{2}$/; -const TRACE_ID_REGEX = /^(?![0]{32})[\da-f]{32}$/; -const PARENT_ID_REGEX = /^(?![0]{16})[\da-f]{16}$/; -const FLAGS_REGEX = /^[\da-f]{2}$/; +const TRACE_PARENT_REGEX = /^\s?((?!ff)[\da-f]{2})-((?![0]{32})[\da-f]{32})-((?![0]{16})[\da-f]{16})-([\da-f]{2})(-.*)?\s?$/; /** * Parses information from the [traceparent] span tag and converts it into {@link SpanContext} @@ -48,33 +43,18 @@ const FLAGS_REGEX = /^[\da-f]{2}$/; * For more information see {@link https://www.w3.org/TR/trace-context/} */ export function parseTraceParent(traceParent: string): SpanContext | null { - const trimmed = traceParent.trim(); - const traceParentParts = trimmed.split('-'); - - // Current version must be structured correctly. - // For future versions, we can grab just the parts we do support. - if ( - traceParentParts[0] === VERSION && - traceParentParts.length !== VERSION_PART_COUNT - ) { - return null; - } + const match = TRACE_PARENT_REGEX.exec(traceParent); + if (!match) return null; - const [version, traceId, parentId, flags] = traceParentParts; - const isValidParent = - VERSION_REGEX.test(version) && - TRACE_ID_REGEX.test(traceId) && - PARENT_ID_REGEX.test(parentId) && - FLAGS_REGEX.test(flags); - - if (!isValidParent) { - return null; - } + // According to the specification the implementation should be compatible + // with future versions. If there are more parts, we only reject it if it's using version 00 + // See https://www.w3.org/TR/trace-context/#versioning-of-traceparent + if (match[1] === '00' && match[5]) return null; return { - traceId: traceId, - spanId: parentId, - traceFlags: parseInt(flags, 16), + traceId: match[2], + spanId: match[3], + traceFlags: parseInt(match[4], 16), }; } From 5fb81c68bc48ca7a15ef1f39b1f0ea28819430be Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Mon, 18 Jan 2021 11:58:05 +0100 Subject: [PATCH 2/3] chore(http-instrumentation): split up the regex to make it more readable --- .../src/context/propagation/HttpTraceContext.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts index 3532a9095d3..8de8def9c56 100644 --- a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts +++ b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts @@ -30,7 +30,13 @@ export const TRACE_PARENT_HEADER = 'traceparent'; export const TRACE_STATE_HEADER = 'tracestate'; const VERSION = '00'; -const TRACE_PARENT_REGEX = /^\s?((?!ff)[\da-f]{2})-((?![0]{32})[\da-f]{32})-((?![0]{16})[\da-f]{16})-([\da-f]{2})(-.*)?\s?$/; +const VERSION_REGEX = '(?!ff)[\\da-f]{2}'; +const TRACE_ID_REGEX = '(?![0]{32})[\\da-f]{32}'; +const PARENT_ID_REGEX = '(?![0]{16})[\\da-f]{16}'; +const FLAGS_REGEX = '[\\da-f]{2}'; +const TRACE_PARENT_REGEX = new RegExp( + `^\\s?(${VERSION_REGEX})-(${TRACE_ID_REGEX})-(${PARENT_ID_REGEX})-(${FLAGS_REGEX})(-.*)?\\s?$` +); /** * Parses information from the [traceparent] span tag and converts it into {@link SpanContext} From e1d426c4ccb84aec742c6dd59c8492241d1e6a78 Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Wed, 20 Jan 2021 03:16:17 +0100 Subject: [PATCH 3/3] chore(http-instrumentation): rename the string regex variables to use _PART --- .../src/context/propagation/HttpTraceContext.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts index 8de8def9c56..735e8d2e443 100644 --- a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts +++ b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts @@ -30,12 +30,12 @@ export const TRACE_PARENT_HEADER = 'traceparent'; export const TRACE_STATE_HEADER = 'tracestate'; const VERSION = '00'; -const VERSION_REGEX = '(?!ff)[\\da-f]{2}'; -const TRACE_ID_REGEX = '(?![0]{32})[\\da-f]{32}'; -const PARENT_ID_REGEX = '(?![0]{16})[\\da-f]{16}'; -const FLAGS_REGEX = '[\\da-f]{2}'; +const VERSION_PART = '(?!ff)[\\da-f]{2}'; +const TRACE_ID_PART = '(?![0]{32})[\\da-f]{32}'; +const PARENT_ID_PART = '(?![0]{16})[\\da-f]{16}'; +const FLAGS_PART = '[\\da-f]{2}'; const TRACE_PARENT_REGEX = new RegExp( - `^\\s?(${VERSION_REGEX})-(${TRACE_ID_REGEX})-(${PARENT_ID_REGEX})-(${FLAGS_REGEX})(-.*)?\\s?$` + `^\\s?(${VERSION_PART})-(${TRACE_ID_PART})-(${PARENT_ID_PART})-(${FLAGS_PART})(-.*)?\\s?$` ); /**