diff --git a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py index bfcd45a8b..0a93fe013 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py @@ -117,13 +117,16 @@ class _InstrumentedFalconAPI(falcon.API): token = context.attach( propagators.extract(otel_wsgi.get_header_from_environ, env) ) - attributes = otel_wsgi.collect_request_attributes(env) span = self._tracer.start_span( otel_wsgi.get_default_span_name(env), kind=trace.SpanKind.SERVER, - attributes=attributes, start_time=start_time, ) + if span.is_recording(): + attributes = otel_wsgi.collect_request_attributes(env) + for key, value in attributes.items(): + span.set_attribute(key, value) + activation = self._tracer.use_span(span, end_on_exit=True) activation.__enter__() env[_ENVIRON_SPAN_KEY] = span @@ -162,7 +165,7 @@ class _TraceMiddleware: def process_request(self, req, resp): span = req.env.get(_ENVIRON_SPAN_KEY) - if not span: + if not span or not span.is_recording(): return attributes = extract_attributes_from_object( @@ -173,7 +176,7 @@ class _TraceMiddleware: def process_resource(self, req, resp, resource, params): span = req.env.get(_ENVIRON_SPAN_KEY) - if not span: + if not span or not span.is_recording(): return resource_name = resource.__class__.__name__ @@ -186,7 +189,7 @@ class _TraceMiddleware: self, req, resp, resource, req_succeeded=None ): # pylint:disable=R0201 span = req.env.get(_ENVIRON_SPAN_KEY) - if not span: + if not span or not span.is_recording(): return status = resp.status diff --git a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py index 5e27e4aac..d64154a77 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest.mock import patch +from unittest.mock import Mock, patch from falcon import testing @@ -188,3 +188,18 @@ class TestFalconInstrumentation(TestBase): span = self.memory_exporter.get_finished_spans()[0] self.assertIn("query_string", span.attributes) self.assertEqual(span.attributes["query_string"], "q=abc") + + def test_traced_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.client().simulate_get(path="/hello?q=abc") + 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)