diff --git a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py index 8e03cd6e7499..3bef955d1481 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py @@ -136,27 +136,31 @@ def _common_request( # pylint: disable=too-many-locals attributes={"endpoint": endpoint_name} ) - add_span_arg_tags( - span, endpoint_name, args, args_name, traced_args, - ) + # Original func returns a boto.connection.HTTPResponse object + result = original_func(*args, **kwargs) - # Obtaining region name - region_name = _get_instance_region_name(instance) + if span.is_recording(): + add_span_arg_tags( + span, endpoint_name, args, args_name, traced_args, + ) - meta = { - "aws.agent": "boto", - "aws.operation": operation_name, - } - if region_name: - meta["aws.region"] = region_name + # Obtaining region name + region_name = _get_instance_region_name(instance) - for key, value in meta.items(): - span.set_attribute(key, value) + meta = { + "aws.agent": "boto", + "aws.operation": operation_name, + } + if region_name: + meta["aws.region"] = region_name - # Original func returns a boto.connection.HTTPResponse object - result = original_func(*args, **kwargs) - span.set_attribute("http.status_code", getattr(result, "status")) - span.set_attribute("http.method", getattr(result, "_method")) + for key, value in meta.items(): + span.set_attribute(key, value) + + span.set_attribute( + "http.status_code", getattr(result, "status") + ) + span.set_attribute("http.method", getattr(result, "_method")) return result diff --git a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py index 0a4a4b8869a0..1a8cc2b387fc 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py @@ -13,6 +13,7 @@ # limitations under the License. from unittest import skipUnless +from unittest.mock import Mock, patch import boto.awslambda import boto.ec2 @@ -80,6 +81,23 @@ def test_ec2_client(self): self.assertEqual(span.attributes["aws.region"], "us-west-2") self.assertEqual(span.name, "ec2.command") + @mock_ec2_deprecated + 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 + ec2 = boto.ec2.connect_to_region("us-west-2") + ec2.get_all_instances() + 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) + @mock_ec2_deprecated def test_analytics_enabled_with_rate(self): ec2 = boto.ec2.connect_to_region("us-west-2")