Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context API #792

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/opentelemetry-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 {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
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
*
dyladan marked this conversation as resolved.
Show resolved Hide resolved
* @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 packages/opentelemetry-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,
};