Skip to content

Commit

Permalink
Separate context propagation (OTEP 66) (open-telemetry#769)
Browse files Browse the repository at this point in the history
* feat: separate context propagation
  • Loading branch information
dyladan authored Feb 18, 2020
1 parent 22383f4 commit b32dce3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"webpack": "^4.35.2"
},
"dependencies": {
"@opentelemetry/api": "^0.4.0"
"@opentelemetry/api": "^0.4.0",
"@opentelemetry/core": "^0.4.0"
}
}
33 changes: 17 additions & 16 deletions src/JaegerHttpTraceFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/

import {
SpanContext,
Carrier,
Context,
HttpTextFormat,
SpanContext,
TraceFlags,
Carrier,
} from '@opentelemetry/api';
import {
getParentSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/core';

export const UBER_TRACE_ID_HEADER = 'uber-trace-id';

Expand Down Expand Up @@ -48,12 +53,10 @@ export class JaegerHttpTraceFormat implements HttpTextFormat {
this._jaegerTraceHeader = customTraceHeader || UBER_TRACE_ID_HEADER;
}

/**
* @param {SpanContext} spanContext - context from which we take information to inject in carrier.
* @param {string} format - unused.
* @param {Carrier} carrier - a carrier to which span information will be injected.
**/
inject(spanContext: SpanContext, format: string, carrier: Carrier) {
inject(context: Context, carrier: Carrier) {
const spanContext = getParentSpanContext(context);
if (!spanContext) return;

const traceFlags = `0${(
spanContext.traceFlags || TraceFlags.UNSAMPLED
).toString(16)}`;
Expand All @@ -63,19 +66,17 @@ export class JaegerHttpTraceFormat implements HttpTextFormat {
] = `${spanContext.traceId}:${spanContext.spanId}:0:${traceFlags}`;
}

/**
* @param {string} format - unused.
* @param {Carrier} carrier - a carrier from which span context will be constructed.
* @return {SpanContext} - returns a span context extracted from carrier.
**/
extract(format: string, carrier: Carrier): SpanContext | null {
extract(context: Context, carrier: Carrier): Context {
const uberTraceIdHeader = carrier[this._jaegerTraceHeader];
if (!uberTraceIdHeader) return null;
if (!uberTraceIdHeader) return context;
const uberTraceId = Array.isArray(uberTraceIdHeader)
? uberTraceIdHeader[0]
: uberTraceIdHeader;

return deserializeSpanContext(uberTraceId);
const spanContext = deserializeSpanContext(uberTraceId);
if (!spanContext) return context;

return setExtractedSpanContext(context, spanContext);
}
}

Expand Down
50 changes: 36 additions & 14 deletions test/JaegerHttpTraceFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
* limitations under the License.
*/

import { Context, SpanContext, TraceFlags } from '@opentelemetry/api';
import {
getExtractedSpanContext,
setExtractedSpanContext,
} from '@opentelemetry/core';
import * as assert from 'assert';
import {
JaegerHttpTraceFormat,
UBER_TRACE_ID_HEADER,
} from '../src/JaegerHttpTraceFormat';
import { SpanContext, TraceFlags } from '@opentelemetry/api';

describe('JaegerHttpTraceFormat', () => {
const jaegerHttpTraceFormat = new JaegerHttpTraceFormat();
Expand All @@ -39,7 +43,10 @@ describe('JaegerHttpTraceFormat', () => {
traceFlags: TraceFlags.SAMPLED,
};

jaegerHttpTraceFormat.inject(spanContext, '', carrier);
jaegerHttpTraceFormat.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext),
carrier
);
assert.deepStrictEqual(
carrier[UBER_TRACE_ID_HEADER],
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01'
Expand All @@ -53,7 +60,10 @@ describe('JaegerHttpTraceFormat', () => {
traceFlags: TraceFlags.SAMPLED,
};

customJaegerHttpTraceFormat.inject(spanContext, '', carrier);
customJaegerHttpTraceFormat.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext),
carrier
);
assert.deepStrictEqual(
carrier[customHeader],
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01'
Expand All @@ -65,7 +75,9 @@ describe('JaegerHttpTraceFormat', () => {
it('should extract context of a sampled span from carrier', () => {
carrier[UBER_TRACE_ID_HEADER] =
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01';
const extractedSpanContext = jaegerHttpTraceFormat.extract('', carrier);
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: '6e0c63257de34c92',
Expand All @@ -78,7 +90,9 @@ describe('JaegerHttpTraceFormat', () => {
it('should extract context of a sampled span from carrier with 1 bit flag', () => {
carrier[UBER_TRACE_ID_HEADER] =
'9c41e35aeb6d1272:45fd2a9709dadcf1:a13699e3fb724f40:1';
const extractedSpanContext = jaegerHttpTraceFormat.extract('', carrier);
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: '45fd2a9709dadcf1',
Expand All @@ -91,7 +105,9 @@ describe('JaegerHttpTraceFormat', () => {
it('should extract context of a sampled span from UTF-8 encoded carrier', () => {
carrier[UBER_TRACE_ID_HEADER] =
'ac1f3dc3c2c0b06e%3A5ac292c4a11a163e%3Ac086aaa825821068%3A1';
const extractedSpanContext = jaegerHttpTraceFormat.extract('', carrier);
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
);

assert.deepStrictEqual(extractedSpanContext, {
spanId: '5ac292c4a11a163e',
Expand All @@ -104,9 +120,8 @@ describe('JaegerHttpTraceFormat', () => {
it('should use custom header if provided', () => {
carrier[customHeader] =
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01';
const extractedSpanContext = customJaegerHttpTraceFormat.extract(
'',
carrier
const extractedSpanContext = getExtractedSpanContext(
customJaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
);

assert.deepStrictEqual(extractedSpanContext, {
Expand All @@ -117,15 +132,22 @@ describe('JaegerHttpTraceFormat', () => {
});
});

it('returns null if UBER_TRACE_ID_HEADER header is missing', () => {
assert.deepStrictEqual(jaegerHttpTraceFormat.extract('', carrier), null);
it('returns undefined if UBER_TRACE_ID_HEADER header is missing', () => {
assert.deepStrictEqual(
getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
),
undefined
);
});

it('returns null if UBER_TRACE_ID_HEADER header is invalid', () => {
it('returns undefined if UBER_TRACE_ID_HEADER header is invalid', () => {
carrier[UBER_TRACE_ID_HEADER] = 'invalid!';
assert.deepStrictEqual(
jaegerHttpTraceFormat.extract('HttpTraceContext', carrier),
null
getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
),
undefined
);
});
});
Expand Down

0 comments on commit b32dce3

Please sign in to comment.