Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)
This commit is contained in:
parent
a52e65d10a
commit
1c145baa69
|
|
@ -38,15 +38,17 @@ def fetch_async(tracer, func, _, args, kwargs):
|
|||
request = args[0]
|
||||
|
||||
span = tracer.start_span(
|
||||
request.method,
|
||||
kind=trace.SpanKind.CLIENT,
|
||||
attributes={
|
||||
request.method, kind=trace.SpanKind.CLIENT, start_time=start_time,
|
||||
)
|
||||
|
||||
if span.is_recording():
|
||||
attributes = {
|
||||
"component": "tornado",
|
||||
"http.url": request.url,
|
||||
"http.method": request.method,
|
||||
},
|
||||
start_time=start_time,
|
||||
)
|
||||
}
|
||||
for key, value in attributes.items():
|
||||
span.set_attribute(key, value)
|
||||
|
||||
with tracer.use_span(span):
|
||||
propagators.inject(type(request.headers).__setitem__, request.headers)
|
||||
|
|
@ -61,7 +63,7 @@ def _finish_tracing_callback(future, span):
|
|||
status_code = None
|
||||
description = None
|
||||
exc = future.exception()
|
||||
if exc:
|
||||
if span.is_recording() and exc:
|
||||
if isinstance(exc, HTTPError):
|
||||
status_code = exc.code
|
||||
description = "{}: {}".format(type(exc).__name__, exc)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
# limitations under the License.
|
||||
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from tornado.testing import AsyncHTTPTestCase
|
||||
|
||||
|
|
@ -132,6 +132,21 @@ class TestTornadoInstrumentation(TornadoTest):
|
|||
|
||||
self.memory_exporter.clear()
|
||||
|
||||
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
|
||||
self.fetch("/")
|
||||
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)
|
||||
|
||||
def test_async_handler(self):
|
||||
self._test_async_handler("/async", "AsyncHandler")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue