Skip to content

How to use AsyncLocalStorage? #4516

Open
@joshkel

Description

@joshkel

Runtime

Node.js

Runtime version

18.17.1

Module version

21.3.10

Used with

Hapi application

Any other relevant information

No response

What problem are you trying to solve?

I'm looking into using AsyncLocalStorage to make certain values accessible throughout my application logic, without explicitly passing it as a parameter everywhere.

As far as I can tell, this is a poor fit for Hapi: Hapi lifecycle methods and plugins offer extension points, for invoking arbitrary logic at different points in the application. However, AsyncLocalStorage wants to be invoked with an asynchronous callback, so it can see the start-to-finish execution of amethod; I can't find a way for a Hapi plugin to wrap a request or handler method start-to-finish.

Similar questions come up with instrumentation and monitoring frameworks such as Sentry or OpenTelemetry; OpenTelemetry's Hapi instrumentation currently uses complex runtime patching to accomplish its goals.

Do you have a new or modified API suggestion to solve the problem?

Hapi could offer a new lifecycle method / extension point that's invoked with a callback, so it can see the entire method start to finish.

Alternatively, Hapi could add explicit support for one or more AsyncLocalStorage instances.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions