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

[celery] Patch via post-import hooks #534

Merged
merged 5 commits into from
Aug 13, 2018

Conversation

Kyle-Verhoog
Copy link
Member

Patching Celery via Post-import Hooks

Background

Patching celery our usual way does not work for celery as celery contains a reference to sys.argv. This is problematic as sys.argv is not defined in sitecustomize.py.

Overview

This PR introduces a fix for this by taking a completely different approach to patching by using post-import hooks as defined here: https://www.python.org/dev/peps/pep-0369.

Caveats

For simplicity and proof of concept this approach is meant only to patch celery via post-import hooks. It is a bit hacky in that we check explicitly for celery and add in the normal patching side-effects.

Take-aways

It turns out that patching this way is extremely easy. We should strongly consider moving to this strategy for patching all of our modules!

@Kyle-Verhoog Kyle-Verhoog added this to the 0.13.0 milestone Aug 7, 2018
@Kyle-Verhoog Kyle-Verhoog self-assigned this Aug 7, 2018
@@ -79,7 +79,22 @@ def patch(raise_errors=True, **patch_modules):
modules = [m for (m, should_patch) in patch_modules.items() if should_patch]
count = 0
for module in modules:
patched = patch_module(module, raise_errors=raise_errors)
# TODO: this is a temporary hack until we shift to using
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patching method is hacked into our existing mechanism. I tried to work around this but there really isn't much that can be done until we work in a completely new patching mechanism to install the hooks.

For now I think it's fine that this remain a hack for the celery release. Once it is validated with the release we can consider refactoring our patching mechanism.


# manually add celery to patched modules and increment count
_PATCHED_MODULES.add(module)
count += 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a patched = True/False here if you want to avoid errors on line 98

# manually add celery to patched modules and increment count
_PATCHED_MODULES.add(module)
count += 1
patched = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the code I think count is incremented twice :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, yes, good catch! we really need to change how we patch so that this dirty hack can go away very soon...



class CeleryPatchTest(unittest.TestCase):
def test_patch_after_import(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for testing this code-path. It ensures the current behavior is preserved without any breaking change.

if patched:
count += 1

# TODO: this is a temporary hack until we shift to using

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our next step is removing this hack and migrate all the instrumentation to this hook system.

@@ -130,7 +130,6 @@ deps =
celery41: celery>=4.1,<4.2
celery42: celery>=4.2,<4.3
ddtracerun: redis
ddtracerun: celery

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to remove because it's already running in our test suite.

@Kyle-Verhoog Kyle-Verhoog merged commit 99d717d into palazzem/celery-signals Aug 13, 2018
@Kyle-Verhoog Kyle-Verhoog deleted the kyle-verhoog/celery-hook branch August 13, 2018 20:14
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

3 participants