Skip to content

Commit

Permalink
Add context API (open-telemetry#792)
Browse files Browse the repository at this point in the history
* chore: add context API
  • Loading branch information
dyladan committed Feb 18, 2021
1 parent 28d7595 commit f0c3413
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
79 changes: 79 additions & 0 deletions api/src/api/context.ts
Original file line number Diff line number Diff line change
@@ -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<T extends (...args: unknown[]) => ReturnType<T>>(
fn: T,
context: Context = this.active()
): ReturnType<T> {
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<T>(target: T, context: Context = this.active()): T {
return this._scopeManager.bind(target, context);
}
}
5 changes: 5 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -59,4 +63,5 @@ export const metrics = MetricsAPI.getInstance();
export default {
trace,
metrics,
context,
};

0 comments on commit f0c3413

Please sign in to comment.