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

AsyncLocalStorage based ContextManager #1210

Merged
merged 6 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ const ADD_LISTENER_METHODS = [
'prependOnceListener' as 'prependOnceListener',
];

export abstract class BaseContextManager implements ContextManager {
export abstract class AbstractAsyncHooksContextManager
implements ContextManager {
abstract active(): Context;

abstract with<T extends (...args: unknown[]) => ReturnType<T>>(
dyladan marked this conversation as resolved.
Show resolved Hide resolved
context: Context,
fn: T
): ReturnType<T>;

abstract enable(): this;

abstract disable(): this;

bind<T>(target: T, context?: Context): T {
// if no specific context to propagate is given, we use the current one
if (context === undefined) {
Expand All @@ -57,10 +62,6 @@ export abstract class BaseContextManager implements ContextManager {
return target;
}

abstract enable(): this;

abstract disable(): this;

private _bindFunction<T extends Function>(target: T, context: Context): T {
const manager = this;
const contextWrapper = function (this: {}, ...args: unknown[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import { Context } from '@opentelemetry/context-base';
import * as asyncHooks from 'async_hooks';
import { BaseContextManager } from './BaseContextManager';
import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager';

export class AsyncHooksContextManager extends BaseContextManager {
export class AsyncHooksContextManager extends AbstractAsyncHooksContextManager {
private _asyncHook: asyncHooks.AsyncHook;
private _contexts: Map<number, Context | undefined> = new Map();
private _stack: Array<Context | undefined> = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import { Context } from '@opentelemetry/context-base';
import { AsyncLocalStorage } from 'async_hooks';
import { BaseContextManager } from './BaseContextManager';
import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager';

export class AsyncLocalStorageContextManager extends BaseContextManager {
export class AsyncLocalStorageContextManager extends AbstractAsyncHooksContextManager {
private _asyncLocalStorage: AsyncLocalStorage<Context>;

constructor() {
Expand Down