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

fix(opentelemetry-context-async-hooks): Support AsyncLocalStorageContextManager in non-Node.js runtimes #4615

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -15,15 +15,50 @@
*/

import { Context, ROOT_CONTEXT } from '@opentelemetry/api';
import { AsyncLocalStorage } from 'async_hooks';

// In some JS runtimes, AsyncLocalStorage is not accessible through the
// `async_hooks` API. This may cause crashes when `async_hooks` is attempted to
// be imported. Node.js provides all of its APIs via the global object, including
// `async_hooks`, meaning we can access `AsyncLocalStorage` through the global
// object. Other runtimes, like the Vercel Edge runtime put the `AsyncLocalStorage`
// binding on the global object directly.
// The import below is only used for type information and needs to be a `type`
// import, otherwise, it may create errors on runtimes that don't support the
// `async_hooks` API.
import type { AsyncLocalStorage } from 'async_hooks';

import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager';

type AsyncLocalStorageClass = new () => AsyncLocalStorage<Context>;

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

constructor() {
super();
this._asyncLocalStorage = new AsyncLocalStorage();

const _globalThis = typeof globalThis === 'object' ? globalThis : global;
const globalWithAsyncLocalStorage = _globalThis as typeof _globalThis & {
// Available in Node 14+
async_hooks?: {
AsyncLocalStorage?: AsyncLocalStorageClass;
};
// Available in environments like Vercel Edge
AsyncLocalStorage?: AsyncLocalStorageClass;
};

const AsyncLocalStorageClass =
(globalWithAsyncLocalStorage.async_hooks &&
globalWithAsyncLocalStorage.async_hooks.AsyncLocalStorage) ||
globalWithAsyncLocalStorage.AsyncLocalStorage;

if (!AsyncLocalStorageClass) {
throw new Error(
'AbstractAsyncHooksContext manager is not supported in this JavaScript runtime'
);
}

this._asyncLocalStorage = new AsyncLocalStorageClass();
}

active(): Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import {
} from '../src';
import { EventEmitter } from 'events';
import { createContextKey, ROOT_CONTEXT } from '@opentelemetry/api';
import { AsyncLocalStorage } from 'async_hooks';

// The test runtime doesn't expose Node.js API on the global object so we need to set it manually.
before(() => {
// @ts-ignore
global.AsyncLocalStorage = AsyncLocalStorage;
});

after(() => {
// @ts-ignore
delete global.AsyncLocalStorage;
});

for (const contextManagerClass of [
AsyncHooksContextManager,
Expand All @@ -41,7 +53,7 @@ for (const contextManagerClass of [
});

afterEach(() => {
contextManager.disable();
contextManager?.disable();
otherContextManager?.disable();
});

Expand Down