Use is_recording flag in requests instrumentation (#1087)
This commit is contained in:
parent
2d5adef109
commit
da202b05f6
|
|
@ -56,8 +56,8 @@ _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY = "suppress_requests_instrumentation"
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def _instrument(tracer_provider=None, span_callback=None):
|
def _instrument(tracer_provider=None, span_callback=None):
|
||||||
"""Enables tracing of all requests calls that go through
|
"""Enables tracing of all requests calls that go through
|
||||||
:code:`requests.session.Session.request` (this includes
|
:code:`requests.session.Session.request` (this includes
|
||||||
:code:`requests.get`, etc.)."""
|
:code:`requests.get`, etc.)."""
|
||||||
|
|
||||||
# Since
|
# Since
|
||||||
# https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1
|
# https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1
|
||||||
|
|
@ -121,9 +121,10 @@ def _instrument(tracer_provider=None, span_callback=None):
|
||||||
with get_tracer(
|
with get_tracer(
|
||||||
__name__, __version__, tracer_provider
|
__name__, __version__, tracer_provider
|
||||||
).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
|
).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
|
||||||
span.set_attribute("component", "http")
|
if span.is_recording():
|
||||||
span.set_attribute("http.method", method.upper())
|
span.set_attribute("component", "http")
|
||||||
span.set_attribute("http.url", url)
|
span.set_attribute("http.method", method.upper())
|
||||||
|
span.set_attribute("http.url", url)
|
||||||
|
|
||||||
headers = get_or_create_headers()
|
headers = get_or_create_headers()
|
||||||
propagators.inject(type(headers).__setitem__, headers)
|
propagators.inject(type(headers).__setitem__, headers)
|
||||||
|
|
@ -139,13 +140,13 @@ def _instrument(tracer_provider=None, span_callback=None):
|
||||||
finally:
|
finally:
|
||||||
context.detach(token)
|
context.detach(token)
|
||||||
|
|
||||||
if exception is not None:
|
if exception is not None and span.is_recording():
|
||||||
span.set_status(
|
span.set_status(
|
||||||
Status(_exception_to_canonical_code(exception))
|
Status(_exception_to_canonical_code(exception))
|
||||||
)
|
)
|
||||||
span.record_exception(exception)
|
span.record_exception(exception)
|
||||||
|
|
||||||
if result is not None:
|
if result is not None and span.is_recording():
|
||||||
span.set_attribute("http.status_code", result.status_code)
|
span.set_attribute("http.status_code", result.status_code)
|
||||||
span.set_attribute("http.status_text", result.reason)
|
span.set_attribute("http.status_text", result.reason)
|
||||||
span.set_status(
|
span.set_status(
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,23 @@ class RequestsIntegrationTestBase(abc.ABC):
|
||||||
|
|
||||||
self.assert_span(num_spans=0)
|
self.assert_span(num_spans=0)
|
||||||
|
|
||||||
|
def test_not_recording(self):
|
||||||
|
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
|
||||||
|
RequestsInstrumentor().uninstrument()
|
||||||
|
# original_tracer_provider returns a default tracer provider, which
|
||||||
|
# in turn will return an INVALID_SPAN, which is always not recording
|
||||||
|
RequestsInstrumentor().instrument(
|
||||||
|
tracer_provider=self.original_tracer_provider
|
||||||
|
)
|
||||||
|
mock_span.is_recording.return_value = False
|
||||||
|
result = self.perform_request(self.URL)
|
||||||
|
self.assertEqual(result.text, "Hello!")
|
||||||
|
self.assert_span(None, 0)
|
||||||
|
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_distributed_context(self):
|
def test_distributed_context(self):
|
||||||
previous_propagator = propagators.get_global_textmap()
|
previous_propagator = propagators.get_global_textmap()
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue