From 83180a4a1c8b05ba242a4509cff2244a39de9b63 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 19 Feb 2020 11:55:04 -0500 Subject: [PATCH 1/5] chore: add context API --- packages/opentelemetry-api/src/api/context.ts | 71 +++++++++++++++++++ packages/opentelemetry-api/src/index.ts | 4 ++ 2 files changed, 75 insertions(+) create mode 100644 packages/opentelemetry-api/src/api/context.ts diff --git a/packages/opentelemetry-api/src/api/context.ts b/packages/opentelemetry-api/src/api/context.ts new file mode 100644 index 0000000000..03704a7791 --- /dev/null +++ b/packages/opentelemetry-api/src/api/context.ts @@ -0,0 +1,71 @@ +/*! + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ScopeManager, NoopScopeManager, Context } from "@opentelemetry/scope-base" + +/** + * Singleton object which represents the entry point to the OpenTelemetry Context API + */ +export class ContextAPI { + private static _instance?: ContextAPI; + private _manager: ScopeManager = new NoopScopeManager(); + + /** Empty private constructor prevents end users from constructing a new instance of the API */ + private constructor() { } + + /** Get the singleton instance of the Context API */ + public static getInstance(): ContextAPI { + if (!this._instance) { + this._instance = new ContextAPI(); + } + + return this._instance; + } + + /** + * Set the current context manager. Returns the initialized context manager + */ + public initGlobalContextManager(contextManager: ScopeManager): ScopeManager { + this._manager = contextManager; + return contextManager; + } + + /** + * Get the currently active context + */ + public active(): Context { + return this._manager.active() + } + + /** + * Execute a function with an active context + * + * @param fn function to execute in a context + * @param scope context to be active during function execution. Defaults to the currently active context + */ + public with ReturnType>(fn: T, scope: Context = this.active()): ReturnType { + return this._manager.with(scope, fn) + } + + /** + * + * @param target function or event emitter to bind + * @param scope context to bind to the event emitter or function. Defaults to the currently active context + */ + public bind(target: T, scope: Context = this.active()): T { + return this._manager.bind(target, scope) + } +} diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index e2ff686fb2..2858fb327b 100644 --- a/packages/opentelemetry-api/src/index.ts +++ b/packages/opentelemetry-api/src/index.ts @@ -48,6 +48,10 @@ export * from './trace/tracer'; export { Context } from '@opentelemetry/scope-base'; +import { ContextAPI } from './api/context'; +/** Entrypoint for context API */ +export const context = ContextAPI.getInstance(); + import { TraceAPI } from './api/trace'; /** Entrypoint for trace API */ export const trace = TraceAPI.getInstance(); From a4dadb646f01b11869887c3b1e2b18f30473bcec Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 19 Feb 2020 13:52:37 -0500 Subject: [PATCH 2/5] chore: lint --- packages/opentelemetry-api/src/api/context.ts | 91 ++++++++++--------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/packages/opentelemetry-api/src/api/context.ts b/packages/opentelemetry-api/src/api/context.ts index 03704a7791..c83a04f808 100644 --- a/packages/opentelemetry-api/src/api/context.ts +++ b/packages/opentelemetry-api/src/api/context.ts @@ -14,58 +14,65 @@ * limitations under the License. */ -import { ScopeManager, NoopScopeManager, Context } from "@opentelemetry/scope-base" +import { + ScopeManager, + NoopScopeManager, + Context, +} from '@opentelemetry/scope-base'; /** * Singleton object which represents the entry point to the OpenTelemetry Context API */ export class ContextAPI { - private static _instance?: ContextAPI; - private _manager: ScopeManager = new NoopScopeManager(); + private static _instance?: ContextAPI; + private _manager: ScopeManager = new NoopScopeManager(); - /** Empty private constructor prevents end users from constructing a new instance of the API */ - private constructor() { } + /** Empty private constructor prevents end users from constructing a new instance of the API */ + private constructor() {} - /** Get the singleton instance of the Context API */ - public static getInstance(): ContextAPI { - if (!this._instance) { - this._instance = new ContextAPI(); - } - - return this._instance; + /** Get the singleton instance of the Context API */ + public static getInstance(): ContextAPI { + if (!this._instance) { + this._instance = new ContextAPI(); } - /** - * Set the current context manager. Returns the initialized context manager - */ - public initGlobalContextManager(contextManager: ScopeManager): ScopeManager { - this._manager = contextManager; - return contextManager; - } + return this._instance; + } - /** - * Get the currently active context - */ - public active(): Context { - return this._manager.active() - } + /** + * Set the current context manager. Returns the initialized context manager + */ + public initGlobalContextManager(contextManager: ScopeManager): ScopeManager { + this._manager = contextManager; + return contextManager; + } - /** - * Execute a function with an active context - * - * @param fn function to execute in a context - * @param scope context to be active during function execution. Defaults to the currently active context - */ - public with ReturnType>(fn: T, scope: Context = this.active()): ReturnType { - return this._manager.with(scope, fn) - } + /** + * Get the currently active context + */ + public active(): Context { + return this._manager.active(); + } - /** - * - * @param target function or event emitter to bind - * @param scope context to bind to the event emitter or function. Defaults to the currently active context - */ - public bind(target: T, scope: Context = this.active()): T { - return this._manager.bind(target, scope) - } + /** + * Execute a function with an active context + * + * @param fn function to execute in a context + * @param scope context to be active during function execution. Defaults to the currently active context + */ + public with ReturnType>( + fn: T, + scope: Context = this.active() + ): ReturnType { + return this._manager.with(scope, fn); + } + + /** + * + * @param target function or event emitter to bind + * @param scope context to bind to the event emitter or function. Defaults to the currently active context + */ + public bind(target: T, scope: Context = this.active()): T { + return this._manager.bind(target, scope); + } } From e933a7aa905f4da6d5935b189c433528530aefa0 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 19 Feb 2020 14:57:27 -0500 Subject: [PATCH 3/5] chore: review naming --- packages/opentelemetry-api/src/api/context.ts | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/opentelemetry-api/src/api/context.ts b/packages/opentelemetry-api/src/api/context.ts index c83a04f808..801d965416 100644 --- a/packages/opentelemetry-api/src/api/context.ts +++ b/packages/opentelemetry-api/src/api/context.ts @@ -25,12 +25,12 @@ import { */ export class ContextAPI { private static _instance?: ContextAPI; - private _manager: ScopeManager = new NoopScopeManager(); + private _scopeManager: ScopeManager = new NoopScopeManager(); /** Empty private constructor prevents end users from constructing a new instance of the API */ private constructor() {} - /** Get the singleton instance of the Context API */ + /** Get the singleton instance of the Scope API */ public static getInstance(): ContextAPI { if (!this._instance) { this._instance = new ContextAPI(); @@ -42,37 +42,38 @@ export class ContextAPI { /** * Set the current context manager. Returns the initialized context manager */ - public initGlobalContextManager(contextManager: ScopeManager): ScopeManager { - this._manager = contextManager; - return contextManager; + public initGlobalContextManager(scopeManager: ScopeManager): ScopeManager { + this._scopeManager = scopeManager; + return scopeManager; } /** * Get the currently active context */ public active(): Context { - return this._manager.active(); + return this._scopeManager.active(); } /** * Execute a function with an active context * * @param fn function to execute in a context - * @param scope context to be active during function execution. Defaults to the currently active context + * @param context context to be active during function execution. Defaults to the currently active context */ public with ReturnType>( fn: T, - scope: Context = this.active() + context: Context = this.active() ): ReturnType { - return this._manager.with(scope, fn); + return this._scopeManager.with(context, fn); } /** - * + * Bind a context to a target function or event emitter + * * @param target function or event emitter to bind - * @param scope context to bind to the event emitter or function. Defaults to the currently active context + * @param context context to bind to the event emitter or function. Defaults to the currently active context */ - public bind(target: T, scope: Context = this.active()): T { - return this._manager.bind(target, scope); + public bind(target: T, context: Context = this.active()): T { + return this._scopeManager.bind(target, context); } } From 03a251d84c32bf617649b73495e327f4deac102a Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 19 Feb 2020 15:32:33 -0500 Subject: [PATCH 4/5] chore: lint --- packages/opentelemetry-api/src/api/context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-api/src/api/context.ts b/packages/opentelemetry-api/src/api/context.ts index 801d965416..6d4b89c4ea 100644 --- a/packages/opentelemetry-api/src/api/context.ts +++ b/packages/opentelemetry-api/src/api/context.ts @@ -69,7 +69,7 @@ export class ContextAPI { /** * Bind a context to a target function or event emitter - * + * * @param target function or event emitter to bind * @param context context to bind to the event emitter or function. Defaults to the currently active context */ From 2c72f6e0d8c32aac2f572ddccf37ddaeed8b23a8 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 19 Feb 2020 15:53:13 -0500 Subject: [PATCH 5/5] chore: add context to default api export --- packages/opentelemetry-api/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index 2858fb327b..eba1f2df12 100644 --- a/packages/opentelemetry-api/src/index.ts +++ b/packages/opentelemetry-api/src/index.ts @@ -63,4 +63,5 @@ export const metrics = MetricsAPI.getInstance(); export default { trace, metrics, + context, };