opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

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

Open lforst opened this issue 1 year ago • 1 comments

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

lforst avatar Apr 09 '24 09:04 lforst

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: lforst / name: Luca Forstner (60b317b13464f91ff8932710e59a5b46aab0f71e)

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Jun 10 '24 06:06 github-actions[bot]