node:async_hooks
The node:async_hooks module in Azion Runtime: AsyncLocalStorage and AsyncResource inside a function, and a sample that carries a request ID.
The node:async_hooks module tracks the lifecycle of asynchronous resources, as the Node.js async_hooks documentation describes. Its hooks fire at each stage of an asynchronous operation, for context propagation and debugging. In Azion Runtime, the module is available through Node.js compatibility. Its common use in a function is AsyncLocalStorage, which carries request-scoped data, such as a request ID, across asynchronous calls.
In a deployed function and under azion dev, AsyncLocalStorage keeps its store across an await and into a setTimeout callback, and outside run(), getStore() returns no value. AsyncResource works too: runInAsyncScope() runs its callback and returns the callback’s value.
Example
The function below reads the X-Request-Id header, stores it with AsyncLocalStorage, and logs it from functions that do not receive it as an argument:
A request with the header X-Request-Id: req-1 receives the body ok. While the request runs, the function logs these lines: