Skip to content

Commit cf4c895

Browse files
committed
requests: ensure that disabled tracing still works
1 parent 2afaf58 commit cf4c895

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

ddtrace/contrib/requests/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
required_modules = ['requests']
2929

3030
with require_modules(required_modules) as missing_modules:
31-
if not missing_modules:
32-
from .patch import TracedSession, patch
33-
__all__ = ['TracedSession', 'patch']
31+
if not missing_modules:
32+
from .patch import TracedSession, patch
33+
__all__ = ['TracedSession', 'patch']

ddtrace/contrib/requests/patch.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22

3+
# stdlib
4+
import logging
5+
36
# 3p
47
import requests
58
import wrapt
@@ -10,6 +13,9 @@
1013
from ddtrace.ext import http
1114

1215

16+
log = logging.getLogger(__name__)
17+
18+
1319
def patch():
1420
""" Monkeypatch the requests library to trace http calls. """
1521
wrapt.wrap_function_wrapper('requests', 'Session.request', _traced_request_func)
@@ -26,19 +32,23 @@ def _traced_request_func(func, instance, args, kwargs):
2632

2733
# bail on the tracing if not enabled.
2834
if not tracer.enabled:
29-
return wrapped(*args, **kwargs)
35+
return func(*args, **kwargs)
3036

3137
# FIXME[matt] be a bit less brittle here.
3238
method = kwargs.get('method') or args[0]
33-
url = kwargs.get('url') or args[1]
39+
url = kwargs.get('url') or args[1]
3440

3541
with tracer.trace("requests.request") as span:
3642
resp = None
3743
try:
3844
resp = func(*args, **kwargs)
3945
return resp
4046
finally:
41-
_apply_tags(span, method, url, resp)
47+
48+
try:
49+
_apply_tags(span, method, url, resp)
50+
except Exception:
51+
log.warn("error patching tags", exc_info=True)
4252

4353

4454
def _apply_tags(span, method, url, response):

tests/contrib/requests/test_requests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
class TestSession(object):
1313

14+
@staticmethod
15+
def test_tracer_disabled():
16+
# ensure all valid combinations of args / kwargs work
17+
tracer, session = get_traced_session()
18+
tracer.enabled = False
19+
out = session.get('http://httpstat.us/200')
20+
eq_(out.status_code, 200)
21+
spans = tracer.writer.pop()
22+
eq_(len(spans), 0)
23+
1424
@staticmethod
1525
def test_args_kwargs():
1626
# ensure all valid combinations of args / kwargs work

0 commit comments

Comments
 (0)