From 3ae51232f0e70d747109b3a18b193df679dae8a1 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/boto/__init__.py | 42 ++++++++++--------- .../tests/test_boto_instrumentation.py | 18 ++++++++ 2 files changed, 41 insertions(+), 19 deletions(-) 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 8e03cd6e7..3bef955d1 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 @@ class BotoInstrumentor(BaseInstrumentor): attributes={"endpoint": endpoint_name} ) - add_span_arg_tags( - span, endpoint_name, args, args_name, traced_args, - ) - - # Obtaining region name - region_name = _get_instance_region_name(instance) - - meta = { - "aws.agent": "boto", - "aws.operation": operation_name, - } - if region_name: - meta["aws.region"] = region_name - - for key, value in meta.items(): - span.set_attribute(key, value) - # 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")) + + if span.is_recording(): + add_span_arg_tags( + span, endpoint_name, args, args_name, traced_args, + ) + + # Obtaining region name + region_name = _get_instance_region_name(instance) + + meta = { + "aws.agent": "boto", + "aws.operation": operation_name, + } + if region_name: + meta["aws.region"] = region_name + + 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 0a4a4b886..1a8cc2b38 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 @@ class TestBotoInstrumentor(TestBase): 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")