Migrate from AsyncResource to TracingChannel for Hapi.js plugin#4597
Migrate from AsyncResource to TracingChannel for Hapi.js plugin#4597cristunaranjo wants to merge 8 commits intoDataDog:masterfrom
Conversation
|
Hi there, |
rochdev
left a comment
There was a problem hiding this comment.
While this addresses the issue of altering the async stack, it also has the effect of breaking tracing. The proper fix here would be to switch from AsyncResource to TracingChannel which is able to track individual stores instead of having to alter the current async context for all code in the process.
|
Thanks for the review @rochdev I'm not too familiar with this library (or tracing libraries in general 😅 ). Where can I find an example of how to use the I'm happy to give it a shot and try to fix it, but this is out of my expertise. |
Generally speaking yes, but for Hapi it's a bit different as its events (at least in the current code) don't really map directly to a
I may be forgetting something, but generally speaking those are the minimum steps to migrate from |
|
I've made the requested changes and confirmed that the storage is functioning correctly. However, I’m not entirely sure how to validate that the traces are working as expected. I'm not familiar with what to look for, and with my previous change the traces seemed fine to me 😅 . |
Sorry for the delay, I just saw this. You can validate that this works by running the tests for Hapi locally with As for the change, it looks good to me for the most part, and if anything is missing the tests will catch it. Of course it might be worth it to also add a test for the incorrect behaviour you were seeing to avoid a regression in the future. The only thing I'm not sure I understand in the PR is the tracing channel map. Why is that needed? |
…anjo/dd-trace-js into fix-async-storage-hapi
|
I have added a test to the Regarding the tracing map, I replicated the approach used with the channels map so that it can be reused in other plugins. However, I am happy to remove it if you feel it is unnecessary @rochdev |
Perfect, thanks!
I guess I don't understand what is the purpose of the map in the first place 😅 If it can be removed, then let's definitely remove it. It should be possible to use |
|
Thanks for the feedback again @rochdev. Really appreciate your help with this. |
|
Thanks for this work, @cristunaranjo! I'm going to make a copy of your PR so that all of our CI tasks can properly run. |
* fix: remove async scope in hapi dd-instrumentation * fix linting * replace asyncResource for tracingChannel * move tracing channels to helpers * add test * remove tracingChannel map --------- Co-authored-by: Cris Naranjo <cris@yulife.com>
|
These changes have merged and should be available in the next release. Thanks for the contribution! |
* fix: remove async scope in hapi dd-instrumentation * fix linting * replace asyncResource for tracingChannel * move tracing channels to helpers * add test * remove tracingChannel map --------- Co-authored-by: Cris Naranjo <cris@yulife.com>
* fix: remove async scope in hapi dd-instrumentation * fix linting * replace asyncResource for tracingChannel * move tracing channels to helpers * add test * remove tracingChannel map --------- Co-authored-by: Cris Naranjo <cris@yulife.com>
* fix: remove async scope in hapi dd-instrumentation * fix linting * replace asyncResource for tracingChannel * move tracing channels to helpers * add test * remove tracingChannel map --------- Co-authored-by: Cris Naranjo <cris@yulife.com>
* fix: remove async scope in hapi dd-instrumentation * fix linting * replace asyncResource for tracingChannel * move tracing channels to helpers * add test * remove tracingChannel map --------- Co-authored-by: Cris Naranjo <cris@yulife.com>
What does this PR do?
This PR addresses issue #4557, where
dd-trace's use ofAsyncResourcein the Hapi.js handler wrapper was causing conflicts with existingAsyncLocalStoragecontexts in applications. By removing theAsyncResourceusage, the PR ensures that user-defined async contexts are respected and not overridden.The original implementation used
AsyncResourceto ensure that tracing was correctly applied within the Hapi.js handler context. However, this was found to interfere with applications that already define their own async contexts usingAsyncLocalStorage. In this PR, I removed theAsyncResourcewrapping to prevent conflicts with these user-defined contexts. The handler function is now directly invoked within the existing async scope.Potential Side Effects
Removing
AsyncResourcemay potentially affect the isolation of the tracing context in environments whereAsyncLocalStorageis not in use. There might be cases where this change could lead to the leakage of async contexts if not properly managed by the application itself. Specifically, applications that rely on the isolation provided byAsyncResourcemight need to take additional steps to ensure context integrity.Testing
I have tested this change in a Hapi.js application using
AsyncLocalStorageand confirmed that the context is preserved across requests without conflicts. Additionally, I ran the existingdd-tracetest suite to ensure that no other functionality was inadvertently affected. If necessary, I am happy to assist with further testing across different Node.js versions or specific scenarios.