Skip to content

Commit

Permalink
Use is_recording flag in flask, django, tornado, boto, botocore instr…
Browse files Browse the repository at this point in the history
…umentations (#1164)
  • Loading branch information
lzchen committed Sep 30, 2020
1 parent 5446a25 commit 0990b56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from unittest import skipUnless
from unittest.mock import Mock, patch

import boto.awslambda
import boto.ec2
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 0990b56

Please sign in to comment.