Skip to content

Commit 99d717d

Browse files
authored
[celery] Patch via post-import hooks (#534)
* [core] revert argv patch * [core] patch celery via post-import hooks * [core] set patched to true for celery patching * [core] remove patched module count side effect * [celery] add tests verifying the import hook patching
1 parent b7f5598 commit 99d717d

File tree

7 files changed

+68
-13
lines changed

7 files changed

+68
-13
lines changed

ddtrace/bootstrap/sitecustomize.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ def update_patched_modules():
7070
if opts:
7171
tracer.configure(**opts)
7272

73-
if not hasattr(sys, 'argv'):
74-
sys.argv = ['']
75-
7673
if patch:
7774
update_patched_modules()
7875
from ddtrace import patch_all; patch_all(**EXTRA_PATCHED_MODULES) # noqa

ddtrace/monkey.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,28 @@ def patch(raise_errors=True, **patch_modules):
7777
>>> patch(psycopg=True, elasticsearch=True)
7878
"""
7979
modules = [m for (m, should_patch) in patch_modules.items() if should_patch]
80-
count = 0
8180
for module in modules:
82-
patched = patch_module(module, raise_errors=raise_errors)
83-
if patched:
84-
count += 1
85-
81+
# TODO: this is a temporary hack until we shift to using
82+
# post-import hooks for everything.
83+
if module == 'celery':
84+
# if patch celery via post-import hooks
85+
from wrapt.importer import when_imported
86+
87+
@when_imported('celery')
88+
def patch_celery(hook):
89+
from ddtrace.contrib.celery import patch
90+
patch()
91+
92+
# manually add celery to patched modules
93+
_PATCHED_MODULES.add(module)
94+
else:
95+
patch_module(module, raise_errors=raise_errors)
96+
97+
patched_modules = get_patched_modules()
8698
log.info("patched %s/%s modules (%s)",
87-
count,
99+
len(patched_modules),
88100
len(modules),
89-
",".join(get_patched_modules()))
101+
",".join(patched_modules))
90102

91103

92104
def patch_module(module, raise_errors=True):
@@ -115,7 +127,7 @@ def _patch_module(module):
115127
"""
116128
path = 'ddtrace.contrib.%s' % module
117129
with _LOCK:
118-
if module in _PATCHED_MODULES:
130+
if module in _PATCHED_MODULES and module != 'celery':
119131
log.debug("already patched: %s", path)
120132
return False
121133

tests/commands/ddtrace_run_patched_modules.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
if __name__ == '__main__':
88
ok_('redis' in monkey.get_patched_modules())
9-
ok_('celery' in monkey.get_patched_modules())
109
print("Test success")

tests/contrib/celery/autopatch.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import print_function
2+
3+
from nose.tools import ok_
4+
5+
from ddtrace import Pin
6+
7+
if __name__ == '__main__':
8+
# have to import celery in order to have the post-import hooks run
9+
import celery
10+
11+
# now celery.Celery should be patched and should have a pin
12+
ok_(Pin.get_from(celery.Celery))
13+
print("Test success")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
import subprocess
3+
import unittest
4+
5+
6+
class DdtraceRunTest(unittest.TestCase):
7+
"""Test that celery is patched successfully if run with ddtrace-run."""
8+
9+
def test_autopatch(self):
10+
out = subprocess.check_output(
11+
['ddtrace-run', 'python', 'tests/contrib/celery/autopatch.py']
12+
)
13+
assert out.startswith(b"Test success")

tests/contrib/celery/test_patch.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from nose.tools import ok_
3+
from ddtrace import Pin
4+
5+
6+
class CeleryPatchTest(unittest.TestCase):
7+
def test_patch_after_import(self):
8+
import celery
9+
from ddtrace import patch
10+
patch(celery=True)
11+
12+
app = celery.Celery()
13+
ok_(Pin.get_from(app) is not None)
14+
15+
def test_patch_before_import(self):
16+
from ddtrace import patch
17+
patch(celery=True)
18+
import celery
19+
20+
app = celery.Celery()
21+
ok_(Pin.get_from(app) is not None)
22+

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ deps =
130130
celery41: celery>=4.1,<4.2
131131
celery42: celery>=4.2,<4.3
132132
ddtracerun: redis
133-
ddtracerun: celery
134133
elasticsearch16: elasticsearch>=1.6,<1.7
135134
elasticsearch17: elasticsearch>=1.7,<1.8
136135
elasticsearch18: elasticsearch>=1.8,<1.9

0 commit comments

Comments
 (0)