Skip to content

Commit

Permalink
fix(opentelemetry-core): modify regex to allow future versions
Browse files Browse the repository at this point in the history
  • Loading branch information
srjames90 committed Jul 1, 2020
1 parent 5aa851a commit 694c3ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { getParentSpanContext, setExtractedSpanContext } from '../context';

export const TRACE_PARENT_HEADER = 'traceparent';
export const TRACE_STATE_HEADER = 'tracestate';
const VALID_TRACE_PARENT_REGEX = /^00-([\da-f]{32})-([\da-f]{16})-([\da-f]{2})$/;
const VALID_TRACE_PARENT_REGEX = /^(?!ff)[\da-f]{2}-([\da-f]{32})-([\da-f]{16})-([\da-f]{2})(-|$)/;
const VERSION = '00';

/**
Expand Down
36 changes: 34 additions & 2 deletions packages/opentelemetry-core/test/context/HttpTraceContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ describe('HttpTraceContext', () => {
});
});

it('should extract context of a sampled span from carrier using a future version', () => {
carrier[TRACE_PARENT_HEADER] =
'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
const extractedSpanContext = getExtractedSpanContext(
httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});

it('should extract context of a sampled span from carrier using a future version and future fields', () => {
carrier[TRACE_PARENT_HEADER] =
'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01-what-the-future-will-be-like';
const extractedSpanContext = getExtractedSpanContext(
httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});

it('returns null if traceparent header is missing', () => {
assert.deepStrictEqual(
getExtractedSpanContext(
Expand Down Expand Up @@ -173,8 +203,6 @@ describe('HttpTraceContext', () => {

const testCases: Record<string, string> = {
invalidParts_tooShort: '00-ffffffffffffffffffffffffffffffff',
invalidParts_tooLong:
'00-ffffffffffffffffffffffffffffffff-ffffffffffffffff-00-01',

invalidVersion_notHex:
'0x-ffffffffffffffffffffffffffffffff-ffffffffffffffff-00',
Expand All @@ -201,6 +229,10 @@ describe('HttpTraceContext', () => {
'00-ffffffffffffffffffffffffffffffff-ffffffff-01',
invalidSpanId_tooLong:
'00-ffffffffffffffffffffffffffffffff-ffffffffffffffff0000-01',
invalidFutureVersion:
'ff-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01',
invalidFutureFieldAfterFlag:
'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01.bad-future-is-imminent-for-you-jk',
};

Object.getOwnPropertyNames(testCases).forEach(testCase => {
Expand Down

0 comments on commit 694c3ea

Please sign in to comment.