mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 17:34:38 +08:00
Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)
This commit is contained in:
@ -136,27 +136,31 @@ class BotoInstrumentor(BaseInstrumentor):
|
|||||||
attributes={"endpoint": endpoint_name}
|
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
|
# Original func returns a boto.connection.HTTPResponse object
|
||||||
result = original_func(*args, **kwargs)
|
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
|
return result
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from unittest import skipUnless
|
from unittest import skipUnless
|
||||||
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import boto.awslambda
|
import boto.awslambda
|
||||||
import boto.ec2
|
import boto.ec2
|
||||||
@ -80,6 +81,23 @@ class TestBotoInstrumentor(TestBase):
|
|||||||
self.assertEqual(span.attributes["aws.region"], "us-west-2")
|
self.assertEqual(span.attributes["aws.region"], "us-west-2")
|
||||||
self.assertEqual(span.name, "ec2.command")
|
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
|
@mock_ec2_deprecated
|
||||||
def test_analytics_enabled_with_rate(self):
|
def test_analytics_enabled_with_rate(self):
|
||||||
ec2 = boto.ec2.connect_to_region("us-west-2")
|
ec2 = boto.ec2.connect_to_region("us-west-2")
|
||||||
|
Reference in New Issue
Block a user