Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)
This commit is contained in:
parent
50f6921142
commit
1bf9f928d3
|
|
@ -97,7 +97,7 @@ class BotocoreInstrumentor(BaseInstrumentor):
|
||||||
) as span:
|
) as span:
|
||||||
|
|
||||||
operation = None
|
operation = None
|
||||||
if args:
|
if args and span.is_recording():
|
||||||
operation = args[0]
|
operation = args[0]
|
||||||
span.resource = Resource(
|
span.resource = Resource(
|
||||||
attributes={
|
attributes={
|
||||||
|
|
@ -119,25 +119,28 @@ class BotocoreInstrumentor(BaseInstrumentor):
|
||||||
{"params", "path", "verb"},
|
{"params", "path", "verb"},
|
||||||
)
|
)
|
||||||
|
|
||||||
region_name = deep_getattr(instance, "meta.region_name")
|
if span.is_recording():
|
||||||
|
region_name = deep_getattr(instance, "meta.region_name")
|
||||||
|
|
||||||
meta = {
|
meta = {
|
||||||
"aws.agent": "botocore",
|
"aws.agent": "botocore",
|
||||||
"aws.operation": operation,
|
"aws.operation": operation,
|
||||||
"aws.region": region_name,
|
"aws.region": region_name,
|
||||||
}
|
}
|
||||||
for key, value in meta.items():
|
for key, value in meta.items():
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
|
|
||||||
result = original_func(*args, **kwargs)
|
result = original_func(*args, **kwargs)
|
||||||
|
|
||||||
span.set_attribute(
|
if span.is_recording():
|
||||||
"http.status_code",
|
span.set_attribute(
|
||||||
result["ResponseMetadata"]["HTTPStatusCode"],
|
"http.status_code",
|
||||||
)
|
result["ResponseMetadata"]["HTTPStatusCode"],
|
||||||
span.set_attribute(
|
)
|
||||||
"retry_attempts", result["ResponseMetadata"]["RetryAttempts"],
|
span.set_attribute(
|
||||||
)
|
"retry_attempts",
|
||||||
|
result["ResponseMetadata"]["RetryAttempts"],
|
||||||
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
@ -177,6 +180,9 @@ def add_span_arg_tags(span, endpoint_name, args, args_names, args_traced):
|
||||||
else {prefix: dict_}
|
else {prefix: dict_}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not span.is_recording():
|
||||||
|
return
|
||||||
|
|
||||||
if endpoint_name not in {"kms", "sts"}:
|
if endpoint_name not in {"kms", "sts"}:
|
||||||
tags = dict(
|
tags = dict(
|
||||||
(name, value)
|
(name, value)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,19 @@
|
||||||
|
# Copyright The OpenTelemetry Authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import botocore.session
|
import botocore.session
|
||||||
from botocore.exceptions import ParamValidationError
|
from botocore.exceptions import ParamValidationError
|
||||||
from moto import ( # pylint: disable=import-error
|
from moto import ( # pylint: disable=import-error
|
||||||
|
|
@ -61,6 +77,23 @@ class TestBotocoreInstrumentor(TestBase):
|
||||||
)
|
)
|
||||||
self.assertEqual(span.name, "ec2.command")
|
self.assertEqual(span.name, "ec2.command")
|
||||||
|
|
||||||
|
@mock_ec2
|
||||||
|
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 = self.session.create_client("ec2", region_name="us-west-2")
|
||||||
|
ec2.describe_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
|
@mock_ec2
|
||||||
def test_traced_client_analytics(self):
|
def test_traced_client_analytics(self):
|
||||||
ec2 = self.session.create_client("ec2", region_name="us-west-2")
|
ec2 = self.session.create_client("ec2", region_name="us-west-2")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue