Skip to content

Commit

Permalink
Move suppress tracing context key to SDK (#2202)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
dyladan and vmarchaud authored May 20, 2021
1 parent 1745d87 commit c85fbe6
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 92 deletions.
1 change: 0 additions & 1 deletion packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"devDependencies": {
"@opentelemetry/api": "^1.0.0-rc.0",
"@opentelemetry/propagator-b3": "0.19.0",
"@types/mocha": "8.2.2",
"@types/node": "14.14.43",
"@types/semver": "7.3.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
*/

import {
Context,
BaggageEntry,
Context,
createBaggage,
getBaggage,
setBaggage,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
createBaggage,
isInstrumentationSuppressed,
TextMapSetter
} from '@opentelemetry/api';
import { getKeyPairs, serializeKeyPairs, parsePairKeyValue } from '../utils';
import { isTracingSuppressed } from '../../trace/suppress-tracing';
import {
BAGGAGE_MAX_NAME_VALUE_PAIRS,
BAGGAGE_ITEMS_SEPARATOR,
BAGGAGE_HEADER,
BAGGAGE_MAX_PER_NAME_VALUE_PAIRS,
BAGGAGE_ITEMS_SEPARATOR,
BAGGAGE_MAX_NAME_VALUE_PAIRS,
BAGGAGE_MAX_PER_NAME_VALUE_PAIRS
} from '../constants';
import {
getKeyPairs,
parsePairKeyValue,
serializeKeyPairs
} from '../utils';

/**
* Propagates {@link Baggage} through Context format propagation.
Expand All @@ -42,7 +46,7 @@ import {
export class HttpBaggagePropagator implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const baggage = getBaggage(context);
if (!baggage || isInstrumentationSuppressed(context)) return;
if (!baggage || isTracingSuppressed(context)) return;
const keyPairs = getKeyPairs(baggage)
.filter((pair: string) => {
return pair.length <= BAGGAGE_MAX_PER_NAME_VALUE_PAIRS;
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './trace/sampler/AlwaysOffSampler';
export * from './trace/sampler/AlwaysOnSampler';
export * from './trace/sampler/ParentBasedSampler';
export * from './trace/sampler/TraceIdRatioBasedSampler';
export * from './trace/suppress-tracing';
export * from './trace/TraceState';
export * from './utils/environment';
export * from './utils/sampling';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
setSpanContext,
SpanContext,
Expand All @@ -26,6 +25,7 @@ import {
TextMapSetter,
TraceFlags,
} from '@opentelemetry/api';
import { isTracingSuppressed } from './suppress-tracing';
import { TraceState } from './TraceState';

export const TRACE_PARENT_HEADER = 'traceparent';
Expand Down Expand Up @@ -77,7 +77,7 @@ export class HttpTraceContextPropagator implements TextMapPropagator {
const spanContext = getSpanContext(context);
if (
!spanContext ||
isInstrumentationSuppressed(context) ||
isTracingSuppressed(context) ||
!isSpanContextValid(spanContext)
)
return;
Expand Down
33 changes: 33 additions & 0 deletions packages/opentelemetry-core/src/trace/suppress-tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Context, createContextKey } from '@opentelemetry/api';

const SUPPRESS_TRACING_KEY = createContextKey(
'OpenTelemetry SDK Context Key SUPPRESS_TRACING'
);

export function suppressTracing(context: Context): Context {
return context.setValue(SUPPRESS_TRACING_KEY, true);
}

export function unsuppressTracing(context: Context): Context {
return context.deleteValue(SUPPRESS_TRACING_KEY);
}

export function isTracingSuppressed(context: Context): boolean {
return context.getValue(SUPPRESS_TRACING_KEY) === true;
}
42 changes: 21 additions & 21 deletions packages/opentelemetry-core/test/propagation/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
SpanContext,
getSpanContext,
setSpanContext,
TextMapGetter,
TextMapSetter,
} from '@opentelemetry/api';
import { Context, ROOT_CONTEXT } from '@opentelemetry/api';
import * as assert from 'assert';
Expand All @@ -29,19 +31,27 @@ import {
HttpTraceContextPropagator,
RandomIdGenerator,
} from '../../src';
import {
B3Propagator,
B3InjectEncoding,
X_B3_SAMPLED,
X_B3_SPAN_ID,
X_B3_TRACE_ID,
} from '@opentelemetry/propagator-b3';
import {
TRACE_PARENT_HEADER,
TRACE_STATE_HEADER,
} from '../../src/trace/HttpTraceContextPropagator';
import { TraceState } from '../../src/trace/TraceState';

class DummyPropagator implements TextMapPropagator {
inject(context: Context, carrier: any, setter: TextMapSetter<any>): void {
carrier['dummy'] = getSpanContext(context);
}
extract(context: Context, carrier: any, getter: TextMapGetter<any>): Context {
if (carrier['dummy']) {
return setSpanContext(context, carrier['dummy']);
}
return context;
}
fields(): string[] {
return ['dummy'];
}
}

describe('Composite Propagator', () => {
let traceId: string;
let spanId: string;
Expand Down Expand Up @@ -70,16 +80,11 @@ describe('Composite Propagator', () => {

it('should inject context using all configured propagators', () => {
const composite = new CompositePropagator({
propagators: [
new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }),
new HttpTraceContextPropagator(),
],
propagators: [new DummyPropagator(), new HttpTraceContextPropagator()],
});
composite.inject(ctxWithSpanContext, carrier, defaultTextMapSetter);

assert.strictEqual(carrier[X_B3_TRACE_ID], traceId);
assert.strictEqual(carrier[X_B3_SPAN_ID], spanId);
assert.strictEqual(carrier[X_B3_SAMPLED], '1');
assert.strictEqual(carrier['dummy'], spanContext);
assert.strictEqual(
carrier[TRACE_PARENT_HEADER],
`00-${traceId}-${spanId}-01`
Expand Down Expand Up @@ -108,20 +113,15 @@ describe('Composite Propagator', () => {

beforeEach(() => {
carrier = {
[X_B3_TRACE_ID]: traceId,
[X_B3_SPAN_ID]: spanId,
[X_B3_SAMPLED]: 1,
['dummy']: { traceId, spanId },
[TRACE_PARENT_HEADER]: `00-${traceId}-${spanId}-01`,
[TRACE_STATE_HEADER]: 'foo=bar',
};
});

it('should extract context using all configured propagators', () => {
const composite = new CompositePropagator({
propagators: [
new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }),
new HttpTraceContextPropagator(),
],
propagators: [new DummyPropagator(), new HttpTraceContextPropagator()],
});
const spanContext = getSpanContext(
composite.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
ROOT_CONTEXT,
setSpanContext,
SpanContext,
suppressInstrumentation,
TraceFlags,
} from '@opentelemetry/api';
import * as assert from 'assert';
Expand All @@ -32,6 +31,7 @@ import {
TRACE_PARENT_HEADER,
TRACE_STATE_HEADER,
} from '../../src/trace/HttpTraceContextPropagator';
import { suppressTracing } from '../../src/trace/suppress-tracing';
import { TraceState } from '../../src/trace/TraceState';

describe('HttpTraceContextPropagator', () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('HttpTraceContextPropagator', () => {
};

httpTraceContext.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
suppressTracing(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
Expand All @@ -107,7 +107,7 @@ describe('HttpTraceContextPropagator', () => {
};

httpTraceContext.inject(
suppressInstrumentation(setSpanContext(ROOT_CONTEXT, spanContext)),
suppressTracing(setSpanContext(ROOT_CONTEXT, spanContext)),
carrier,
defaultTextMapSetter
);
Expand Down
3 changes: 0 additions & 3 deletions packages/opentelemetry-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
"test/**/*.ts"
],
"references": [
{
"path": "../opentelemetry-propagator-b3"
},
{
"path": "../opentelemetry-semantic-conventions"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-instrumentation-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"devDependencies": {
"@opentelemetry/api": "^1.0.0-rc.0",
"@opentelemetry/context-async-hooks": "0.19.0",
"@opentelemetry/core": "0.19.0",
"@opentelemetry/node": "0.19.0",
"@opentelemetry/tracing": "0.19.0",
"@types/got": "9.6.11",
Expand Down Expand Up @@ -72,6 +71,7 @@
"@opentelemetry/api": "^1.0.0-rc.0"
},
"dependencies": {
"@opentelemetry/core": "0.19.0",
"@opentelemetry/instrumentation": "0.19.0",
"@opentelemetry/semantic-conventions": "0.19.0",
"semver": "^7.1.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-instrumentation-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
setSpan,
ROOT_CONTEXT,
getSpan,
suppressInstrumentation,
NOOP_TRACER,
diag,
} from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import type * as http from 'http';
import type * as https from 'https';
import { Socket } from 'net';
Expand Down Expand Up @@ -388,7 +388,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
(e: Error) => diag.error('caught ignoreIncomingPaths error: ', e)
)
) {
return context.with(suppressInstrumentation(context.active()), () => {
return context.with(suppressTracing(context.active()), () => {
context.bind(request);
context.bind(response);
return original.apply(this, [event, ...args]);
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-propagator-b3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"@opentelemetry/core": "^0.19.0"
},
"peerDependencies": {
"@opentelemetry/api": "^1.0.0-rc.0"
},
Expand Down
14 changes: 7 additions & 7 deletions packages/opentelemetry-propagator-b3/src/B3MultiPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
isValidSpanId,
isValidTraceId,
Expand All @@ -27,14 +26,15 @@ import {
TextMapSetter,
TraceFlags,
} from '@opentelemetry/api';
import { isTracingSuppressed } from '@opentelemetry/core';
import { B3_DEBUG_FLAG_KEY } from './common';
import {
X_B3_TRACE_ID,
X_B3_SPAN_ID,
X_B3_SAMPLED,
X_B3_PARENT_SPAN_ID,
X_B3_FLAGS,
X_B3_PARENT_SPAN_ID,
X_B3_SAMPLED,
X_B3_SPAN_ID,
X_B3_TRACE_ID,
} from './constants';
import { B3_DEBUG_FLAG_KEY } from './common';

const VALID_SAMPLED_VALUES = new Set([true, 'true', 'True', '1', 1]);
const VALID_UNSAMPLED_VALUES = new Set([false, 'false', 'False', '0', 0]);
Expand Down Expand Up @@ -99,7 +99,7 @@ export class B3MultiPropagator implements TextMapPropagator {
if (
!spanContext ||
!isSpanContextValid(spanContext) ||
isInstrumentationSuppressed(context)
isTracingSuppressed(context)
)
return;

Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-propagator-b3/src/B3Propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import {
Context,
isInstrumentationSuppressed,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
} from '@opentelemetry/api';
import { isTracingSuppressed } from '@opentelemetry/core';
import { B3MultiPropagator } from './B3MultiPropagator';
import { B3SinglePropagator } from './B3SinglePropagator';
import { B3_CONTEXT_HEADER } from './constants';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class B3Propagator implements TextMapPropagator {
}

inject(context: Context, carrier: unknown, setter: TextMapSetter) {
if (isInstrumentationSuppressed(context)) {
if (isTracingSuppressed(context)) {
return;
}
this._inject(context, carrier, setter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
Context,
getSpanContext,
isInstrumentationSuppressed,
isSpanContextValid,
isValidSpanId,
isValidTraceId,
Expand All @@ -27,8 +26,9 @@ import {
TextMapSetter,
TraceFlags,
} from '@opentelemetry/api';
import { B3_CONTEXT_HEADER } from './constants';
import { isTracingSuppressed } from '@opentelemetry/core';
import { B3_DEBUG_FLAG_KEY } from './common';
import { B3_CONTEXT_HEADER } from './constants';

const B3_CONTEXT_REGEX = /((?:[0-9a-f]{16}){1,2})-([0-9a-f]{16})(?:-([01d](?![0-9a-f])))?(?:-([0-9a-f]{16}))?/;
const PADDING = '0'.repeat(16);
Expand Down Expand Up @@ -56,7 +56,7 @@ export class B3SinglePropagator implements TextMapPropagator {
if (
!spanContext ||
!isSpanContextValid(spanContext) ||
isInstrumentationSuppressed(context)
isTracingSuppressed(context)
)
return;

Expand Down
Loading

0 comments on commit c85fbe6

Please sign in to comment.