Skip to content

Commit

Permalink
Add getter and setter arguments to propagation API (open-telemetry#827)
Browse files Browse the repository at this point in the history
* feat: add getter and setter arguments to propagation API

* chore: add getter and setter to composite propagator
  • Loading branch information
dyladan authored Mar 5, 2020
1 parent d0645f4 commit 09f881c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/JaegerHttpTraceFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/

import {
Carrier,
Context,
HttpTextFormat,
SpanContext,
TraceFlags,
SetterFunction,
GetterFunction,
} from '@opentelemetry/api';
import {
getParentSpanContext,
Expand Down Expand Up @@ -53,21 +54,23 @@ export class JaegerHttpTraceFormat implements HttpTextFormat {
this._jaegerTraceHeader = customTraceHeader || UBER_TRACE_ID_HEADER;
}

inject(context: Context, carrier: Carrier) {
inject(context: Context, carrier: unknown, setter: SetterFunction) {
const spanContext = getParentSpanContext(context);
if (!spanContext) return;

const traceFlags = `0${(
spanContext.traceFlags || TraceFlags.UNSAMPLED
).toString(16)}`;

carrier[
this._jaegerTraceHeader
] = `${spanContext.traceId}:${spanContext.spanId}:0:${traceFlags}`;
setter(
carrier,
this._jaegerTraceHeader,
`${spanContext.traceId}:${spanContext.spanId}:0:${traceFlags}`
);
}

extract(context: Context, carrier: Carrier): Context {
const uberTraceIdHeader = carrier[this._jaegerTraceHeader];
extract(context: Context, carrier: unknown, getter: GetterFunction): Context {
const uberTraceIdHeader = getter(carrier, this._jaegerTraceHeader);
if (!uberTraceIdHeader) return context;
const uberTraceId = Array.isArray(uberTraceIdHeader)
? uberTraceIdHeader[0]
Expand Down
50 changes: 41 additions & 9 deletions test/JaegerHttpTraceFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

import { Context, SpanContext, TraceFlags } from '@opentelemetry/api';
import {
Context,
defaultGetter,
defaultSetter,
SpanContext,
TraceFlags,
} from '@opentelemetry/api';
import {
getExtractedSpanContext,
setExtractedSpanContext,
Expand Down Expand Up @@ -45,7 +51,8 @@ describe('JaegerHttpTraceFormat', () => {

jaegerHttpTraceFormat.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext),
carrier
carrier,
defaultSetter
);
assert.deepStrictEqual(
carrier[UBER_TRACE_ID_HEADER],
Expand All @@ -62,7 +69,8 @@ describe('JaegerHttpTraceFormat', () => {

customJaegerHttpTraceFormat.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext),
carrier
carrier,
defaultSetter
);
assert.deepStrictEqual(
carrier[customHeader],
Expand All @@ -76,7 +84,11 @@ describe('JaegerHttpTraceFormat', () => {
carrier[UBER_TRACE_ID_HEADER] =
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01';
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
jaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
);

assert.deepStrictEqual(extractedSpanContext, {
Expand All @@ -91,7 +103,11 @@ describe('JaegerHttpTraceFormat', () => {
carrier[UBER_TRACE_ID_HEADER] =
'9c41e35aeb6d1272:45fd2a9709dadcf1:a13699e3fb724f40:1';
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
jaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
);

assert.deepStrictEqual(extractedSpanContext, {
Expand All @@ -106,7 +122,11 @@ describe('JaegerHttpTraceFormat', () => {
carrier[UBER_TRACE_ID_HEADER] =
'ac1f3dc3c2c0b06e%3A5ac292c4a11a163e%3Ac086aaa825821068%3A1';
const extractedSpanContext = getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
jaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
);

assert.deepStrictEqual(extractedSpanContext, {
Expand All @@ -121,7 +141,11 @@ describe('JaegerHttpTraceFormat', () => {
carrier[customHeader] =
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01';
const extractedSpanContext = getExtractedSpanContext(
customJaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
customJaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
);

assert.deepStrictEqual(extractedSpanContext, {
Expand All @@ -135,7 +159,11 @@ describe('JaegerHttpTraceFormat', () => {
it('returns undefined if UBER_TRACE_ID_HEADER header is missing', () => {
assert.deepStrictEqual(
getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
jaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
),
undefined
);
Expand All @@ -145,7 +173,11 @@ describe('JaegerHttpTraceFormat', () => {
carrier[UBER_TRACE_ID_HEADER] = 'invalid!';
assert.deepStrictEqual(
getExtractedSpanContext(
jaegerHttpTraceFormat.extract(Context.ROOT_CONTEXT, carrier)
jaegerHttpTraceFormat.extract(
Context.ROOT_CONTEXT,
carrier,
defaultGetter
)
),
undefined
);
Expand Down

0 comments on commit 09f881c

Please sign in to comment.