Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: do not inject span context when instrumentation is suppressed #2082

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
setSpanContext,
SpanContext,
TextMapGetter,
Expand Down Expand Up @@ -73,7 +74,7 @@ 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) return;
if (!spanContext || isInstrumentationSuppressed(context)) return;

const traceParent = `${VERSION}-${spanContext.traceId}-${
spanContext.spanId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
TraceFlags,
getSpanContext,
setSpanContext,
suppressInstrumentation,
} from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
import * as assert from 'assert';
Expand Down Expand Up @@ -78,6 +79,23 @@ 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,
traceState: new TraceState('foo=bar,baz=qux'),
};

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

describe('.extract()', () => {
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