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 authored and codeboten committed Oct 23, 2020
1 parent e81890f commit 602083e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.


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

from tornado.testing import AsyncHTTPTestCase

Expand Down Expand Up @@ -132,6 +132,21 @@ def _test_http_method_call(self, method):

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

Expand Down

0 comments on commit 602083e

Please sign in to comment.