Skip to content

Commit 6a28ed2

Browse files
author
Emanuele Palazzetti
committed
[httplib] provide a override context manager to switch the global tracer with a dummy one
1 parent 93a79af commit 6a28ed2

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

tests/contrib/httplib/test_httplib.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
from ddtrace.contrib.httplib import patch, unpatch
1212
from ddtrace.contrib.httplib.patch import should_skip_request
1313
from ddtrace.pin import Pin
14+
15+
from .utils import override_global_tracer
1416
from ...test_tracer import get_dummy_tracer
1517

18+
1619
if PY2:
1720
from urllib2 import urlopen, build_opener, Request
1821
else:
@@ -316,7 +319,9 @@ def test_urllib_request(self):
316319
we return the original response
317320
we capture a span for the request
318321
"""
319-
resp = urlopen('http://httpstat.us/200')
322+
with override_global_tracer(self.tracer):
323+
resp = urlopen('http://httpstat.us/200')
324+
320325
self.assertEqual(self.to_str(resp.read()), '200 OK')
321326
self.assertEqual(resp.getcode(), 200)
322327

@@ -338,7 +343,9 @@ def test_urllib_request_https(self):
338343
we return the original response
339344
we capture a span for the request
340345
"""
341-
resp = urlopen('https://httpbin.org/status/200')
346+
with override_global_tracer(self.tracer):
347+
resp = urlopen('https://httpbin.org/status/200')
348+
342349
self.assertEqual(self.to_str(resp.read()), '')
343350
self.assertEqual(resp.getcode(), 200)
344351

@@ -361,7 +368,9 @@ def test_urllib_request_object(self):
361368
we capture a span for the request
362369
"""
363370
req = Request('http://httpstat.us/200')
364-
resp = urlopen(req)
371+
with override_global_tracer(self.tracer):
372+
resp = urlopen(req)
373+
365374
self.assertEqual(self.to_str(resp.read()), '200 OK')
366375
self.assertEqual(resp.getcode(), 200)
367376

@@ -383,7 +392,9 @@ def test_urllib_request_opener(self):
383392
we capture a span for the request
384393
"""
385394
opener = build_opener()
386-
resp = opener.open('http://httpstat.us/200')
395+
with override_global_tracer(self.tracer):
396+
resp = opener.open('http://httpstat.us/200')
397+
387398
self.assertEqual(self.to_str(resp.read()), '200 OK')
388399
self.assertEqual(resp.getcode(), 200)
389400

@@ -410,7 +421,9 @@ def test_urllib_request(self):
410421
we return the original response
411422
we capture a span for the request
412423
"""
413-
resp = urllib.urlopen('http://httpstat.us/200')
424+
with override_global_tracer(self.tracer):
425+
resp = urllib.urlopen('http://httpstat.us/200')
426+
414427
self.assertEqual(resp.read(), '200 OK')
415428
self.assertEqual(resp.getcode(), 200)
416429

@@ -432,7 +445,9 @@ def test_urllib_request_https(self):
432445
we return the original response
433446
we capture a span for the request
434447
"""
435-
resp = urllib.urlopen('https://httpbin.org/status/200')
448+
with override_global_tracer(self.tracer):
449+
resp = urllib.urlopen('https://httpbin.org/status/200')
450+
436451
self.assertEqual(resp.read(), '')
437452
self.assertEqual(resp.getcode(), 200)
438453

tests/contrib/httplib/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import ddtrace
2+
3+
from contextlib import contextmanager
4+
5+
6+
@contextmanager
7+
def override_global_tracer(tracer):
8+
"""Helper functions that overrides the global tracer available in the
9+
`ddtrace` package. This is required because in some `httplib` tests we
10+
can't get easily the PIN object attached to the `HTTPConnection` to
11+
replace the used tracer with a dummy tracer.
12+
"""
13+
original_tracer = ddtrace.tracer
14+
ddtrace.tracer = tracer
15+
yield
16+
ddtrace.tracer = original_tracer

0 commit comments

Comments
 (0)