-
Notifications
You must be signed in to change notification settings - Fork 415
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
[core] Add module import hook install logic #754
Conversation
I wonder if we shouldn't just do this first? It feels like we are setting ourselves up for failure here. We might not see an edge case where this is a problem right now, but a customer will definitely find one for us... especially if we already can see this being an issue in the future. |
I don't think we're quite setting ourselves up for failure since this is quite an edge-case (most modules break or have undefined behaviour when reimported/deleted and imported again). Plus there is the fact that our current patching does not account for reimports so the functionality is the same from the user's perspective (they would have to call With that said, it's something we're going to change out eventually so it's worth considering not introducing it in the first place. I will investigate bringing in the core pieces of |
Fair enough. My only concern here is if we know what we probably need, if we hold off on it, will we ever implement it? |
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.
Mostly looks good.
It is probably worth having a test case where we setup a non-dd import hook and mess with importing and install/uninstall to ensure we don't mess with the default behavior of wrapt
. Just in case someone is using dd-trace-py
along with wrapt.register_post_import_hook
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.
a few minor comments
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.
do we need a test case for installing a module hook twice?
we should probably try to deduplicate functions being registered for a module.
@Kyle-Verhoog I'll take it from here |
@brettlangdon #769 took out the hook logic from this PR. This PR is now focused on adding the API in which integrations will use the import hook system. |
Overview
This PR adds the required logic necessary for installing module import hooks.
Now integrations can register their
patch
functions to be run when the required module is imported. This means that integrations can be certain of being the first to run after the code is imported.What this looks like for integrations (with
gevent
as the example). Ingevent/__init__.py
:Caveats
This approach uses
wrapt
's module import hooking system which deletes the hooks after they are run. This isn't desirable for our use-case even though it's highly unlikely that modules will be reimported or deleted. Another undesired effect of usingwrapt
is that we cannot uninstall module import hooks and have to importwrapt
privates to do so. While it is overkill for this PR to introduce our own meta-path managing it is something we will have to move to long term.