diff --git a/instana/instrumentation/urllib3.py b/instana/instrumentation/urllib3.py index 2be0e20b..9fb97ce9 100644 --- a/instana/instrumentation/urllib3.py +++ b/instana/instrumentation/urllib3.py @@ -7,8 +7,13 @@ @wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen') def urlopen_with_instana(wrapped, instance, args, kwargs): + context = instana.internal_tracer.current_context() + + # If we're not tracing, just return + if context is None: + return wrapped(*args, **kwargs) + try: - context = instana.internal_tracer.current_context() span = instana.internal_tracer.start_span("urllib3", child_of=context) span.set_tag(ext.HTTP_URL, args[1]) span.set_tag(ext.HTTP_METHOD, args[0]) diff --git a/instana/tracer.py b/instana/tracer.py index d871548b..34d63fe3 100644 --- a/instana/tracer.py +++ b/instana/tracer.py @@ -69,7 +69,10 @@ def start_span( return self.current_span def current_context(self): - return self.current_span.context + context = None + if self.current_span: + context = self.current_span.context + return context def inject(self, span_context, format, carrier): if format in self._propagators: diff --git a/tests/test_urllib3.py b/tests/test_urllib3.py index 9bb7596a..1766f20f 100644 --- a/tests/test_urllib3.py +++ b/tests/test_urllib3.py @@ -10,6 +10,7 @@ def setUp(self): self.http = urllib3.PoolManager() self.recorder = tracer.recorder self.recorder.clear_spans() + tracer.current_span = None def tearDown(self): """ Do nothing for now """ @@ -19,6 +20,9 @@ def test_vanilla_requests(self): r = self.http.request('GET', 'http://127.0.0.1:5000/') assert_equals(r.status, 200) + spans = self.recorder.queued_spans() + assert_equals(0, len(spans)) + def test_get_request(self): span = tracer.start_span("test") r = self.http.request('GET', 'http://127.0.0.1:5000/')