From 5fc957673ef9c5aa07eca84430194829bfd378ec Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 20 Feb 2020 15:20:45 -0500 Subject: [PATCH] Add context API (#792) * chore: add context API --- packages/opentelemetry-api/src/api/context.ts | 79 +++++++++++++++++++ packages/opentelemetry-api/src/index.ts | 5 ++ 2 files changed, 84 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..6d4b89c4ea --- /dev/null +++ b/packages/opentelemetry-api/src/api/context.ts @@ -0,0 +1,79 @@ +/*! + * 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 _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 Scope 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(scopeManager: ScopeManager): ScopeManager { + this._scopeManager = scopeManager; + return scopeManager; + } + + /** + * Get the currently active context + */ + public active(): Context { + return this._scopeManager.active(); + } + + /** + * Execute a function with an active context + * + * @param fn function to execute in a context + * @param context context to be active during function execution. Defaults to the currently active context + */ + public with ReturnType>( + fn: T, + context: Context = this.active() + ): ReturnType { + 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 context context to bind to the event emitter or function. Defaults to the currently active context + */ + public bind(target: T, context: Context = this.active()): T { + return this._scopeManager.bind(target, context); + } +} diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index e2ff686fb2..eba1f2df12 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(); @@ -59,4 +63,5 @@ export const metrics = MetricsAPI.getInstance(); export default { trace, metrics, + context, };