Skip to content

Commit

Permalink
chore: do not inject span context when instrumentation is suppressed (#…
Browse files Browse the repository at this point in the history
…2082)

* chore: do not inject span context when instrumentation is suppressed

* chore: review comments
  • Loading branch information
dyladan authored Apr 8, 2021
1 parent 80ff5e2 commit 64c8ef5
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TextMapSetter,
createBaggage,
baggageEntryMetadataFromString,
isInstrumentationSuppressed,
} from '@opentelemetry/api';

const KEY_PAIR_SEPARATOR = '=';
Expand All @@ -49,7 +50,7 @@ export const MAX_TOTAL_LENGTH = 8192;
export class HttpBaggage implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const baggage = getBaggage(context);
if (!baggage) return;
if (!baggage || isInstrumentationSuppressed(context)) return;
const keyPairs = this._getKeyPairs(baggage)
.filter((pair: string) => {
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
setSpanContext,
SpanContext,
Expand Down Expand Up @@ -74,7 +75,12 @@ 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 || !isSpanContextValid(spanContext)) return;
if (
!spanContext ||
isInstrumentationSuppressed(context) ||
!isSpanContextValid(spanContext)
)
return;

const traceParent = `${VERSION}-${spanContext.traceId}-${
spanContext.spanId
Expand Down
29 changes: 23 additions & 6 deletions packages/opentelemetry-core/test/context/HttpTraceContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import {
defaultTextMapGetter,
defaultTextMapSetter,
SpanContext,
TraceFlags,
getSpanContext,
setSpanContext,
INVALID_TRACEID,
INVALID_SPANID,
INVALID_TRACEID,
ROOT_CONTEXT,
setSpanContext,
SpanContext,
suppressInstrumentation,
TraceFlags,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
import * as assert from 'assert';
import {
HttpTraceContext,
Expand Down Expand Up @@ -81,6 +82,22 @@ describe('HttpTraceContext', () => {
assert.deepStrictEqual(carrier[TRACE_STATE_HEADER], 'foo=bar,baz=qux');
});

it('should not set traceparent and tracestate header if instrumentation is suppressed', () => {
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};

httpTraceContext.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
assert.strictEqual(carrier[TRACE_PARENT_HEADER], undefined);
assert.strictEqual(carrier[TRACE_STATE_HEADER], undefined);
});

it('should ignore invalid span context', () => {
const spanContext: SpanContext = {
traceId: INVALID_TRACEID,
Expand All @@ -90,7 +107,7 @@ describe('HttpTraceContext', () => {
};

httpTraceContext.inject(
setSpanContext(ROOT_CONTEXT, spanContext),
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
isValidSpanId,
isValidTraceId,
Expand Down Expand Up @@ -95,7 +96,12 @@ function getTraceFlags(
export class B3MultiPropagator implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const spanContext = getSpanContext(context);
if (!spanContext || !isSpanContextValid(spanContext)) return;
if (
!spanContext ||
!isSpanContextValid(spanContext) ||
isInstrumentationSuppressed(context)
)
return;

const debug = context.getValue(B3_DEBUG_FLAG_KEY);
setter.set(carrier, X_B3_TRACE_ID, spanContext.traceId);
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-propagator-b3/src/B3Propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {
Context,
isInstrumentationSuppressed,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
Expand Down Expand Up @@ -53,6 +54,9 @@ export class B3Propagator implements TextMapPropagator {
}

inject(context: Context, carrier: unknown, setter: TextMapSetter) {
if (isInstrumentationSuppressed(context)) {
return;
}
this._inject(context, carrier, setter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
isValidSpanId,
isValidTraceId,
Expand Down Expand Up @@ -52,7 +53,12 @@ function convertToTraceFlags(samplingState: string | undefined): TraceFlags {
export class B3SinglePropagator implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const spanContext = getSpanContext(context);
if (!spanContext || !isSpanContextValid(spanContext)) return;
if (
!spanContext ||
!isSpanContextValid(spanContext) ||
isInstrumentationSuppressed(context)
)
return;

const samplingState =
context.getValue(B3_DEBUG_FLAG_KEY) || spanContext.traceFlags & 0x1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getSpanContext,
setSpanContext,
SpanContext,
suppressInstrumentation,
TraceFlags,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
Expand Down Expand Up @@ -138,6 +139,23 @@ describe('B3MultiPropagator', () => {
assert.deepStrictEqual(carrier[X_B3_FLAGS], undefined);
assert.deepStrictEqual(carrier[X_B3_PARENT_SPAN_ID], undefined);
});

it('should not inject if instrumentation suppressed', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};
b3Propagator.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
assert.strictEqual(carrier[X_B3_TRACE_ID], undefined);
assert.strictEqual(carrier[X_B3_SPAN_ID], undefined);
assert.strictEqual(carrier[X_B3_FLAGS], undefined);
assert.strictEqual(carrier[X_B3_PARENT_SPAN_ID], undefined);
});
});

describe('.extract()', () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/opentelemetry-propagator-b3/test/B3Propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getSpanContext,
setSpanContext,
ROOT_CONTEXT,
suppressInstrumentation,
} from '@opentelemetry/api';
import { B3Propagator } from '../src/B3Propagator';
import { B3InjectEncoding } from '../src/types';
Expand Down Expand Up @@ -88,6 +89,24 @@ describe('B3Propagator', () => {
assert.strictEqual(carrier[X_B3_SPAN_ID], 'e457b5a2e4d86bd1');
assert.strictEqual(carrier[X_B3_SAMPLED], '1');
});

it('should not inject if instrumentation suppressed', () => {
propagator = new B3Propagator();

const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
spanId: 'e457b5a2e4d86bd1',
traceFlags: TraceFlags.SAMPLED,
};

propagator.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);

assert.strictEqual(carrier[B3_CONTEXT_HEADER], undefined);
});
});

describe('.extract()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
setSpanContext,
SpanContext,
TraceFlags,
suppressInstrumentation,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
import * as assert from 'assert';
Expand Down Expand Up @@ -123,6 +124,22 @@ describe('B3SinglePropagator', () => {

assert.strictEqual(carrier[B3_CONTEXT_HEADER], undefined);
});

it('does not inject if instrumentation is suppressed', () => {
const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
spanId: 'e457b5a2e4d86bd1',
traceFlags: TraceFlags.SAMPLED,
};

propagator.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);

assert.strictEqual(carrier[B3_CONTEXT_HEADER], undefined);
});
});

describe('.extract', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TextMapGetter,
TextMapPropagator,
TextMapSetter,
isInstrumentationSuppressed,
} from '@opentelemetry/api';

export const UBER_TRACE_ID_HEADER = 'uber-trace-id';
Expand Down Expand Up @@ -54,7 +55,7 @@ export class JaegerHttpTracePropagator implements TextMapPropagator {

inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const spanContext = getSpanContext(context);
if (!spanContext) return;
if (!spanContext || isInstrumentationSuppressed(context)) return;

const traceFlags = `0${(spanContext.traceFlags || TraceFlags.NONE).toString(
16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ROOT_CONTEXT,
setSpanContext,
SpanContext,
suppressInstrumentation,
TextMapGetter,
TraceFlags,
} from '@opentelemetry/api';
Expand Down Expand Up @@ -78,6 +79,21 @@ describe('JaegerHttpTracePropagator', () => {
'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01'
);
});

it('should not set uber trace id header if instrumentation suppressed', () => {
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
};

jaegerHttpTracePropagator.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
assert.strictEqual(carrier[UBER_TRACE_ID_HEADER], undefined);
});
});

describe('.extract()', () => {
Expand Down

0 comments on commit 64c8ef5

Please sign in to comment.