Use is_recording flag in flask, django, tornado, boto, botocore instrumentations (#1164)

This commit is contained in:
Leighton Chen
2020-09-30 10:36:57 -04:00
committed by GitHub
parent e3199ac411
commit 287e026060
2 changed files with 25 additions and 8 deletions

View File

@ -101,21 +101,23 @@ class _DjangoMiddleware(MiddlewareMixin):
tracer = get_tracer(__name__, __version__) tracer = get_tracer(__name__, __version__)
attributes = collect_request_attributes(environ)
for attr in self._traced_request_attrs:
value = getattr(request, attr, None)
if value is not None:
attributes[attr] = str(value)
span = tracer.start_span( span = tracer.start_span(
self._get_span_name(request), self._get_span_name(request),
kind=SpanKind.SERVER, kind=SpanKind.SERVER,
attributes=attributes,
start_time=environ.get( start_time=environ.get(
"opentelemetry-instrumentor-django.starttime_key" "opentelemetry-instrumentor-django.starttime_key"
), ),
) )
if span.is_recording():
attributes = collect_request_attributes(environ)
for attr in self._traced_request_attrs:
value = getattr(request, attr, None)
if value is not None:
attributes[attr] = str(value)
for key, value in attributes.items():
span.set_attribute(key, value)
activation = tracer.use_span(span, end_on_exit=True) activation = tracer.use_span(span, end_on_exit=True)
activation.__enter__() activation.__enter__()

View File

@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
from sys import modules from sys import modules
from unittest.mock import patch from unittest.mock import Mock, patch
from django import VERSION from django import VERSION
from django.conf import settings from django.conf import settings
@ -89,6 +89,21 @@ class TestMiddleware(WsgiTestBase):
self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_code"], 200)
self.assertEqual(span.attributes["http.status_text"], "OK") self.assertEqual(span.attributes["http.status_text"], "OK")
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
Client().get("/traced/")
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_traced_post(self): def test_traced_post(self):
Client().post("/traced/") Client().post("/traced/")