Skip to content

Commit 85c9022

Browse files
authored
Merge pull request #47 from instana/respect_my_authoritah!
Urllib3: Respect tracing state
2 parents edc88e7 + 24e5286 commit 85c9022

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

instana/instrumentation/urllib3.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77

88
@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
99
def urlopen_with_instana(wrapped, instance, args, kwargs):
10+
context = instana.internal_tracer.current_context()
11+
12+
# If we're not tracing, just return
13+
if context is None:
14+
return wrapped(*args, **kwargs)
15+
1016
try:
11-
context = instana.internal_tracer.current_context()
1217
span = instana.internal_tracer.start_span("urllib3", child_of=context)
1318
span.set_tag(ext.HTTP_URL, args[1])
1419
span.set_tag(ext.HTTP_METHOD, args[0])

instana/tracer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def start_span(
6969
return self.current_span
7070

7171
def current_context(self):
72-
return self.current_span.context
72+
context = None
73+
if self.current_span:
74+
context = self.current_span.context
75+
return context
7376

7477
def inject(self, span_context, format, carrier):
7578
if format in self._propagators:

tests/test_urllib3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setUp(self):
1010
self.http = urllib3.PoolManager()
1111
self.recorder = tracer.recorder
1212
self.recorder.clear_spans()
13+
tracer.current_span = None
1314

1415
def tearDown(self):
1516
""" Do nothing for now """
@@ -19,6 +20,9 @@ def test_vanilla_requests(self):
1920
r = self.http.request('GET', 'http://127.0.0.1:5000/')
2021
assert_equals(r.status, 200)
2122

23+
spans = self.recorder.queued_spans()
24+
assert_equals(0, len(spans))
25+
2226
def test_get_request(self):
2327
span = tracer.start_span("test")
2428
r = self.http.request('GET', 'http://127.0.0.1:5000/')

0 commit comments

Comments
 (0)