diff --git a/api/src/api/propagation.ts b/api/src/api/propagation.ts index 92a046a659..3e39ab1ff0 100644 --- a/api/src/api/propagation.ts +++ b/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/api/src/context-base/NoopContextManager.ts b/api/src/context-base/NoopContextManager.ts index e02816f221..fd2e9f2121 100644 --- a/api/src/context-base/NoopContextManager.ts +++ b/api/src/context-base/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/api/src/context-base/context.ts b/api/src/context-base/context.ts index 2a6804312c..3010e4409f 100644 --- a/api/src/context-base/context.ts +++ b/api/src/context-base/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/api/src/context-base/index.ts b/api/src/context-base/index.ts index d9e53ab004..0957a63910 100644 --- a/api/src/context-base/index.ts +++ b/api/src/context-base/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/api/src/context-base/types.ts b/api/src/context-base/types.ts index e58a4990cf..20922441a7 100644 --- a/api/src/context-base/types.ts +++ b/api/src/context-base/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/api/src/index.ts b/api/src/index.ts index 0a7e8bbd85..d7e99d2efd 100644 --- a/api/src/index.ts +++ b/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/api/test/context-base/NoopContextManager.test.ts b/api/test/context-base/NoopContextManager.test.ts index b35f979799..61f9ae7355 100644 --- a/api/test/context-base/NoopContextManager.test.ts +++ b/api/test/context-base/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();