-
Notifications
You must be signed in to change notification settings - Fork 803
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(instrumentation): apply unwrap before wrap in base class #4692
Conversation
I didn't add tests, as this functionality exists to support instrumentation tests like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if we should make unwrap
private. Private methods can still be called from tests using obj["_somePrivateProperty"]
and it really shouldn't be used by users.
That's a good point. Adding @Flarna comment from here:
I think there are still a lot of opportunities for improvements which we can address. This PR will allow us to clean dozens of lines of codes from contrib instrumentations. BTW - I wonder if we can introduce something to record which patches where made, and then automate the |
Sorry, I'm a bit late in this because of vacation. Do we know why We rely on simmer and others might also use shimmer - so OTel might undo wrappings done by others. Fine in tests but in real world? |
FWIW, the Elastic APM agent has a light fork of shimmer.js to have it use a private symbol for the "is wrapped" and "unwrap" properties, so that it won't conflict with other uses of shimmer. |
…elemetry#4692) * feat(instrumentation): apply unwrap before wrap in base class * chore: CHANGELOG
This PR aims to cleanup code from instrumentations, that unwraps a patched function before attempting to patch it again.
For instrumentation implementations, there will be no need to
_unwrap
before calling_wrap
, refactoring this:to this shorter cleaner version:
Improvement Opportunity
A common implementation of instrumnetation
patch
hook looks like this:Instrumentation first checks if the function it attempts to patch is already wrapped, and if so, unwrap it before it is wrapped again. This is needed, I believe, for test to function correctly, where the tests are
enable()
d anddisabled()
d again and again to clear their state and start fresh.Change
The
_wrap
function is implemented in theInstrumentationBase
class for platform node. I moved the common checkisWrapped
and_unwrap
ing it if needed, into the_wrap
implementation, and removed it from the 2 core instrumentation. you can already see how many lines of code are cleared from existing instrumentations, making the codebase a bit shorter and removing lines that add no real value and decrease readability in instrumentation implementation.Benefits
This PR moves the
unwrap
call to happen as part of thewrap
for nodejs base instrumentation class:patch
implementationsunwrap
in implementations, thus making tests more robust.Change Analysis
unwrap
ed to begin with (end users, and tests where the check is already ), this addition will not do any extra work since theisWrapped
is false.wrap
on an alreadywrap
ed function. I think this case doesn't make sense and it will only happen if the implementation forgot to run theunwraping
. In this case, applying theunwrap
in base class would guarantee that we only have one active patch per function. This should never happen in end users' code, only in tests, as end users are not expected toenable
anddisable
instrumentations back and forte in their apps.We can release this feature without breaking any existing instrumentation, and then create followup PRs to cleanup contrib instrumentations and take advantage of this service.