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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lforst
Copy link

@lforst lforst commented Apr 9, 2024

Which problem is this PR solving?

The AsyncHooksContextManager currently depends on the Node.js specific API async_hooks in order to make use of AsyncLocalStorage. Other JS runtimes, like for example the Vercel Edge runtime, do not support this specific API, but provide other mechanisms for usind AsyncLocalStorage, e.g. by exposing the AsyncLocalStorage API on the global object. Using the AsyncLocalStorageContextManager in these runtimes will fail since the async_hooks import is not available.

Short description of the changes

Instead of importing the Node.js-specific API async_hooks which would break in non-Node.js runtimes during initialization, we go through a set of fallbacks to try and find AsyncLocalStorage:

  1. Look on globalThis.async_hooks.AsyncLocalStorage (this would succeed in Node.js)
  2. Look on globalThis.AsyncLocalStorage (this would succeed in most other runtimes)

Type of change

Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

Copy link

linux-foundation-easycla bot commented Apr 9, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: lforst / name: Luca Forstner (60b317b)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant