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

feat: Support async local storage in interceptors #11142

Conversation

nordfjord
Copy link
Contributor

This is necessary to allow use cases such as allowing interceptors to
change the context in OpenTelemetry. You might for example want to add
Baggage to the context in an interceptor but because of the way it was
structured before the baggage would not make its way into the controller

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Before this change the context would be lost before the controller was called because of the deferred nature of the next call.

Issue Number: N/A

What is the new behavior?

This PR makes it possible to use AsyncLocalStorage from interceptors in such a way that the controller method will have access to the storage.

Does this PR introduce a breaking change?

  • Yes
  • No

Not sure if it's a breaking change. If anyone relies on the interceptor chain running asap, but the controller being deferred this would be a breaking change. I find this a highly unlikely scenario so have put it down as a no.

Other information

@coveralls
Copy link

coveralls commented Feb 20, 2023

Pull Request Test Coverage Report for Build aae6ef71-ee4a-46e5-b19e-3334be198d62

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.002%) to 92.883%

Totals Coverage Status
Change from base Build aa3fcab8-9499-4650-b471-bc16ef351958: -0.002%
Covered Lines: 6486
Relevant Lines: 6983

💛 - Coveralls

This is necessary to allow use cases such as allowing interceptors to
change the context in OpenTelemetry. You might for example want to add
Baggage to the context in an interceptor but because of the way it was
structured before the baggage would not make its way into the controller
@nordfjord nordfjord force-pushed the support-async-local-storage-in-interceptors branch from d2b1943 to 25d9ef9 Compare February 20, 2023 19:13
@kamilmysliwiec kamilmysliwiec added this to the 10.0.0 milestone Apr 5, 2023
@kamilmysliwiec kamilmysliwiec changed the base branch from master to 10.0.0 April 5, 2023 11:11
@kamilmysliwiec kamilmysliwiec merged commit 10c1bd8 into nestjs:10.0.0 Apr 5, 2023
@khaelys
Copy link

khaelys commented Dec 13, 2023

On Nest 9 you can wrap the observable, a snippet can be found here
On Nest 10 now you can just return the Observable from the AsyncLocalStorage:

return MyAsyncLocalStorage.run(mySharableContext, () => {
      // Continue the request handling
      return next.handle();
});

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

Successfully merging this pull request may close these issues.

None yet

4 participants