Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)

This commit is contained in:
Leighton Chen
2020-09-30 10:36:57 -04:00
committed by alrex
parent e4a9b090b9
commit 3ae51232f0
2 changed files with 41 additions and 19 deletions

View File

@ -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

View File

@ -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")