Skip to content

Commit a46883a

Browse files
author
Emanuele Palazzetti
committed
[requests] add a test to check requests module proxy is correctly instrumented
1 parent 8f1865d commit a46883a

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

ddtrace/contrib/requests/connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _wrap_request(func, instance, args, kwargs):
5353
span.service = _extract_service_name(instance, span)
5454

5555
# propagate distributed tracing headers
56-
if config.get_from(instance)['distributed_tracing']:
56+
if config.get_from(instance).get('distributed_tracing'):
5757
propagator = HTTPPropagator()
5858
propagator.inject(span.context, headers)
5959
kwargs['headers'] = headers
@@ -64,11 +64,11 @@ def _wrap_request(func, instance, args, kwargs):
6464
return response
6565
finally:
6666
try:
67-
span.set_tag(http.METHOD, method)
67+
span.set_tag(http.METHOD, method.upper())
6868
span.set_tag(http.URL, url)
6969
if response is not None:
7070
span.set_tag(http.STATUS_CODE, response.status_code)
7171
# `span.error` must be an integer
7272
span.error = int(500 <= response.status_code)
7373
except Exception:
74-
log.debug("error patching tags", exc_info=True)
74+
log.debug("requests: error adding tags", exc_info=True)

tests/contrib/requests/test_requests.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import requests
23

34
from requests import Session
45
from nose.tools import eq_
@@ -7,6 +8,7 @@
78
from ddtrace.ext import http, errors
89
from ddtrace.contrib.requests import patch, unpatch
910

11+
from ...util import override_global_tracer
1012
from ...test_tracer import get_dummy_tracer
1113

1214
# socket name comes from https://english.stackexchange.com/a/44048
@@ -51,9 +53,9 @@ def test_args_kwargs(self):
5153
url = URL_200
5254
method = 'GET'
5355
inputs = [
54-
([], {'method': method, 'url': url}),
55-
([method], {'url': url}),
56-
([method, url], {}),
56+
([], {'method': method, 'url': url}),
57+
([method], {'url': url}),
58+
([method, url], {}),
5759
]
5860

5961
for args, kwargs in inputs:
@@ -101,6 +103,21 @@ def test_200(self):
101103
eq_(s.error, 0)
102104
eq_(s.span_type, http.TYPE)
103105

106+
def test_requests_module_200(self):
107+
# ensure the requests API is instrumented even without
108+
# using a `Session` directly
109+
with override_global_tracer(self.tracer):
110+
out = requests.get(URL_200)
111+
eq_(out.status_code, 200)
112+
# validation
113+
spans = self.tracer.writer.pop()
114+
eq_(len(spans), 1)
115+
s = spans[0]
116+
eq_(s.get_tag(http.METHOD), 'GET')
117+
eq_(s.get_tag(http.STATUS_CODE), '200')
118+
eq_(s.error, 0)
119+
eq_(s.span_type, http.TYPE)
120+
104121
def test_post_500(self):
105122
out = self.session.post(URL_500)
106123
# validation

tests/contrib/requests/test_requests_distributed.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def matcher(request):
4040
eq_('bar', resp.text)
4141

4242
def test_propagation_true(self):
43-
# [Backward compatibility]: ensure users can switch the distributed
44-
# tracing flag using the `Session` attribute
43+
# ensure distributed tracing can be enabled
4544
cfg = config.get_from(self.session)
4645
cfg['distributed_tracing'] = True
4746
adapter = Adapter()
@@ -63,8 +62,7 @@ def matcher(request):
6362
eq_(root.span_id, req.parent_id)
6463

6564
def test_propagation_false(self):
66-
# [Backward compatibility]: ensure users can switch the distributed
67-
# tracing flag using the `Session` attribute
65+
# ensure distributed tracing can be disabled
6866
cfg = config.get_from(self.session)
6967
cfg['distributed_tracing'] = False
7068
adapter = Adapter()

0 commit comments

Comments
 (0)