diff --git a/packages/opentelemetry-api/src/api/propagation.ts b/packages/opentelemetry-api/src/api/propagation.ts index 92a046a659..3e39ab1ff0 100644 --- a/packages/opentelemetry-api/src/api/propagation.ts +++ b/packages/opentelemetry-api/src/api/propagation.ts @@ -75,7 +75,7 @@ export class PropagationAPI { public inject( carrier: Carrier, setter: SetterFunction = defaultSetter, - context = contextApi.active() + context: Context = contextApi.active() ): void { return this._getGlobalPropagator().inject(context, carrier, setter); } @@ -90,7 +90,7 @@ export class PropagationAPI { public extract( carrier: Carrier, getter: GetterFunction = defaultGetter, - context = contextApi.active() + context: Context = contextApi.active() ): Context { return this._getGlobalPropagator().extract(context, carrier, getter); } diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index 0a7e8bbd85..d7e99d2efd 100644 --- a/packages/opentelemetry-api/src/index.ts +++ b/packages/opentelemetry-api/src/index.ts @@ -61,7 +61,12 @@ export { INVALID_SPAN_CONTEXT, } from './trace/spancontext-utils'; -export { Context } from '@opentelemetry/context-base'; +export { + Context, + ROOT_CONTEXT, + createContextKey, + ContextManager, +} from '@opentelemetry/context-base'; import { ContextAPI } from './api/context'; /** Entrypoint for context API */ diff --git a/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts b/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts index 05b9fd9875..7b2d79593c 100644 --- a/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts +++ b/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Context } from '@opentelemetry/context-base'; +import { Context, ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as asyncHooks from 'async_hooks'; import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager'; @@ -35,7 +35,7 @@ export class AsyncHooksContextManager extends AbstractAsyncHooksContextManager { } active(): Context { - return this._stack[this._stack.length - 1] ?? Context.ROOT_CONTEXT; + return this._stack[this._stack.length - 1] ?? ROOT_CONTEXT; } with ReturnType>( diff --git a/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts b/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts index c16c323439..1626af9dc5 100644 --- a/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts +++ b/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Context } from '@opentelemetry/context-base'; +import { Context, ROOT_CONTEXT } from '@opentelemetry/context-base'; import { AsyncLocalStorage } from 'async_hooks'; import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager'; @@ -27,7 +27,7 @@ export class AsyncLocalStorageContextManager extends AbstractAsyncHooksContextMa } active(): Context { - return this._asyncLocalStorage.getStore() ?? Context.ROOT_CONTEXT; + return this._asyncLocalStorage.getStore() ?? ROOT_CONTEXT; } with ReturnType>( diff --git a/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts b/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts index 6a2fbf0507..4e9c0b19b6 100644 --- a/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts +++ b/packages/opentelemetry-context-async-hooks/test/AsyncHooksContextManager.test.ts @@ -20,7 +20,7 @@ import { AsyncLocalStorageContextManager, } from '../src'; import { EventEmitter } from 'events'; -import { Context } from '@opentelemetry/context-base'; +import { createContextKey, ROOT_CONTEXT } from '@opentelemetry/context-base'; for (const contextManagerClass of [ AsyncHooksContextManager, @@ -30,7 +30,7 @@ for (const contextManagerClass of [ let contextManager: | AsyncHooksContextManager | AsyncLocalStorageContextManager; - const key1 = Context.createKey('test key 1'); + const key1 = createContextKey('test key 1'); before(function () { if ( @@ -77,11 +77,11 @@ for (const contextManagerClass of [ describe('.with()', () => { it('should run the callback (null as target)', done => { - contextManager.with(Context.ROOT_CONTEXT, done); + contextManager.with(ROOT_CONTEXT, done); }); it('should run the callback (object as target)', done => { - const test = Context.ROOT_CONTEXT.setValue(key1, 1); + const test = ROOT_CONTEXT.setValue(key1, 1); contextManager.with(test, () => { assert.strictEqual( contextManager.active(), @@ -94,7 +94,7 @@ for (const contextManagerClass of [ it('should run the callback (when disabled)', done => { contextManager.disable(); - contextManager.with(Context.ROOT_CONTEXT, () => { + contextManager.with(ROOT_CONTEXT, () => { contextManager.enable(); return done(); }); @@ -102,7 +102,7 @@ for (const contextManagerClass of [ it('should rethrow errors', done => { assert.throws(() => { - contextManager.with(Context.ROOT_CONTEXT, () => { + contextManager.with(ROOT_CONTEXT, () => { throw new Error('This should be rethrown'); }); }); @@ -110,8 +110,8 @@ for (const contextManagerClass of [ }); it('should finally restore an old context', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1'); - const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 'ctx2'); + const ctx1 = ROOT_CONTEXT.setValue(key1, 'ctx1'); + const ctx2 = ROOT_CONTEXT.setValue(key1, 'ctx2'); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); contextManager.with(ctx2, () => { @@ -123,7 +123,7 @@ for (const contextManagerClass of [ }); it('should finally restore an old context', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1'); + const ctx1 = ROOT_CONTEXT.setValue(key1, 'ctx1'); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); setTimeout(() => { @@ -150,7 +150,7 @@ for (const contextManagerClass of [ ); assert.strictEqual(contextManager.active(), scope1); }); - assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT); + assert.strictEqual(contextManager.active(), ROOT_CONTEXT); }); it('should not loose the context', done => { @@ -162,7 +162,7 @@ for (const contextManagerClass of [ assert.strictEqual(contextManager.active(), scope1); return done(); }); - assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT); + assert.strictEqual(contextManager.active(), ROOT_CONTEXT); }); it('should correctly restore context using async/await', async () => { @@ -186,7 +186,7 @@ for (const contextManagerClass of [ }); assert.strictEqual(contextManager.active(), scope1); }); - assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT); + assert.strictEqual(contextManager.active(), ROOT_CONTEXT); }); it('should works with multiple concurrent operations', done => { @@ -208,7 +208,7 @@ for (const contextManagerClass of [ }, 100); assert.strictEqual(contextManager.active(), scope1); }); - assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT); + assert.strictEqual(contextManager.active(), ROOT_CONTEXT); contextManager.with(scope2, async () => { assert.strictEqual(contextManager.active(), scope2); setTimeout(() => { @@ -220,7 +220,7 @@ for (const contextManagerClass of [ }, 20); assert.strictEqual(contextManager.active(), scope2); }); - assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT); + assert.strictEqual(contextManager.active(), ROOT_CONTEXT); }); it('should work with timers using the same timeout', done => { @@ -255,24 +255,18 @@ for (const contextManagerClass of [ describe('.bind(function)', () => { it('should return the same target (when enabled)', () => { const test = { a: 1 }; - assert.deepStrictEqual( - contextManager.bind(test, Context.ROOT_CONTEXT), - test - ); + assert.deepStrictEqual(contextManager.bind(test, ROOT_CONTEXT), test); }); it('should return the same target (when disabled)', () => { contextManager.disable(); const test = { a: 1 }; - assert.deepStrictEqual( - contextManager.bind(test, Context.ROOT_CONTEXT), - test - ); + assert.deepStrictEqual(contextManager.bind(test, ROOT_CONTEXT), test); contextManager.enable(); }); it('should return current context (when enabled)', done => { - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const fn = contextManager.bind(() => { assert.strictEqual( contextManager.active(), @@ -290,7 +284,7 @@ for (const contextManagerClass of [ */ it('should return current context (when disabled)', done => { contextManager.disable(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const fn = contextManager.bind(() => { assert.strictEqual( contextManager.active(), @@ -303,7 +297,7 @@ for (const contextManagerClass of [ }); it('should fail to return current context with async op', done => { - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const fn = contextManager.bind(() => { assert.strictEqual(contextManager.active(), context); setTimeout(() => { @@ -322,24 +316,18 @@ for (const contextManagerClass of [ describe('.bind(event-emitter)', () => { it('should return the same target (when enabled)', () => { const ee = new EventEmitter(); - assert.deepStrictEqual( - contextManager.bind(ee, Context.ROOT_CONTEXT), - ee - ); + assert.deepStrictEqual(contextManager.bind(ee, ROOT_CONTEXT), ee); }); it('should return the same target (when disabled)', () => { const ee = new EventEmitter(); contextManager.disable(); - assert.deepStrictEqual( - contextManager.bind(ee, Context.ROOT_CONTEXT), - ee - ); + assert.deepStrictEqual(contextManager.bind(ee, ROOT_CONTEXT), ee); }); it('should return current context and removeListener (when enabled)', done => { const ee = new EventEmitter(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const patchedEe = contextManager.bind(ee, context); const handler = () => { assert.deepStrictEqual(contextManager.active(), context); @@ -354,7 +342,7 @@ for (const contextManagerClass of [ it('should return current context and removeAllListener (when enabled)', done => { const ee = new EventEmitter(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const patchedEe = contextManager.bind(ee, context); const handler = () => { assert.deepStrictEqual(contextManager.active(), context); @@ -374,7 +362,7 @@ for (const contextManagerClass of [ it('should return context (when disabled)', done => { contextManager.disable(); const ee = new EventEmitter(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const patchedEe = contextManager.bind(ee, context); const handler = () => { assert.deepStrictEqual(contextManager.active(), context); @@ -389,7 +377,7 @@ for (const contextManagerClass of [ it('should not return current context with async op', done => { const ee = new EventEmitter(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const patchedEe = contextManager.bind(ee, context); const handler = () => { assert.deepStrictEqual(contextManager.active(), context); diff --git a/packages/opentelemetry-context-base/src/NoopContextManager.ts b/packages/opentelemetry-context-base/src/NoopContextManager.ts index e02816f221..fd2e9f2121 100644 --- a/packages/opentelemetry-context-base/src/NoopContextManager.ts +++ b/packages/opentelemetry-context-base/src/NoopContextManager.ts @@ -14,22 +14,22 @@ * limitations under the License. */ +import { ROOT_CONTEXT } from './context'; import * as types from './types'; -import { Context } from './context'; export class NoopContextManager implements types.ContextManager { - active(): Context { - return Context.ROOT_CONTEXT; + active(): types.Context { + return ROOT_CONTEXT; } with ReturnType>( - context: Context, + context: types.Context, fn: T ): ReturnType { return fn(); } - bind(target: T, context?: Context): T { + bind(target: T, context?: types.Context): T { return target; } diff --git a/packages/opentelemetry-context-base/src/context.ts b/packages/opentelemetry-context-base/src/context.ts index 2a6804312c..3010e4409f 100644 --- a/packages/opentelemetry-context-base/src/context.ts +++ b/packages/opentelemetry-context-base/src/context.ts @@ -13,31 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export class Context { - private _currentContext: Map; - - /** The root context is used as the default parent context when there is no active context */ - public static readonly ROOT_CONTEXT = new Context(); - /** - * This is another identifier to the root context which allows developers to easily search the - * codebase for direct uses of context which need to be removed in later PRs. - * - * It's existence is temporary and it should be removed when all references are fixed. - */ - public static readonly TODO = Context.ROOT_CONTEXT; +import { Context } from './types'; - /** Get a key to uniquely identify a context value */ - public static createKey(description: string) { - return Symbol.for(description); - } +export class BaseContext implements Context { + private _currentContext: Map; /** * Construct a new context which inherits values from an optional parent context. * * @param parentContext a context from which to inherit values */ - private constructor(parentContext?: Map) { + constructor(parentContext?: Map) { this._currentContext = parentContext ? new Map(parentContext) : new Map(); } @@ -58,7 +45,7 @@ export class Context { * @param value value to set for the given key */ setValue(key: symbol, value: unknown): Context { - const context = new Context(this._currentContext); + const context = new BaseContext(this._currentContext); context._currentContext.set(key, value); return context; } @@ -70,8 +57,16 @@ export class Context { * @param key context key for which to clear a value */ deleteValue(key: symbol): Context { - const context = new Context(this._currentContext); + const context = new BaseContext(this._currentContext); context._currentContext.delete(key); return context; } } + +/** The root context is used as the default parent context when there is no active context */ +export const ROOT_CONTEXT: Context = new BaseContext(); + +/** Get a key to uniquely identify a context value */ +export function createContextKey(description: string) { + return Symbol.for(description); +} diff --git a/packages/opentelemetry-context-base/src/index.ts b/packages/opentelemetry-context-base/src/index.ts index d9e53ab004..0957a63910 100644 --- a/packages/opentelemetry-context-base/src/index.ts +++ b/packages/opentelemetry-context-base/src/index.ts @@ -14,6 +14,6 @@ * limitations under the License. */ -export * from './types'; -export * from './context'; +export { createContextKey, ROOT_CONTEXT } from './context'; export * from './NoopContextManager'; +export * from './types'; diff --git a/packages/opentelemetry-context-base/src/types.ts b/packages/opentelemetry-context-base/src/types.ts index e58a4990cf..20922441a7 100644 --- a/packages/opentelemetry-context-base/src/types.ts +++ b/packages/opentelemetry-context-base/src/types.ts @@ -14,7 +14,31 @@ * limitations under the License. */ -import { Context } from './context'; +export interface Context { + /** + * Get a value from the context. + * + * @param key key which identifies a context value + */ + getValue(key: symbol): unknown; + + /** + * Create a new context which inherits from this context and has + * the given key set to the given value. + * + * @param key context key for which to set the value + * @param value value to set for the given key + */ + setValue(key: symbol, value: unknown): Context; + + /** + * Return a new context which inherits from this context but does + * not contain a value for the given key. + * + * @param key context key for which to clear a value + */ + deleteValue(key: symbol): Context; +} export interface ContextManager { /** diff --git a/packages/opentelemetry-context-base/test/NoopContextManager.test.ts b/packages/opentelemetry-context-base/test/NoopContextManager.test.ts index b35f979799..61f9ae7355 100644 --- a/packages/opentelemetry-context-base/test/NoopContextManager.test.ts +++ b/packages/opentelemetry-context-base/test/NoopContextManager.test.ts @@ -15,7 +15,8 @@ */ import * as assert from 'assert'; -import { NoopContextManager, Context } from '../src'; +import { NoopContextManager, ROOT_CONTEXT } from '../src'; +import { createContextKey } from '../src/context'; describe('NoopContextManager', () => { let contextManager: NoopContextManager; @@ -45,17 +46,17 @@ describe('NoopContextManager', () => { }); describe('.with()', () => { - it('should run the callback (Context.ROOT_CONTEXT as target)', done => { - contextManager.with(Context.ROOT_CONTEXT, done); + it('should run the callback (ROOT_CONTEXT as target)', done => { + contextManager.with(ROOT_CONTEXT, done); }); it('should run the callback (object as target)', done => { - const key = Context.createKey('test key 1'); - const test = Context.ROOT_CONTEXT.setValue(key, 1); + const key = createContextKey('test key 1'); + const test = ROOT_CONTEXT.setValue(key, 1); contextManager.with(test, () => { assert.strictEqual( contextManager.active(), - Context.ROOT_CONTEXT, + ROOT_CONTEXT, 'should not have context' ); return done(); @@ -64,7 +65,7 @@ describe('NoopContextManager', () => { it('should run the callback (when disabled)', done => { contextManager.disable(); - contextManager.with(Context.ROOT_CONTEXT, () => { + contextManager.with(ROOT_CONTEXT, () => { contextManager.enable(); return done(); }); @@ -72,19 +73,19 @@ describe('NoopContextManager', () => { }); describe('.active()', () => { - it('should always return Context.ROOT_CONTEXT (when enabled)', () => { + it('should always return ROOT_CONTEXT (when enabled)', () => { assert.strictEqual( contextManager.active(), - Context.ROOT_CONTEXT, + ROOT_CONTEXT, 'should not have context' ); }); - it('should always return Context.ROOT_CONTEXT (when disabled)', () => { + it('should always return ROOT_CONTEXT (when disabled)', () => { contextManager.disable(); assert.strictEqual( contextManager.active(), - Context.ROOT_CONTEXT, + ROOT_CONTEXT, 'should not have context' ); contextManager.enable(); diff --git a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts index 93ce9e1c6c..b757664cd3 100644 --- a/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts +++ b/packages/opentelemetry-context-zone-peer-dep/src/ZoneContextManager.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { ContextManager, Context } from '@opentelemetry/context-base'; +import { + Context, + ContextManager, + ROOT_CONTEXT, +} from '@opentelemetry/context-base'; import { Func, TargetWithEvents } from './types'; import { isListenerObject } from './util'; @@ -46,9 +50,7 @@ export class ZoneContextManager implements ContextManager { * @param activeZone */ private _activeContextFromZone(activeZone: Zone | undefined): Context { - return ( - (activeZone && activeZone.get(ZONE_CONTEXT_KEY)) || Context.ROOT_CONTEXT - ); + return (activeZone && activeZone.get(ZONE_CONTEXT_KEY)) || ROOT_CONTEXT; } /** @@ -190,7 +192,7 @@ export class ZoneContextManager implements ContextManager { */ active(): Context { if (!this._enabled) { - return Context.ROOT_CONTEXT; + return ROOT_CONTEXT; } const activeZone = this._getActiveZone(); @@ -199,7 +201,7 @@ export class ZoneContextManager implements ContextManager { return active; } - return Context.ROOT_CONTEXT; + return ROOT_CONTEXT; } /** diff --git a/packages/opentelemetry-context-zone-peer-dep/test/ZoneContextManager.test.ts b/packages/opentelemetry-context-zone-peer-dep/test/ZoneContextManager.test.ts index 905348005c..4a21678999 100644 --- a/packages/opentelemetry-context-zone-peer-dep/test/ZoneContextManager.test.ts +++ b/packages/opentelemetry-context-zone-peer-dep/test/ZoneContextManager.test.ts @@ -18,14 +18,14 @@ import 'zone.js'; import * as sinon from 'sinon'; import * as assert from 'assert'; import { ZoneContextManager } from '../src'; -import { Context } from '@opentelemetry/context-base'; +import { ROOT_CONTEXT, createContextKey } from '@opentelemetry/context-base'; let clock: any; describe('ZoneContextManager', () => { let contextManager: ZoneContextManager; - const key1 = Context.createKey('test key 1'); - const key2 = Context.createKey('test key 2'); + const key1 = createContextKey('test key 1'); + const key2 = createContextKey('test key 2'); beforeEach(() => { clock = sinon.useFakeTimers(); @@ -40,7 +40,7 @@ describe('ZoneContextManager', () => { describe('.enable()', () => { it('should work', () => { - const ctx = Context.ROOT_CONTEXT.setValue(key1, 1); + const ctx = ROOT_CONTEXT.setValue(key1, 1); assert.doesNotThrow(() => { assert( contextManager.enable() === contextManager, @@ -55,7 +55,7 @@ describe('ZoneContextManager', () => { describe('.disable()', () => { it('should work', () => { - const ctx = Context.ROOT_CONTEXT.setValue(key1, 1); + const ctx = ROOT_CONTEXT.setValue(key1, 1); assert.doesNotThrow(() => { assert( contextManager.disable() === contextManager, @@ -63,7 +63,7 @@ describe('ZoneContextManager', () => { ); contextManager.with(ctx, () => { assert( - contextManager.active() === Context.ROOT_CONTEXT, + contextManager.active() === ROOT_CONTEXT, 'should have root context' ); }); @@ -77,7 +77,7 @@ describe('ZoneContextManager', () => { }); it('should run the callback (object as target)', done => { - const test = Context.ROOT_CONTEXT.setValue(key1, 1); + const test = ROOT_CONTEXT.setValue(key1, 1); contextManager.with(test, () => { assert.strictEqual( contextManager.active(), @@ -106,9 +106,9 @@ describe('ZoneContextManager', () => { }); it('should finally restore an old context, including the async task', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1'); - const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 'ctx2'); - const ctx3 = Context.ROOT_CONTEXT.setValue(key1, 'ctx3'); + const ctx1 = ROOT_CONTEXT.setValue(key1, 'ctx1'); + const ctx2 = ROOT_CONTEXT.setValue(key1, 'ctx2'); + const ctx3 = ROOT_CONTEXT.setValue(key1, 'ctx3'); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); @@ -130,9 +130,9 @@ describe('ZoneContextManager', () => { }); it('should finally restore an old context when context is an object, including the async task', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 1); - const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 2); - const ctx3 = Context.ROOT_CONTEXT.setValue(key1, 3); + const ctx1 = ROOT_CONTEXT.setValue(key1, 1); + const ctx2 = ROOT_CONTEXT.setValue(key1, 2); + const ctx3 = ROOT_CONTEXT.setValue(key1, 3); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); contextManager.with(ctx2, () => { @@ -152,24 +152,15 @@ describe('ZoneContextManager', () => { assert.strictEqual(contextManager.active(), window); }); it('should correctly return the contexts for 3 parallel actions', () => { - const rootSpan = Context.ROOT_CONTEXT.setValue(key1, 'root'); + const rootSpan = ROOT_CONTEXT.setValue(key1, 'root'); contextManager.with(rootSpan, () => { assert.ok( contextManager.active().getValue(key1) === 'root', 'Current span is rootSpan' ); - const concurrentSpan1 = Context.ROOT_CONTEXT.setValue( - key2, - 'concurrentSpan1' - ); - const concurrentSpan2 = Context.ROOT_CONTEXT.setValue( - key2, - 'concurrentSpan2' - ); - const concurrentSpan3 = Context.ROOT_CONTEXT.setValue( - key2, - 'concurrentSpan3' - ); + const concurrentSpan1 = ROOT_CONTEXT.setValue(key2, 'concurrentSpan1'); + const concurrentSpan2 = ROOT_CONTEXT.setValue(key2, 'concurrentSpan2'); + const concurrentSpan3 = ROOT_CONTEXT.setValue(key2, 'concurrentSpan3'); contextManager.with(concurrentSpan1, () => { setTimeout(() => { @@ -201,7 +192,7 @@ describe('ZoneContextManager', () => { }); it('should fork new zone from active one', () => { - const context = Context.ROOT_CONTEXT; + const context = ROOT_CONTEXT; const rootZone = Zone.current; contextManager.with(context, () => { const zone1 = Zone.current; @@ -233,7 +224,7 @@ describe('ZoneContextManager', () => { } const obj1 = new Obj('a1'); - const ctx = Context.ROOT_CONTEXT.setValue(key1, obj1); + const ctx = ROOT_CONTEXT.setValue(key1, obj1); obj1.title = 'a2'; const obj2 = new Obj('b1'); const wrapper: any = contextManager.bind(obj2.getTitle, ctx); @@ -242,24 +233,18 @@ describe('ZoneContextManager', () => { it('should return the same target (when enabled)', () => { const test = { a: 1 }; - assert.deepStrictEqual( - contextManager.bind(test, Context.ROOT_CONTEXT), - test - ); + assert.deepStrictEqual(contextManager.bind(test, ROOT_CONTEXT), test); }); it('should return the same target (when disabled)', () => { contextManager.disable(); const test = { a: 1 }; - assert.deepStrictEqual( - contextManager.bind(test, Context.ROOT_CONTEXT), - test - ); + assert.deepStrictEqual(contextManager.bind(test, ROOT_CONTEXT), test); contextManager.enable(); }); it('should return current context (when enabled)', done => { - const context = Context.ROOT_CONTEXT.setValue(key1, { a: 1 }); + const context = ROOT_CONTEXT.setValue(key1, { a: 1 }); const fn: any = contextManager.bind(() => { assert.strictEqual( contextManager.active(), @@ -273,11 +258,11 @@ describe('ZoneContextManager', () => { it('should return root context (when disabled)', done => { contextManager.disable(); - const context = Context.ROOT_CONTEXT.setValue(key1, { a: 1 }); + const context = ROOT_CONTEXT.setValue(key1, { a: 1 }); const fn: any = contextManager.bind(() => { assert.strictEqual( contextManager.active(), - Context.ROOT_CONTEXT, + ROOT_CONTEXT, 'should have context' ); return done(); @@ -286,7 +271,7 @@ describe('ZoneContextManager', () => { }); it('should bind the the certain context to the target "addEventListener" function', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 1); + const ctx1 = ROOT_CONTEXT.setValue(key1, 1); const element = document.createElement('div'); contextManager.bind(element, ctx1); @@ -310,7 +295,7 @@ describe('ZoneContextManager', () => { }); it('should preserve zone when creating new click event inside zone', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 1); + const ctx1 = ROOT_CONTEXT.setValue(key1, 1); const element = document.createElement('div'); contextManager.bind(element, ctx1); diff --git a/packages/opentelemetry-core/src/context/context.ts b/packages/opentelemetry-core/src/context/context.ts index 6edbfa0a43..1d06ba8dcd 100644 --- a/packages/opentelemetry-core/src/context/context.ts +++ b/packages/opentelemetry-core/src/context/context.ts @@ -14,23 +14,27 @@ * limitations under the License. */ -import { Span, SpanContext } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { + Span, + SpanContext, + createContextKey, + Context, +} from '@opentelemetry/api'; /** * Active span key */ -export const ACTIVE_SPAN_KEY = Context.createKey( +export const ACTIVE_SPAN_KEY = createContextKey( 'OpenTelemetry Context Key ACTIVE_SPAN' ); -const EXTRACTED_SPAN_CONTEXT_KEY = Context.createKey( +const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey( 'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT' ); /** * Shared key for indicating if instrumentation should be suppressed beyond * this current scope. */ -export const SUPPRESS_INSTRUMENTATION_KEY = Context.createKey( +export const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( 'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' ); diff --git a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts index 43da0b1189..d4b11894a4 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts +++ b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts @@ -20,6 +20,7 @@ import { TextMapPropagator, SetterFunction, TraceFlags, + createContextKey, } from '@opentelemetry/api'; import { getParentSpanContext, setExtractedSpanContext } from '../context'; @@ -28,10 +29,10 @@ export const X_B3_SPAN_ID = 'x-b3-spanid'; export const X_B3_SAMPLED = 'x-b3-sampled'; export const X_B3_PARENT_SPAN_ID = 'x-b3-parentspanid'; export const X_B3_FLAGS = 'x-b3-flags'; -export const PARENT_SPAN_ID_KEY = Context.createKey( +export const PARENT_SPAN_ID_KEY = createContextKey( 'OpenTelemetry Context Key B3 Parent Span Id' ); -export const DEBUG_FLAG_KEY = Context.createKey( +export const DEBUG_FLAG_KEY = createContextKey( 'OpenTelemetry Context Key B3 Debug Flag' ); const VALID_TRACEID_REGEX = /^([0-9a-f]{16}){1,2}$/i; diff --git a/packages/opentelemetry-core/src/correlation-context/correlation-context.ts b/packages/opentelemetry-core/src/correlation-context/correlation-context.ts index 130bf24142..cb1f37cc55 100644 --- a/packages/opentelemetry-core/src/correlation-context/correlation-context.ts +++ b/packages/opentelemetry-core/src/correlation-context/correlation-context.ts @@ -14,10 +14,13 @@ * limitations under the License. */ -import { CorrelationContext } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { + CorrelationContext, + Context, + createContextKey, +} from '@opentelemetry/api'; -const CORRELATION_CONTEXT = Context.createKey( +const CORRELATION_CONTEXT = createContextKey( 'OpenTelemetry Distributed Contexts Key' ); diff --git a/packages/opentelemetry-core/test/context/B3Propagator.test.ts b/packages/opentelemetry-core/test/context/B3Propagator.test.ts index 567fcc6778..fc4dcb67bb 100644 --- a/packages/opentelemetry-core/test/context/B3Propagator.test.ts +++ b/packages/opentelemetry-core/test/context/B3Propagator.test.ts @@ -20,7 +20,7 @@ import { SpanContext, TraceFlags, } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; import { getExtractedSpanContext, @@ -55,7 +55,7 @@ describe('B3Propagator', () => { }; b3Propagator.inject( - setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext), + setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultSetter ); @@ -79,7 +79,7 @@ describe('B3Propagator', () => { }; b3Propagator.inject( - setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext), + setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultSetter ); @@ -99,10 +99,7 @@ describe('B3Propagator', () => { spanId: '6e0c63257de34c92', traceFlags: TraceFlags.NONE, }; - const contextWithDebug = Context.ROOT_CONTEXT.setValue( - DEBUG_FLAG_KEY, - '1' - ); + const contextWithDebug = ROOT_CONTEXT.setValue(DEBUG_FLAG_KEY, '1'); b3Propagator.inject( setExtractedSpanContext(contextWithDebug, spanContext), @@ -126,7 +123,7 @@ describe('B3Propagator', () => { traceFlags: TraceFlags.NONE, }; - const contextWithParentSpanId = Context.ROOT_CONTEXT.setValue( + const contextWithParentSpanId = ROOT_CONTEXT.setValue( PARENT_SPAN_ID_KEY, 'f4592dc481026a8c' ); @@ -152,7 +149,7 @@ describe('B3Propagator', () => { traceFlags: TraceFlags.NONE, }; b3Propagator.inject( - setExtractedSpanContext(Context.ROOT_CONTEXT, emptySpanContext), + setExtractedSpanContext(ROOT_CONTEXT, emptySpanContext), carrier, defaultSetter ); @@ -168,7 +165,7 @@ describe('B3Propagator', () => { carrier[X_B3_TRACE_ID] = '0af7651916cd43dd8448eb211c80319c'; carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -190,7 +187,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -213,7 +210,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = true; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -236,7 +233,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = false; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -261,7 +258,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_FLAGS] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -287,7 +284,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = '0'; carrier[X_B3_SAMPLED] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -311,7 +308,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = 'false'; carrier[X_B3_SAMPLED] = '0'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -335,7 +332,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = 'true'; carrier[X_B3_SAMPLED] = '0'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -359,7 +356,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = '3'; carrier[X_B3_SAMPLED] = '0'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -385,7 +382,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = '0'; carrier[X_B3_SAMPLED] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -409,7 +406,7 @@ describe('B3Propagator', () => { carrier[X_B3_FLAGS] = '1'; carrier[X_B3_SAMPLED] = '0'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -436,7 +433,7 @@ describe('B3Propagator', () => { carrier[X_B3_TRACE_ID] = undefined; carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -447,7 +444,7 @@ describe('B3Propagator', () => { carrier[X_B3_TRACE_ID] = '0af7651916cd43dd8448eb211c80319c'; carrier[X_B3_SPAN_ID] = undefined; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -459,7 +456,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_PARENT_SPAN_ID] = 'invalid'; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -471,7 +468,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_PARENT_SPAN_ID] = '0af7651916cd43dd8448eb211c80319d'; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -483,7 +480,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = '2'; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -492,7 +489,7 @@ describe('B3Propagator', () => { describe('AND b3 header is missing', () => { it('should return undefined', () => { const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -503,7 +500,7 @@ describe('B3Propagator', () => { carrier[X_B3_TRACE_ID] = 'invalid!'; carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; const context = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(context, undefined); }); @@ -515,7 +512,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); @@ -570,7 +567,7 @@ describe('B3Propagator', () => { Object.getOwnPropertyNames(testCases).forEach(testCase => { carrier[X_B3_TRACE_ID] = testCases[testCase]; const extractedSpanContext = getExtractedSpanContext( - b3Propagator.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + b3Propagator.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, undefined, testCase); }); @@ -578,24 +575,24 @@ describe('B3Propagator', () => { it('should fail gracefully on bad responses from getter', () => { const ctx1 = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => 1 // not a number ); const ctx2 = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => [] // empty array ); const ctx3 = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => undefined // missing value ); - assert.ok(ctx1 === Context.ROOT_CONTEXT); - assert.ok(ctx2 === Context.ROOT_CONTEXT); - assert.ok(ctx3 === Context.ROOT_CONTEXT); + assert.ok(ctx1 === ROOT_CONTEXT); + assert.ok(ctx2 === ROOT_CONTEXT); + assert.ok(ctx3 === ROOT_CONTEXT); }); it('should left-pad 64 bit trace ids with 0', () => { @@ -603,7 +600,7 @@ describe('B3Propagator', () => { carrier[X_B3_SPAN_ID] = 'b7ad6b7169203331'; carrier[X_B3_SAMPLED] = '1'; const context = b3Propagator.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, defaultGetter ); diff --git a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts index cf2852112f..05fe343f1e 100644 --- a/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts +++ b/packages/opentelemetry-core/test/context/HttpTraceContext.test.ts @@ -20,7 +20,7 @@ import { SpanContext, TraceFlags, } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; import { getExtractedSpanContext, @@ -50,7 +50,7 @@ describe('HttpTraceContext', () => { }; httpTraceContext.inject( - setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext), + setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultSetter ); @@ -70,7 +70,7 @@ describe('HttpTraceContext', () => { }; httpTraceContext.inject( - setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext), + setExtractedSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultSetter ); @@ -87,7 +87,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, { @@ -102,7 +102,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, { @@ -117,7 +117,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'cc-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01-what-the-future-will-be-like'; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, { @@ -131,7 +131,7 @@ describe('HttpTraceContext', () => { it('returns null if traceparent header is missing', () => { assert.deepStrictEqual( getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ), undefined ); @@ -141,7 +141,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = 'invalid!'; assert.deepStrictEqual( getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ), undefined ); @@ -154,7 +154,7 @@ describe('HttpTraceContext', () => { assert.deepStrictEqual( getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ), undefined ); @@ -165,7 +165,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01', ]; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, { spanId: 'b7ad6b7169203331', @@ -180,7 +180,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = 'foo=bar,baz=qux'; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual( @@ -198,7 +198,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = ['foo=bar,baz=qux', 'quux=quuz']; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, { spanId: 'b7ad6b7169203331', @@ -252,7 +252,7 @@ describe('HttpTraceContext', () => { carrier[TRACE_PARENT_HEADER] = testCases[testCase]; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext, undefined, testCase); }); @@ -263,7 +263,7 @@ describe('HttpTraceContext', () => { '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01'; carrier[TRACE_STATE_HEADER] = 'foo=1 \t , \t bar=2, \t baz=3 '; const extractedSpanContext = getExtractedSpanContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual(extractedSpanContext!.traceState!.get('foo'), '1'); @@ -273,24 +273,24 @@ describe('HttpTraceContext', () => { it('should fail gracefully on bad responses from getter', () => { const ctx1 = httpTraceContext.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => 1 // not a number ); const ctx2 = httpTraceContext.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => [] // empty array ); const ctx3 = httpTraceContext.extract( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, carrier, (c, k) => undefined // missing value ); - assert.ok(ctx1 === Context.ROOT_CONTEXT); - assert.ok(ctx2 === Context.ROOT_CONTEXT); - assert.ok(ctx3 === Context.ROOT_CONTEXT); + assert.ok(ctx1 === ROOT_CONTEXT); + assert.ok(ctx2 === ROOT_CONTEXT); + assert.ok(ctx3 === ROOT_CONTEXT); }); }); }); diff --git a/packages/opentelemetry-core/test/context/composite.test.ts b/packages/opentelemetry-core/test/context/composite.test.ts index 6047f40fba..1b1bb24196 100644 --- a/packages/opentelemetry-core/test/context/composite.test.ts +++ b/packages/opentelemetry-core/test/context/composite.test.ts @@ -20,7 +20,7 @@ import { TextMapPropagator, SpanContext, } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { Context, ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; import { CompositePropagator, @@ -66,10 +66,7 @@ describe('Composite Propagator', () => { traceFlags: 1, traceState: new TraceState('foo=bar'), }; - ctxWithSpanContext = setExtractedSpanContext( - Context.ROOT_CONTEXT, - spanContext - ); + ctxWithSpanContext = setExtractedSpanContext(ROOT_CONTEXT, spanContext); }); it('should inject context using all configured propagators', () => { @@ -119,7 +116,7 @@ describe('Composite Propagator', () => { propagators: [new B3Propagator(), new HttpTraceContext()], }); const spanContext = getExtractedSpanContext( - composite.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + composite.extract(ROOT_CONTEXT, carrier, defaultGetter) ); if (!spanContext) { @@ -138,7 +135,7 @@ describe('Composite Propagator', () => { propagators: [new ThrowingPropagator(), new HttpTraceContext()], }); const spanContext = getExtractedSpanContext( - composite.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + composite.extract(ROOT_CONTEXT, carrier, defaultGetter) ); if (!spanContext) { diff --git a/packages/opentelemetry-core/test/context/context.test.ts b/packages/opentelemetry-core/test/context/context.test.ts index 1755727d49..5abb1d785c 100644 --- a/packages/opentelemetry-core/test/context/context.test.ts +++ b/packages/opentelemetry-core/test/context/context.test.ts @@ -22,13 +22,13 @@ import { unsuppressInstrumentation, isInstrumentationSuppressed, } from '../../src/context/context'; -import { Context } from '@opentelemetry/api'; +import { ROOT_CONTEXT } from '@opentelemetry/api'; describe('Context Helpers', () => { describe('suppressInstrumentation', () => { it('should set suppress to true', () => { const expectedValue = true; - const context = suppressInstrumentation(Context.ROOT_CONTEXT); + const context = suppressInstrumentation(ROOT_CONTEXT); const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY); const boolValue = value as boolean; @@ -40,7 +40,7 @@ describe('Context Helpers', () => { describe('unsuppressInstrumentation', () => { it('should set suppress to false', () => { const expectedValue = false; - const context = unsuppressInstrumentation(Context.ROOT_CONTEXT); + const context = unsuppressInstrumentation(ROOT_CONTEXT); const value = context.getValue(SUPPRESS_INSTRUMENTATION_KEY); const boolValue = value as boolean; @@ -52,7 +52,7 @@ describe('Context Helpers', () => { describe('isInstrumentationSuppressed', () => { it('should get value as bool', () => { const expectedValue = true; - const context = Context.ROOT_CONTEXT.setValue( + const context = ROOT_CONTEXT.setValue( SUPPRESS_INSTRUMENTATION_KEY, expectedValue ); @@ -63,10 +63,7 @@ describe('Context Helpers', () => { }); describe('when suppress instrumentation set to null', () => { - const context = Context.ROOT_CONTEXT.setValue( - SUPPRESS_INSTRUMENTATION_KEY, - null - ); + const context = ROOT_CONTEXT.setValue(SUPPRESS_INSTRUMENTATION_KEY, null); it('should return false', () => { const value = isInstrumentationSuppressed(context); @@ -76,7 +73,7 @@ describe('Context Helpers', () => { }); describe('when suppress instrumentation set to undefined', () => { - const context = Context.ROOT_CONTEXT.setValue( + const context = ROOT_CONTEXT.setValue( SUPPRESS_INSTRUMENTATION_KEY, undefined ); diff --git a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts index 981f7a78c7..247f747032 100644 --- a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts +++ b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts @@ -19,7 +19,7 @@ import { defaultSetter, CorrelationContext, } from '@opentelemetry/api'; -import { Context } from '@opentelemetry/context-base'; +import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; import { getCorrelationContext, @@ -49,7 +49,7 @@ describe('HttpCorrelationContext', () => { }; httpTraceContext.inject( - setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), + setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, defaultSetter ); @@ -70,7 +70,7 @@ describe('HttpCorrelationContext', () => { correlationContext['longPair'] = { value }; httpTraceContext.inject( - setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), + setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, defaultSetter ); @@ -101,7 +101,7 @@ describe('HttpCorrelationContext', () => { expected = expected.slice(0, -1); httpTraceContext.inject( - setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), + setCorrelationContext(ROOT_CONTEXT, correlationContext), carrier, defaultSetter ); @@ -114,7 +114,7 @@ describe('HttpCorrelationContext', () => { carrier[CORRELATION_CONTEXT_HEADER] = 'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm'; const extractedCorrelationContext = getCorrelationContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); const expected: CorrelationContext = { @@ -130,7 +130,7 @@ describe('HttpCorrelationContext', () => { it('returns undefined if header is missing', () => { assert.deepStrictEqual( getCorrelationContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ), undefined ); @@ -145,7 +145,7 @@ describe('HttpCorrelationContext', () => { }; assert.deepStrictEqual( getCorrelationContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ), expected ); @@ -188,7 +188,7 @@ describe('HttpCorrelationContext', () => { carrier[CORRELATION_CONTEXT_HEADER] = testCases[testCase].header; const extractedSpanContext = getCorrelationContext( - httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) + httpTraceContext.extract(ROOT_CONTEXT, carrier, defaultGetter) ); assert.deepStrictEqual( extractedSpanContext, diff --git a/packages/opentelemetry-shim-opentracing/src/shim.ts b/packages/opentelemetry-shim-opentracing/src/shim.ts index c5d4417c3a..df8014d384 100644 --- a/packages/opentelemetry-shim-opentracing/src/shim.ts +++ b/packages/opentelemetry-shim-opentracing/src/shim.ts @@ -180,7 +180,7 @@ export class TracerShim extends opentracing.Tracer { carrier, api.defaultSetter, setCorrelationContext( - setExtractedSpanContext(api.Context.ROOT_CONTEXT, oTelSpanContext), + setExtractedSpanContext(api.ROOT_CONTEXT, oTelSpanContext), oTelSpanCorrelationContext ) ); diff --git a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts index 2e95fce020..6202beb4bb 100644 --- a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts @@ -14,8 +14,13 @@ * limitations under the License. */ -import { Context, context, SpanContext, TraceFlags } from '@opentelemetry/api'; -import { ContextManager } from '@opentelemetry/context-base'; +import { + context, + SpanContext, + TraceFlags, + ContextManager, + ROOT_CONTEXT, +} from '@opentelemetry/api'; import { AlwaysOnSampler, AlwaysOffSampler, @@ -177,7 +182,7 @@ describe('BasicTracerProvider', () => { const span = tracer.startSpan( 'my-span', {}, - setExtractedSpanContext(Context.ROOT_CONTEXT, { + setExtractedSpanContext(ROOT_CONTEXT, { traceId: 'd4cda95b652f4a1592b449d5929fda1b', spanId: '6e0c63257de34c92', traceFlags: TraceFlags.SAMPLED, @@ -198,7 +203,7 @@ describe('BasicTracerProvider', () => { const childSpan = tracer.startSpan( 'child-span', {}, - setActiveSpan(Context.ROOT_CONTEXT, span) + setActiveSpan(ROOT_CONTEXT, span) ); const context = childSpan.context(); assert.strictEqual(context.traceId, span.context().traceId); @@ -216,7 +221,7 @@ describe('BasicTracerProvider', () => { { parent: overrideParent, }, - setActiveSpan(Context.ROOT_CONTEXT, span) + setActiveSpan(ROOT_CONTEXT, span) ); const context = childSpan.context(); assert.strictEqual(context.traceId, overrideParent.context().traceId); @@ -234,7 +239,7 @@ describe('BasicTracerProvider', () => { { parent: overrideParent.context(), }, - setActiveSpan(Context.ROOT_CONTEXT, span) + setActiveSpan(ROOT_CONTEXT, span) ); const context = childSpan.context(); assert.strictEqual(context.traceId, overrideParent.context().traceId); @@ -250,7 +255,7 @@ describe('BasicTracerProvider', () => { const rootSpan = tracer.startSpan( 'root-span', { parent: null }, - setActiveSpan(Context.ROOT_CONTEXT, span) + setActiveSpan(ROOT_CONTEXT, span) ); const context = rootSpan.context(); assert.notStrictEqual(context.traceId, overrideParent.context().traceId); @@ -264,7 +269,7 @@ describe('BasicTracerProvider', () => { 'my-span', {}, setExtractedSpanContext( - Context.ROOT_CONTEXT, + ROOT_CONTEXT, ('invalid-parent' as unknown) as SpanContext ) ); @@ -277,7 +282,7 @@ describe('BasicTracerProvider', () => { const span = tracer.startSpan( 'my-span', {}, - setExtractedSpanContext(Context.ROOT_CONTEXT, { + setExtractedSpanContext(ROOT_CONTEXT, { traceId: '0', spanId: '0', traceFlags: TraceFlags.SAMPLED, @@ -327,8 +332,7 @@ describe('BasicTracerProvider', () => { describe('.getCurrentSpan()', () => { it('should return current span when it exists', () => { context.setGlobalContextManager({ - active: () => - setActiveSpan(Context.ROOT_CONTEXT, ('foo' as any) as Span), + active: () => setActiveSpan(ROOT_CONTEXT, ('foo' as any) as Span), disable: () => {}, } as ContextManager); diff --git a/packages/opentelemetry-tracing/test/Tracer.test.ts b/packages/opentelemetry-tracing/test/Tracer.test.ts index 9851aee624..1a2ea19b9e 100644 --- a/packages/opentelemetry-tracing/test/Tracer.test.ts +++ b/packages/opentelemetry-tracing/test/Tracer.test.ts @@ -19,9 +19,9 @@ import { NoopSpan, Sampler, SamplingDecision, - Context, NOOP_SPAN, TraceFlags, + ROOT_CONTEXT, } from '@opentelemetry/api'; import { BasicTracerProvider, Tracer, Span } from '../src'; import { @@ -119,7 +119,7 @@ describe('Tracer', () => { }); describe('when suppressInstrumentation true', () => { - const context = suppressInstrumentation(Context.ROOT_CONTEXT); + const context = suppressInstrumentation(ROOT_CONTEXT); it('should return cached no-op span ', done => { const tracer = new Tracer( diff --git a/packages/opentelemetry-tracing/test/export/TestStackContextManager.ts b/packages/opentelemetry-tracing/test/export/TestStackContextManager.ts index 3062ea1069..6612a34381 100644 --- a/packages/opentelemetry-tracing/test/export/TestStackContextManager.ts +++ b/packages/opentelemetry-tracing/test/export/TestStackContextManager.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { ContextManager, Context } from '@opentelemetry/context-base'; +import { + ContextManager, + Context, + ROOT_CONTEXT, +} from '@opentelemetry/context-base'; /** * A test-only ContextManager that uses an in-memory stack to keep track of @@ -26,9 +30,7 @@ export class TestStackContextManager implements ContextManager { private _contextStack: Context[] = []; active(): Context { - return ( - this._contextStack[this._contextStack.length - 1] ?? Context.ROOT_CONTEXT - ); + return this._contextStack[this._contextStack.length - 1] ?? ROOT_CONTEXT; } with ReturnType>( diff --git a/packages/opentelemetry-web/src/StackContextManager.ts b/packages/opentelemetry-web/src/StackContextManager.ts index 718ec88035..a49609c0c8 100644 --- a/packages/opentelemetry-web/src/StackContextManager.ts +++ b/packages/opentelemetry-web/src/StackContextManager.ts @@ -15,7 +15,7 @@ */ import { Context } from '@opentelemetry/api'; -import { ContextManager } from '@opentelemetry/context-base'; +import { ContextManager, ROOT_CONTEXT } from '@opentelemetry/context-base'; /** * Stack Context Manager for managing the state in web @@ -30,7 +30,7 @@ export class StackContextManager implements ContextManager { /** * Keeps the reference to current context */ - public _currentContext = Context.ROOT_CONTEXT; + public _currentContext = ROOT_CONTEXT; /** * @@ -39,7 +39,7 @@ export class StackContextManager implements ContextManager { */ private _bindFunction( target: T, - context = Context.ROOT_CONTEXT + context = ROOT_CONTEXT ): T { const manager = this; const contextWrapper = function (this: unknown, ...args: unknown[]) { @@ -66,7 +66,7 @@ export class StackContextManager implements ContextManager { * @param target * @param context */ - bind(target: T, context = Context.ROOT_CONTEXT): T { + bind(target: T, context = ROOT_CONTEXT): T { // if no specific context to propagate is given, we use the current one if (context === undefined) { context = this.active(); @@ -81,7 +81,7 @@ export class StackContextManager implements ContextManager { * Disable the context manager (clears the current context) */ disable(): this { - this._currentContext = Context.ROOT_CONTEXT; + this._currentContext = ROOT_CONTEXT; this._enabled = false; return this; } @@ -94,7 +94,7 @@ export class StackContextManager implements ContextManager { return this; } this._enabled = true; - this._currentContext = Context.ROOT_CONTEXT; + this._currentContext = ROOT_CONTEXT; return this; } @@ -109,7 +109,7 @@ export class StackContextManager implements ContextManager { fn: () => ReturnType ): ReturnType { const previousContext = this._currentContext; - this._currentContext = context || Context.ROOT_CONTEXT; + this._currentContext = context || ROOT_CONTEXT; try { return fn(); diff --git a/packages/opentelemetry-web/test/StackContextManager.test.ts b/packages/opentelemetry-web/test/StackContextManager.test.ts index daebd27971..7a5742bab7 100644 --- a/packages/opentelemetry-web/test/StackContextManager.test.ts +++ b/packages/opentelemetry-web/test/StackContextManager.test.ts @@ -14,13 +14,13 @@ * limitations under the License. */ +import { createContextKey, ROOT_CONTEXT } from '@opentelemetry/api'; import * as assert from 'assert'; import { StackContextManager } from '../src'; -import { Context } from '@opentelemetry/api'; describe('StackContextManager', () => { let contextManager: StackContextManager; - const key1 = Context.createKey('test key 1'); + const key1 = createContextKey('test key 1'); beforeEach(() => { contextManager = new StackContextManager(); @@ -39,7 +39,7 @@ describe('StackContextManager', () => { 'should return this' ); assert( - contextManager.active() === Context.ROOT_CONTEXT, + contextManager.active() === ROOT_CONTEXT, 'should have root context' ); }); @@ -54,7 +54,7 @@ describe('StackContextManager', () => { 'should return this' ); assert( - contextManager.active() === Context.ROOT_CONTEXT, + contextManager.active() === ROOT_CONTEXT, 'should have no context' ); }); @@ -67,7 +67,7 @@ describe('StackContextManager', () => { }); it('should run the callback (object as target)', done => { - const test = Context.ROOT_CONTEXT.setValue(key1, 1); + const test = ROOT_CONTEXT.setValue(key1, 1); contextManager.with(test, () => { assert.strictEqual( contextManager.active(), @@ -96,9 +96,9 @@ describe('StackContextManager', () => { }); it('should finally restore an old context', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 'ctx1'); - const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 'ctx2'); - const ctx3 = Context.ROOT_CONTEXT.setValue(key1, 'ctx3'); + const ctx1 = ROOT_CONTEXT.setValue(key1, 'ctx1'); + const ctx2 = ROOT_CONTEXT.setValue(key1, 'ctx2'); + const ctx3 = ROOT_CONTEXT.setValue(key1, 'ctx3'); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); contextManager.with(ctx2, () => { @@ -115,9 +115,9 @@ describe('StackContextManager', () => { }); it('should finally restore an old context when context is an object', done => { - const ctx1 = Context.ROOT_CONTEXT.setValue(key1, 1); - const ctx2 = Context.ROOT_CONTEXT.setValue(key1, 2); - const ctx3 = Context.ROOT_CONTEXT.setValue(key1, 3); + const ctx1 = ROOT_CONTEXT.setValue(key1, 1); + const ctx2 = ROOT_CONTEXT.setValue(key1, 2); + const ctx3 = ROOT_CONTEXT.setValue(key1, 3); contextManager.with(ctx1, () => { assert.strictEqual(contextManager.active(), ctx1); contextManager.with(ctx2, () => { @@ -149,7 +149,7 @@ describe('StackContextManager', () => { } const obj1 = new Obj('a1'); - const ctx = Context.ROOT_CONTEXT.setValue(key1, obj1); + const ctx = ROOT_CONTEXT.setValue(key1, obj1); obj1.title = 'a2'; const obj2 = new Obj('b1'); const wrapper: any = contextManager.bind(obj2.getTitle, ctx); @@ -157,19 +157,19 @@ describe('StackContextManager', () => { }); it('should return the same target (when enabled)', () => { - const test = Context.ROOT_CONTEXT.setValue(key1, 1); + const test = ROOT_CONTEXT.setValue(key1, 1); assert.deepStrictEqual(contextManager.bind(test), test); }); it('should return the same target (when disabled)', () => { contextManager.disable(); - const test = Context.ROOT_CONTEXT.setValue(key1, 1); + const test = ROOT_CONTEXT.setValue(key1, 1); assert.deepStrictEqual(contextManager.bind(test), test); contextManager.enable(); }); it('should return current context (when enabled)', done => { - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const fn: any = contextManager.bind(() => { assert.strictEqual( contextManager.active(), @@ -183,7 +183,7 @@ describe('StackContextManager', () => { it('should return current context (when disabled)', done => { contextManager.disable(); - const context = Context.ROOT_CONTEXT.setValue(key1, 1); + const context = ROOT_CONTEXT.setValue(key1, 1); const fn: any = contextManager.bind(() => { assert.strictEqual( contextManager.active(),