From 602083eae3902960ad781de9d9a2567702a7dfc2 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 30 Sep 2020 10:36:57 -0400 Subject: [PATCH] Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164) --- .../instrumentation/tornado/client.py | 16 +++++++++------- .../tests/test_instrumentation.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py index 1fe8f58a2c71..12330c0919db 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py @@ -38,15 +38,17 @@ def fetch_async(tracer, func, _, args, kwargs): request = args[0] span = tracer.start_span( - request.method, - kind=trace.SpanKind.CLIENT, - attributes={ + request.method, kind=trace.SpanKind.CLIENT, start_time=start_time, + ) + + if span.is_recording(): + attributes = { "component": "tornado", "http.url": request.url, "http.method": request.method, - }, - start_time=start_time, - ) + } + for key, value in attributes.items(): + span.set_attribute(key, value) with tracer.use_span(span): propagators.inject(type(request.headers).__setitem__, request.headers) @@ -61,7 +63,7 @@ def _finish_tracing_callback(future, span): status_code = None description = None exc = future.exception() - if exc: + if span.is_recording() and exc: if isinstance(exc, HTTPError): status_code = exc.code description = "{}: {}".format(type(exc).__name__, exc) diff --git a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py index 2d5ffd00f78e..d900b5d360e1 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py @@ -13,7 +13,7 @@ # limitations under the License. -from unittest.mock import patch +from unittest.mock import Mock, patch from tornado.testing import AsyncHTTPTestCase @@ -132,6 +132,21 @@ def _test_http_method_call(self, method): self.memory_exporter.clear() + def test_not_recording(self): + mock_tracer = Mock() + mock_span = Mock() + mock_span.is_recording.return_value = False + mock_tracer.start_span.return_value = mock_span + mock_tracer.use_span.return_value.__enter__ = mock_span + mock_tracer.use_span.return_value.__exit__ = mock_span + with patch("opentelemetry.trace.get_tracer") as tracer: + tracer.return_value = mock_tracer + self.fetch("/") + self.assertFalse(mock_span.is_recording()) + self.assertTrue(mock_span.is_recording.called) + self.assertFalse(mock_span.set_attribute.called) + self.assertFalse(mock_span.set_status.called) + def test_async_handler(self): self._test_async_handler("/async", "AsyncHandler")