diff --git a/benchmark/propagator.js b/benchmark/propagator.js index f40d4221b39..6f547743d14 100644 --- a/benchmark/propagator.js +++ b/benchmark/propagator.js @@ -34,10 +34,10 @@ for (const setup of setups) { api.setExtractedSpanContext(Context.ROOT_CONTEXT, { traceId: 'd4cda95b652f4a1592b449d5929fda1b', spanId: '6e0c63257de34c92' - }), setup.injectCarrier, api.defaultSetter); + }), setup.injectCarrier, api.defaultTextMapSetter); }) .add('#Extract', function () { - propagator.extract(Context.ROOT_CONTEXT, setup.extractCarrier, api.defaultGetter); + propagator.extract(Context.ROOT_CONTEXT, setup.extractCarrier, api.defaultTextMapGetter); }); // run async diff --git a/packages/opentelemetry-api/src/api/global-utils.ts b/packages/opentelemetry-api/src/api/global-utils.ts index 407399c8fbc..87791f2817a 100644 --- a/packages/opentelemetry-api/src/api/global-utils.ts +++ b/packages/opentelemetry-api/src/api/global-utils.ts @@ -65,4 +65,4 @@ export function makeGetter( * version. If the global API is not compatible with the API package * attempting to get it, a NOOP API implementation will be returned. */ -export const API_BACKWARDS_COMPATIBILITY_VERSION = 0; +export const API_BACKWARDS_COMPATIBILITY_VERSION = 1; diff --git a/packages/opentelemetry-api/src/api/propagation.ts b/packages/opentelemetry-api/src/api/propagation.ts index 3e39ab1ff0c..76b6ef57fa8 100644 --- a/packages/opentelemetry-api/src/api/propagation.ts +++ b/packages/opentelemetry-api/src/api/propagation.ts @@ -15,10 +15,14 @@ */ import { Context } from '@opentelemetry/context-base'; -import { defaultGetter, GetterFunction } from '../context/propagation/getter'; -import { TextMapPropagator } from '../context/propagation/TextMapPropagator'; import { NOOP_TEXT_MAP_PROPAGATOR } from '../context/propagation/NoopTextMapPropagator'; -import { defaultSetter, SetterFunction } from '../context/propagation/setter'; +import { + defaultTextMapGetter, + defaultTextMapSetter, + TextMapGetter, + TextMapPropagator, + TextMapSetter, +} from '../context/propagation/TextMapPropagator'; import { ContextAPI } from './context'; import { API_BACKWARDS_COMPATIBILITY_VERSION, @@ -74,7 +78,7 @@ export class PropagationAPI { */ public inject( carrier: Carrier, - setter: SetterFunction = defaultSetter, + setter: TextMapSetter = defaultTextMapSetter, context: Context = contextApi.active() ): void { return this._getGlobalPropagator().inject(context, carrier, setter); @@ -89,7 +93,7 @@ export class PropagationAPI { */ public extract( carrier: Carrier, - getter: GetterFunction = defaultGetter, + getter: TextMapGetter = defaultTextMapGetter, context: Context = contextApi.active() ): Context { return this._getGlobalPropagator().extract(context, carrier, getter); diff --git a/packages/opentelemetry-api/src/context/propagation/NoopTextMapPropagator.ts b/packages/opentelemetry-api/src/context/propagation/NoopTextMapPropagator.ts index c2155fadc98..06105496f61 100644 --- a/packages/opentelemetry-api/src/context/propagation/NoopTextMapPropagator.ts +++ b/packages/opentelemetry-api/src/context/propagation/NoopTextMapPropagator.ts @@ -22,9 +22,9 @@ import { TextMapPropagator } from './TextMapPropagator'; */ export class NoopTextMapPropagator implements TextMapPropagator { /** Noop inject function does nothing */ - inject(context: Context, carrier: unknown, setter: Function): void {} + inject(context: Context, carrier: unknown): void {} /** Noop extract function does nothing and returns the input context */ - extract(context: Context, carrier: unknown, getter: Function): Context { + extract(context: Context, carrier: unknown): Context { return context; } } diff --git a/packages/opentelemetry-api/src/context/propagation/TextMapPropagator.ts b/packages/opentelemetry-api/src/context/propagation/TextMapPropagator.ts index f56112495d7..52960612d86 100644 --- a/packages/opentelemetry-api/src/context/propagation/TextMapPropagator.ts +++ b/packages/opentelemetry-api/src/context/propagation/TextMapPropagator.ts @@ -15,8 +15,6 @@ */ import { Context } from '@opentelemetry/context-base'; -import { SetterFunction } from './setter'; -import { GetterFunction } from './getter'; /** * Injects `Context` into and extracts it from carriers that travel @@ -29,7 +27,7 @@ import { GetterFunction } from './getter'; * usually implemented via library-specific request interceptors, where the * client-side injects values and the server-side extracts them. */ -export interface TextMapPropagator { +export interface TextMapPropagator { /** * Injects values from a given `Context` into a carrier. * @@ -40,10 +38,14 @@ export interface TextMapPropagator { * the wire. * @param carrier the carrier of propagation fields, such as http request * headers. - * @param setter a function which accepts a carrier, key, and value, which - * sets the key on the carrier to the value. + * @param setter an optional {@link TextMapSetter}. If undefined, values will be + * set by direct object assignment. */ - inject(context: Context, carrier: unknown, setter: SetterFunction): void; + inject( + context: Context, + carrier: Carrier, + setter: TextMapSetter + ): void; /** * Given a `Context` and a carrier, extract context values from a @@ -54,8 +56,77 @@ export interface TextMapPropagator { * the wire. * @param carrier the carrier of propagation fields, such as http request * headers. - * @param getter a function which accepts a carrier and a key, and returns - * the value from the carrier identified by the key. + * @param getter an optional {@link TextMapGetter}. If undefined, keys will be all + * own properties, and keys will be accessed by direct object access. */ - extract(context: Context, carrier: unknown, getter: GetterFunction): Context; + extract( + context: Context, + carrier: Carrier, + getter: TextMapGetter + ): Context; } + +/** + * A setter is specified by the caller to define a specific method + * to set key/value pairs on the carrier within a propagator. + */ +export interface TextMapSetter { + /** + * Callback used to set a key/value pair on an object. + * + * Should be called by the propagator each time a key/value pair + * should be set, and should set that key/value pair on the propagator. + * + * @param carrier object or class which carries key/value pairs + * @param key string key to modify + * @param value value to be set to the key on the carrier + */ + set(carrier: Carrier, key: string, value: string): void; +} + +/** + * A getter is specified by the caller to define a specific method + * to get the value of a key from a carrier. + */ +export interface TextMapGetter { + /** + * Get a list of all keys available on the carrier. + * + * @param carrier + */ + keys(carrier: Carrier): string[]; + + /** + * Get the value of a specific key from the carrier. + * + * @param carrier + * @param key + */ + get(carrier: Carrier, key: string): undefined | string | string[]; +} + +export const defaultTextMapGetter: TextMapGetter = { + get(carrier, key) { + if (carrier == null) { + return undefined; + } + return carrier[key]; + }, + + keys(carrier) { + if (carrier == null) { + return []; + } + return Object.keys(carrier); + }, +}; + +export const defaultTextMapSetter: TextMapSetter = { + set(carrier, key, value) { + if (carrier == null) { + return; + } + + carrier[key] = value; + }, +}; diff --git a/packages/opentelemetry-api/src/context/propagation/getter.ts b/packages/opentelemetry-api/src/context/propagation/getter.ts deleted file mode 100644 index 75650107284..00000000000 --- a/packages/opentelemetry-api/src/context/propagation/getter.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -export type GetterFunction = ( - carrier: Carrier, - key: string -) => unknown; - -/** - * Default getter which just does a simple property access. Returns - * undefined if the key is not set. - * - * @param carrier - * @param key - */ -export function defaultGetter(carrier: any, key: string): unknown { - return carrier[key]; -} diff --git a/packages/opentelemetry-api/src/context/propagation/setter.ts b/packages/opentelemetry-api/src/context/propagation/setter.ts deleted file mode 100644 index 2eb56adf46f..00000000000 --- a/packages/opentelemetry-api/src/context/propagation/setter.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -export type SetterFunction = ( - carrier: Carrier, - key: string, - value: unknown -) => void; - -/** - * Default setter which sets value via direct property access - * - * @param carrier - * @param key - */ -export function defaultSetter(carrier: any, key: string, value: unknown) { - carrier[key] = value; -} diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index 02a257f0b52..e290b8a6c7e 100644 --- a/packages/opentelemetry-api/src/index.ts +++ b/packages/opentelemetry-api/src/index.ts @@ -18,10 +18,8 @@ export * from './common/Exception'; export * from './common/Logger'; export * from './common/Time'; export * from './context/context'; -export * from './context/propagation/getter'; export * from './context/propagation/TextMapPropagator'; export * from './context/propagation/NoopTextMapPropagator'; -export * from './context/propagation/setter'; export * from './correlation_context/CorrelationContext'; export * from './correlation_context/EntryValue'; export * from './metrics/BatchObserverResult'; diff --git a/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts b/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts index 159fae4c464..d93489d41db 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts +++ b/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts @@ -16,15 +16,15 @@ import { Context, - GetterFunction, - TextMapPropagator, - SetterFunction, - TraceFlags, + getParentSpanContext, + isSpanContextValid, isValidSpanId, isValidTraceId, - isSpanContextValid, - getParentSpanContext, setExtractedSpanContext, + TextMapGetter, + TextMapPropagator, + TextMapSetter, + TraceFlags, } from '@opentelemetry/api'; import { B3_DEBUG_FLAG_KEY } from './b3-common'; @@ -46,12 +46,12 @@ export function parseHeader(header: unknown) { return Array.isArray(header) ? header[0] : header; } -function getHeaderValue(carrier: unknown, getter: GetterFunction, key: string) { - const header = getter(carrier, key); +function getHeaderValue(carrier: unknown, getter: TextMapGetter, key: string) { + const header = getter.get(carrier, key); return parseHeader(header); } -function getTraceId(carrier: unknown, getter: GetterFunction): string { +function getTraceId(carrier: unknown, getter: TextMapGetter): string { const traceId = getHeaderValue(carrier, getter, X_B3_TRACE_ID); if (typeof traceId === 'string') { return traceId.padStart(32, '0'); @@ -59,7 +59,7 @@ function getTraceId(carrier: unknown, getter: GetterFunction): string { return ''; } -function getSpanId(carrier: unknown, getter: GetterFunction): string { +function getSpanId(carrier: unknown, getter: TextMapGetter): string { const spanId = getHeaderValue(carrier, getter, X_B3_SPAN_ID); if (typeof spanId === 'string') { return spanId; @@ -67,17 +67,14 @@ function getSpanId(carrier: unknown, getter: GetterFunction): string { return ''; } -function getDebug( - carrier: unknown, - getter: GetterFunction -): string | undefined { +function getDebug(carrier: unknown, getter: TextMapGetter): string | undefined { const debug = getHeaderValue(carrier, getter, X_B3_FLAGS); return debug === '1' ? '1' : undefined; } function getTraceFlags( carrier: unknown, - getter: GetterFunction + getter: TextMapGetter ): TraceFlags | undefined { const traceFlags = getHeaderValue(carrier, getter, X_B3_SAMPLED); const debug = getDebug(carrier, getter); @@ -96,21 +93,21 @@ function getTraceFlags( * Based on: /~https://github.com/openzipkin/b3-propagation */ export class B3MultiPropagator implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { const spanContext = getParentSpanContext(context); if (!spanContext || !isSpanContextValid(spanContext)) return; const debug = context.getValue(B3_DEBUG_FLAG_KEY); - setter(carrier, X_B3_TRACE_ID, spanContext.traceId); - setter(carrier, X_B3_SPAN_ID, spanContext.spanId); + setter.set(carrier, X_B3_TRACE_ID, spanContext.traceId); + setter.set(carrier, X_B3_SPAN_ID, spanContext.spanId); // According to the B3 spec, if the debug flag is set, // the sampled flag shouldn't be propagated as well. if (debug === '1') { - setter(carrier, X_B3_FLAGS, debug); + setter.set(carrier, X_B3_FLAGS, debug); } else if (spanContext.traceFlags !== undefined) { // We set the header only if there is an existing sampling decision. // Otherwise we will omit it => Absent. - setter( + setter.set( carrier, X_B3_SAMPLED, (TraceFlags.SAMPLED & spanContext.traceFlags) === TraceFlags.SAMPLED @@ -120,7 +117,7 @@ export class B3MultiPropagator implements TextMapPropagator { } } - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { const traceId = getTraceId(carrier, getter); const spanId = getSpanId(carrier, getter); const traceFlags = getTraceFlags(carrier, getter) as TraceFlags; diff --git a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts index 94974061ef1..a2b227f83d2 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts +++ b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts @@ -16,12 +16,12 @@ import { Context, - GetterFunction, + TextMapGetter, TextMapPropagator, - SetterFunction, + TextMapSetter, } from '@opentelemetry/api'; -import { B3SinglePropagator, B3_CONTEXT_HEADER } from './B3SinglePropagator'; import { B3MultiPropagator } from './B3MultiPropagator'; +import { B3SinglePropagator, B3_CONTEXT_HEADER } from './B3SinglePropagator'; /** Enumeraion of B3 inject encodings */ export enum B3InjectEncoding { @@ -47,7 +47,7 @@ export class B3Propagator implements TextMapPropagator { private readonly _inject: ( context: Context, carrier: unknown, - setter: SetterFunction + setter: TextMapSetter ) => void; constructor(config: B3PropagatorConfig = {}) { @@ -58,12 +58,12 @@ export class B3Propagator implements TextMapPropagator { } } - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { this._inject(context, carrier, setter); } - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { - if (getter(carrier, B3_CONTEXT_HEADER)) { + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { + if (getter.get(carrier, B3_CONTEXT_HEADER)) { return this._b3SinglePropagator.extract(context, carrier, getter); } else { return this._b3MultiPropagator.extract(context, carrier, getter); diff --git a/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts b/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts index 72f84d9ac4e..53444a1da9d 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts +++ b/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts @@ -16,15 +16,15 @@ import { Context, - GetterFunction, - TextMapPropagator, - SetterFunction, - TraceFlags, + getParentSpanContext, + isSpanContextValid, isValidSpanId, isValidTraceId, - isSpanContextValid, - getParentSpanContext, setExtractedSpanContext, + TextMapGetter, + TextMapPropagator, + TextMapSetter, + TraceFlags, } from '@opentelemetry/api'; import { B3_DEBUG_FLAG_KEY } from './b3-common'; @@ -52,18 +52,18 @@ function convertToTraceFlags(samplingState: string | undefined): TraceFlags { * Based on: /~https://github.com/openzipkin/b3-propagation */ export class B3SinglePropagator implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { const spanContext = getParentSpanContext(context); if (!spanContext || !isSpanContextValid(spanContext)) return; const samplingState = context.getValue(B3_DEBUG_FLAG_KEY) || spanContext.traceFlags & 0x1; const value = `${spanContext.traceId}-${spanContext.spanId}-${samplingState}`; - setter(carrier, B3_CONTEXT_HEADER, value); + setter.set(carrier, B3_CONTEXT_HEADER, value); } - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { - const header = getter(carrier, B3_CONTEXT_HEADER); + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { + const header = getter.get(carrier, B3_CONTEXT_HEADER); const b3Context = Array.isArray(header) ? header[0] : header; if (typeof b3Context !== 'string') return context; diff --git a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts index 3d196d803b4..88e7330f117 100644 --- a/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts +++ b/packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts @@ -16,13 +16,13 @@ import { Context, - GetterFunction, - TextMapPropagator, - SetterFunction, - SpanContext, - TraceFlags, getParentSpanContext, setExtractedSpanContext, + SpanContext, + TextMapGetter, + TextMapPropagator, + TextMapSetter, + TraceFlags, } from '@opentelemetry/api'; import { TraceState } from '../../trace/TraceState'; @@ -85,7 +85,7 @@ export function parseTraceParent(traceParent: string): SpanContext | null { * https://www.w3.org/TR/trace-context/ */ export class HttpTraceContext implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { const spanContext = getParentSpanContext(context); if (!spanContext) return; @@ -93,14 +93,18 @@ export class HttpTraceContext implements TextMapPropagator { spanContext.spanId }-0${Number(spanContext.traceFlags || TraceFlags.NONE).toString(16)}`; - setter(carrier, TRACE_PARENT_HEADER, traceParent); + setter.set(carrier, TRACE_PARENT_HEADER, traceParent); if (spanContext.traceState) { - setter(carrier, TRACE_STATE_HEADER, spanContext.traceState.serialize()); + setter.set( + carrier, + TRACE_STATE_HEADER, + spanContext.traceState.serialize() + ); } } - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { - const traceParentHeader = getter(carrier, TRACE_PARENT_HEADER); + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { + const traceParentHeader = getter.get(carrier, TRACE_PARENT_HEADER); if (!traceParentHeader) return context; const traceParent = Array.isArray(traceParentHeader) ? traceParentHeader[0] @@ -111,7 +115,7 @@ export class HttpTraceContext implements TextMapPropagator { spanContext.isRemote = true; - const traceStateHeader = getter(carrier, TRACE_STATE_HEADER); + const traceStateHeader = getter.get(carrier, TRACE_STATE_HEADER); if (traceStateHeader) { // If more than one `tracestate` header is found, we merge them into a // single header. diff --git a/packages/opentelemetry-core/src/context/propagation/composite.ts b/packages/opentelemetry-core/src/context/propagation/composite.ts index e0adfb585ec..67ef894b5f0 100644 --- a/packages/opentelemetry-core/src/context/propagation/composite.ts +++ b/packages/opentelemetry-core/src/context/propagation/composite.ts @@ -16,10 +16,10 @@ import { Context, - GetterFunction, + TextMapGetter, TextMapPropagator, Logger, - SetterFunction, + TextMapSetter, } from '@opentelemetry/api'; import { NoopLogger } from '../../common/NoopLogger'; import { CompositePropagatorConfig } from './types'; @@ -48,7 +48,7 @@ export class CompositePropagator implements TextMapPropagator { * @param context Context to inject * @param carrier Carrier into which context will be injected */ - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { for (const propagator of this._propagators) { try { propagator.inject(context, carrier, setter); @@ -69,7 +69,7 @@ export class CompositePropagator implements TextMapPropagator { * @param context Context to add values to * @param carrier Carrier from which to extract context */ - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { return this._propagators.reduce((ctx, propagator) => { try { return propagator.extract(ctx, carrier, getter); diff --git a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts index b46f6805a69..64d7de38e10 100644 --- a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts +++ b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts @@ -17,9 +17,9 @@ import { Context, CorrelationContext, - GetterFunction, + TextMapGetter, TextMapPropagator, - SetterFunction, + TextMapSetter, } from '@opentelemetry/api'; import { getCorrelationContext, @@ -50,7 +50,7 @@ type KeyPair = { * https://w3c.github.io/correlation-context/ */ export class HttpCorrelationContext implements TextMapPropagator { - inject(context: Context, carrier: unknown, setter: SetterFunction) { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { const correlationContext = getCorrelationContext(context); if (!correlationContext) return; const keyPairs = this._getKeyPairs(correlationContext) @@ -60,7 +60,7 @@ export class HttpCorrelationContext implements TextMapPropagator { .slice(0, MAX_NAME_VALUE_PAIRS); const headerValue = this._serializeKeyPairs(keyPairs); if (headerValue.length > 0) { - setter(carrier, CORRELATION_CONTEXT_HEADER, headerValue); + setter.set(carrier, CORRELATION_CONTEXT_HEADER, headerValue); } } @@ -80,8 +80,8 @@ export class HttpCorrelationContext implements TextMapPropagator { ); } - extract(context: Context, carrier: unknown, getter: GetterFunction): Context { - const headerValue: string = getter( + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { + const headerValue: string = getter.get( carrier, CORRELATION_CONTEXT_HEADER ) as string; diff --git a/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts b/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts index b50d3b4dc54..97149b4b893 100644 --- a/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts +++ b/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts @@ -15,15 +15,16 @@ */ import { - defaultGetter, - defaultSetter, - SpanContext, - TraceFlags, + defaultTextMapGetter, + defaultTextMapSetter, getActiveSpan, setExtractedSpanContext, + SpanContext, + TraceFlags, } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; +import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; import { B3MultiPropagator, X_B3_FLAGS, @@ -32,7 +33,6 @@ import { X_B3_SPAN_ID, X_B3_TRACE_ID, } from '../../src/context/propagation/B3MultiPropagator'; -import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; import { TraceState } from '../../src/trace/TraceState'; describe('B3MultiPropagator', () => { @@ -54,7 +54,7 @@ describe('B3MultiPropagator', () => { b3Propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[X_B3_TRACE_ID], @@ -77,7 +77,7 @@ describe('B3MultiPropagator', () => { b3Propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[X_B3_TRACE_ID], @@ -99,7 +99,7 @@ describe('B3MultiPropagator', () => { b3Propagator.inject( setExtractedSpanContext(contextWithDebug, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[X_B3_TRACE_ID], @@ -119,7 +119,7 @@ describe('B3MultiPropagator', () => { b3Propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, emptySpanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual(carrier[X_B3_TRACE_ID], undefined); assert.deepStrictEqual(carrier[X_B3_SPAN_ID], undefined); @@ -135,7 +135,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -156,7 +156,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -178,7 +178,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -200,7 +200,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -224,7 +224,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -249,7 +249,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -272,7 +272,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -295,7 +295,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -318,7 +318,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -339,7 +339,7 @@ describe('B3MultiPropagator', () => { carrier[X_B3_TRACE_ID] = undefined; carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; const context = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(context, undefined); }); @@ -350,7 +350,7 @@ describe('B3MultiPropagator', () => { carrier[X_B3_TRACE_ID] = '0af7651916cd43dd8448eb211c80319c'; carrier[X_B3_SPAN_ID] = undefined; const context = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(context, undefined); }); @@ -362,7 +362,7 @@ describe('B3MultiPropagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = '2'; const context = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(context, undefined); }); @@ -371,7 +371,7 @@ describe('B3MultiPropagator', () => { describe('AND b3 header is missing', () => { it('should return undefined', () => { const context = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(context, undefined); }); @@ -382,7 +382,7 @@ describe('B3MultiPropagator', () => { carrier[X_B3_TRACE_ID] = 'invalid!'; carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; const context = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(context, undefined); }); @@ -396,7 +396,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -448,28 +448,26 @@ describe('B3MultiPropagator', () => { Object.getOwnPropertyNames(testCases).forEach(testCase => { carrier[X_B3_TRACE_ID] = testCases[testCase]; const extractedSpanContext = getActiveSpan( - b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, undefined, testCase); }); }); it('should fail gracefully on bad responses from getter', () => { - const ctx1 = b3Propagator.extract( - ROOT_CONTEXT, - carrier, - (c, k) => 1 // not a number - ); - const ctx2 = b3Propagator.extract( - ROOT_CONTEXT, - carrier, - (c, k) => [] // empty array - ); - const ctx3 = b3Propagator.extract( - ROOT_CONTEXT, - carrier, - (c, k) => undefined // missing value - ); + const ctx1 = b3Propagator.extract(ROOT_CONTEXT, carrier, { + // @ts-expect-error + get: (c, k) => 1, // not a number + keys: defaultTextMapGetter.keys, + }); + const ctx2 = b3Propagator.extract(ROOT_CONTEXT, carrier, { + get: (c, k) => [], // empty array + keys: defaultTextMapGetter.keys, + }); + const ctx3 = b3Propagator.extract(ROOT_CONTEXT, carrier, { + get: (c, k) => undefined, // missing value + keys: defaultTextMapGetter.keys, + }); assert.ok(ctx1 === ROOT_CONTEXT); assert.ok(ctx2 === ROOT_CONTEXT); @@ -483,7 +481,7 @@ describe('B3MultiPropagator', () => { const context = b3Propagator.extract( ROOT_CONTEXT, carrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); diff --git a/packages/opentelemetry-core/test/context/B3Propagator.test.ts b/packages/opentelemetry-core/test/context/B3Propagator.test.ts index 68aa1453bd8..5964714bb8f 100644 --- a/packages/opentelemetry-core/test/context/B3Propagator.test.ts +++ b/packages/opentelemetry-core/test/context/B3Propagator.test.ts @@ -16,8 +16,8 @@ import * as assert from 'assert'; import { - defaultGetter, - defaultSetter, + defaultTextMapGetter, + defaultTextMapSetter, SpanContext, TraceFlags, getActiveSpan, @@ -57,7 +57,7 @@ describe('B3Propagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); const expected = '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1'; @@ -78,7 +78,7 @@ describe('B3Propagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.strictEqual( @@ -106,7 +106,7 @@ describe('B3Propagator', () => { const context = propagator.extract( ROOT_CONTEXT, b3SingleCarrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -122,7 +122,7 @@ describe('B3Propagator', () => { const context = propagator.extract( ROOT_CONTEXT, b3MultiCarrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); @@ -138,7 +138,7 @@ describe('B3Propagator', () => { const context = propagator.extract( ROOT_CONTEXT, b3MixedCarrier, - defaultGetter + defaultTextMapGetter ); const extractedSpanContext = getActiveSpan(context)?.context(); diff --git a/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts b/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts index 43ea3acbca0..60fcf051f0f 100644 --- a/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts +++ b/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts @@ -14,23 +14,23 @@ * limitations under the License. */ -import * as assert from 'assert'; import { - defaultGetter, - defaultSetter, - SpanContext, - TraceFlags, + defaultTextMapGetter, + defaultTextMapSetter, INVALID_SPANID, INVALID_TRACEID, getActiveSpan, setExtractedSpanContext, + SpanContext, + TraceFlags, } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; +import * as assert from 'assert'; +import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; import { B3SinglePropagator, B3_CONTEXT_HEADER, } from '../../src/context/propagation/B3SinglePropagator'; -import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; describe('B3SinglePropagator', () => { const propagator = new B3SinglePropagator(); @@ -51,7 +51,7 @@ describe('B3SinglePropagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); const expected = '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1'; @@ -68,7 +68,7 @@ describe('B3SinglePropagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); const expected = '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-0'; @@ -87,7 +87,7 @@ describe('B3SinglePropagator', () => { propagator.inject( setExtractedSpanContext(context, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); const expected = '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-d'; @@ -104,7 +104,7 @@ describe('B3SinglePropagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.strictEqual(carrier[B3_CONTEXT_HEADER], undefined); @@ -120,7 +120,7 @@ describe('B3SinglePropagator', () => { propagator.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.strictEqual(carrier[B3_CONTEXT_HEADER], undefined); @@ -134,7 +134,11 @@ describe('B3SinglePropagator', () => { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -151,7 +155,11 @@ describe('B3SinglePropagator', () => { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -168,7 +176,11 @@ describe('B3SinglePropagator', () => { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -184,7 +196,11 @@ describe('B3SinglePropagator', () => { [B3_CONTEXT_HEADER]: '4aaba1a52cf8ee09-e457b5a2e4d86bd1', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -201,7 +217,11 @@ describe('B3SinglePropagator', () => { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-d', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -218,7 +238,11 @@ describe('B3SinglePropagator', () => { [B3_CONTEXT_HEADER]: 'abc123-e457b5a2e4d86bd1', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(undefined, extractedSpanContext); @@ -229,7 +253,11 @@ describe('B3SinglePropagator', () => { [B3_CONTEXT_HEADER]: '80f198ee56343ba864fe8b2a57d3eff7-abc123', }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(undefined, extractedSpanContext); @@ -240,7 +268,11 @@ describe('B3SinglePropagator', () => { [B3_CONTEXT_HEADER]: `${INVALID_TRACEID}-e457b5a2e4d86bd1`, }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(undefined, extractedSpanContext); @@ -251,7 +283,11 @@ describe('B3SinglePropagator', () => { [B3_CONTEXT_HEADER]: `80f198ee56343ba864fe8b2a57d3eff7-${INVALID_SPANID}`, }; - const context = propagator.extract(ROOT_CONTEXT, carrier, defaultGetter); + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); const extractedSpanContext = getActiveSpan(context)?.context(); assert.deepStrictEqual(undefined, extractedSpanContext); diff --git a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts index 07f35065132..4bbe71a842c 100644 --- a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts +++ b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts @@ -15,8 +15,8 @@ */ import { - defaultGetter, - defaultSetter, + defaultTextMapGetter, + defaultTextMapSetter, SpanContext, TraceFlags, getActiveSpan, @@ -50,7 +50,7 @@ describe('HttpTraceContext', () => { httpTraceContext.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[TRACE_PARENT_HEADER], @@ -70,7 +70,7 @@ describe('HttpTraceContext', () => { httpTraceContext.inject( setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[TRACE_PARENT_HEADER], @@ -85,7 +85,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -100,7 +100,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -115,7 +115,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01-what-the-future-will-be-like'; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, { @@ -129,7 +129,7 @@ describe('HttpTraceContext', () => { it('returns null if traceparent header is missing', () => { assert.deepStrictEqual( getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(), undefined ); @@ -139,7 +139,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'invalid!'; assert.deepStrictEqual( getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(), undefined ); @@ -152,7 +152,7 @@ describe('HttpTraceContext', () => { assert.deepStrictEqual( getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(), undefined ); @@ -163,7 +163,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01', ]; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, { spanId: 'b7ad6b7169203331', @@ -178,7 +178,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = 'foo=bar,baz=qux'; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual( @@ -196,7 +196,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = ['foo=bar,baz=qux', 'quux=quuz']; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, { spanId: 'b7ad6b7169203331', @@ -250,7 +250,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = testCases[testCase]; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext, undefined, testCase); }); @@ -261,7 +261,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = 'foo=1 \t , \t bar=2, \t baz=3 '; const extractedSpanContext = getActiveSpan( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); assert.deepStrictEqual(extractedSpanContext!.traceState!.get('foo'), '1'); @@ -270,21 +270,19 @@ describe('HttpTraceContext', () => { }); it('should fail gracefully on bad responses from getter', () => { - const ctx1 = httpTraceContext.extract( - ROOT_CONTEXT, - carrier, - (c, k) => 1 // not a number - ); - const ctx2 = httpTraceContext.extract( - ROOT_CONTEXT, - carrier, - (c, k) => [] // empty array - ); - const ctx3 = httpTraceContext.extract( - ROOT_CONTEXT, - carrier, - (c, k) => undefined // missing value - ); + const ctx1 = httpTraceContext.extract(ROOT_CONTEXT, carrier, { + // @ts-expect-error + get: (c, k) => 1, // not a number + keys: () => [], + }); + const ctx2 = httpTraceContext.extract(ROOT_CONTEXT, carrier, { + get: (c, k) => [], // empty array + keys: () => [], + }); + const ctx3 = httpTraceContext.extract(ROOT_CONTEXT, carrier, { + get: (c, k) => undefined, // missing value + keys: () => [], + }); assert.ok(ctx1 === ROOT_CONTEXT); assert.ok(ctx2 === ROOT_CONTEXT); diff --git a/packages/opentelemetry-core/test/context/composite.test.ts b/packages/opentelemetry-core/test/context/composite.test.ts index ca22e012dbf..eccf66e9deb 100644 --- a/packages/opentelemetry-core/test/context/composite.test.ts +++ b/packages/opentelemetry-core/test/context/composite.test.ts @@ -15,8 +15,8 @@ */ import { - defaultGetter, - defaultSetter, + defaultTextMapGetter, + defaultTextMapSetter, TextMapPropagator, SpanContext, getActiveSpan, @@ -71,7 +71,7 @@ describe('Composite Propagator', () => { const composite = new CompositePropagator({ propagators: [new B3MultiPropagator(), new HttpTraceContext()], }); - composite.inject(ctxWithSpanContext, carrier, defaultSetter); + composite.inject(ctxWithSpanContext, carrier, defaultTextMapSetter); assert.strictEqual(carrier[X_B3_TRACE_ID], traceId); assert.strictEqual(carrier[X_B3_SPAN_ID], spanId); @@ -87,7 +87,7 @@ describe('Composite Propagator', () => { const composite = new CompositePropagator({ propagators: [new ThrowingPropagator(), new HttpTraceContext()], }); - composite.inject(ctxWithSpanContext, carrier, defaultSetter); + composite.inject(ctxWithSpanContext, carrier, defaultTextMapSetter); assert.strictEqual( carrier[TRACE_PARENT_HEADER], @@ -114,7 +114,7 @@ describe('Composite Propagator', () => { propagators: [new B3MultiPropagator(), new HttpTraceContext()], }); const spanContext = getActiveSpan( - composite.extract(ROOT_CONTEXT, carrier, defaultGetter) + composite.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); if (!spanContext) { @@ -133,7 +133,7 @@ describe('Composite Propagator', () => { propagators: [new ThrowingPropagator(), new HttpTraceContext()], }); const spanContext = getActiveSpan( - composite.extract(ROOT_CONTEXT, carrier, defaultGetter) + composite.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) )?.context(); if (!spanContext) { diff --git a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts index 247f7470322..63bd3c5ec7d 100644 --- a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts +++ b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts @@ -15,8 +15,8 @@ */ import { - defaultGetter, - defaultSetter, + defaultTextMapGetter, + defaultTextMapSetter, CorrelationContext, } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; @@ -51,7 +51,7 @@ describe('HttpCorrelationContext', () => { httpTraceContext.inject( setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[CORRELATION_CONTEXT_HEADER], @@ -72,7 +72,7 @@ describe('HttpCorrelationContext', () => { httpTraceContext.inject( setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual( carrier[CORRELATION_CONTEXT_HEADER], @@ -103,7 +103,7 @@ describe('HttpCorrelationContext', () => { httpTraceContext.inject( setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, - defaultSetter + defaultTextMapSetter ); assert.deepStrictEqual(carrier[CORRELATION_CONTEXT_HEADER], expected); }); @@ -114,7 +114,7 @@ describe('HttpCorrelationContext', () => { carrier[CORRELATION_CONTEXT_HEADER] = 'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm'; const extractedCorrelationContext = getCorrelationContext( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) ); const expected: CorrelationContext = { @@ -130,7 +130,7 @@ describe('HttpCorrelationContext', () => { it('returns undefined if header is missing', () => { assert.deepStrictEqual( getCorrelationContext( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) ), undefined ); @@ -145,7 +145,7 @@ describe('HttpCorrelationContext', () => { }; assert.deepStrictEqual( getCorrelationContext( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) ), expected ); @@ -188,7 +188,7 @@ describe('HttpCorrelationContext', () => { carrier[CORRELATION_CONTEXT_HEADER] = testCases[testCase].header; const extractedSpanContext = getCorrelationContext( - httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter) ); assert.deepStrictEqual( extractedSpanContext, diff --git a/packages/opentelemetry-plugin-fetch/src/fetch.ts b/packages/opentelemetry-plugin-fetch/src/fetch.ts index d3865ac0d4c..11815c899ad 100644 --- a/packages/opentelemetry-plugin-fetch/src/fetch.ts +++ b/packages/opentelemetry-plugin-fetch/src/fetch.ts @@ -109,9 +109,9 @@ export class FetchPlugin extends core.BasePlugin> { } if (options instanceof Request) { - api.propagation.inject(options.headers, (h, k, v) => - h.set(k, typeof v === 'string' ? v : String(v)) - ); + api.propagation.inject(options.headers, { + set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)), + }); } else { const headers: Partial> = {}; api.propagation.inject(headers); diff --git a/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts b/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts index 5443ebb3cd6..33fdc8659de 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts @@ -243,7 +243,7 @@ function getMetadata( * @param metadata */ export function setSpanContext(metadata: grpcJs.Metadata): void { - propagation.inject(metadata, (metadata, k, v) => - metadata.set(k, v as grpcJs.MetadataValue) - ); + propagation.inject(metadata, { + set: (metadata, k, v) => metadata.set(k, v as grpcJs.MetadataValue), + }); } diff --git a/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts b/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts index d68156676b9..867919934cd 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts @@ -101,9 +101,10 @@ export function patchServer( plugin.logger.debug('patch func: %s', JSON.stringify(spanOptions)); context.with( - propagation.extract(call.metadata, (carrier, key) => - carrier.get(key) - ), + propagation.extract(call.metadata, { + get: (carrier, key) => carrier.get(key).map(String), + keys: carrier => Object.keys(carrier.getMap()), + }), () => { const span = plugin.tracer .startSpan(spanName, spanOptions) diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index 9b52fd9430d..bc185dc777d 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -124,9 +124,9 @@ export class GrpcPlugin extends BasePlugin { } private _setSpanContext(metadata: grpcTypes.Metadata): void { - propagation.inject(metadata, (metadata, k, v) => - metadata.set(k, v as grpcTypes.MetadataValue) - ); + propagation.inject(metadata, { + set: (metadata, k, v) => metadata.set(k, v as grpcTypes.MetadataValue), + }); } private _patchServer() { @@ -182,9 +182,10 @@ export class GrpcPlugin extends BasePlugin { ); context.with( - propagation.extract(call.metadata, (carrier, key) => - carrier.get(key) - ), + propagation.extract(call.metadata, { + get: (metadata, key) => metadata.get(key).map(String), + keys: metadata => Object.keys(metadata.getMap()), + }), () => { const span = plugin._tracer .startSpan(spanName, spanOptions) diff --git a/packages/opentelemetry-shim-opentracing/src/shim.ts b/packages/opentelemetry-shim-opentracing/src/shim.ts index e44b42099ff..5b1364308a8 100644 --- a/packages/opentelemetry-shim-opentracing/src/shim.ts +++ b/packages/opentelemetry-shim-opentracing/src/shim.ts @@ -176,7 +176,7 @@ export class TracerShim extends opentracing.Tracer { case opentracing.FORMAT_TEXT_MAP: { api.propagation.inject( carrier, - api.defaultSetter, + api.defaultTextMapSetter, setCorrelationContext( api.setExtractedSpanContext(api.ROOT_CONTEXT, oTelSpanContext), oTelSpanCorrelationContext