Skip to content

Commit d376df1

Browse files
authored
Use is_recording flag in requests instrumentation (#1087)
1 parent 23cf584 commit d376df1

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
# pylint: disable=unused-argument
5757
def _instrument(tracer_provider=None, span_callback=None):
5858
"""Enables tracing of all requests calls that go through
59-
:code:`requests.session.Session.request` (this includes
60-
:code:`requests.get`, etc.)."""
59+
:code:`requests.session.Session.request` (this includes
60+
:code:`requests.get`, etc.)."""
6161

6262
# Since
6363
# https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1
@@ -121,9 +121,10 @@ def _instrumented_requests_call(
121121
with get_tracer(
122122
__name__, __version__, tracer_provider
123123
).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
124-
span.set_attribute("component", "http")
125-
span.set_attribute("http.method", method.upper())
126-
span.set_attribute("http.url", url)
124+
if span.is_recording():
125+
span.set_attribute("component", "http")
126+
span.set_attribute("http.method", method.upper())
127+
span.set_attribute("http.url", url)
127128

128129
headers = get_or_create_headers()
129130
propagators.inject(type(headers).__setitem__, headers)
@@ -139,13 +140,13 @@ def _instrumented_requests_call(
139140
finally:
140141
context.detach(token)
141142

142-
if exception is not None:
143+
if exception is not None and span.is_recording():
143144
span.set_status(
144145
Status(_exception_to_canonical_code(exception))
145146
)
146147
span.record_exception(exception)
147148

148-
if result is not None:
149+
if result is not None and span.is_recording():
149150
span.set_attribute("http.status_code", result.status_code)
150151
span.set_attribute("http.status_text", result.reason)
151152
span.set_status(

Diff for: instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

+17
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ def test_suppress_instrumentation(self):
147147

148148
self.assert_span(num_spans=0)
149149

150+
def test_not_recording(self):
151+
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
152+
RequestsInstrumentor().uninstrument()
153+
# original_tracer_provider returns a default tracer provider, which
154+
# in turn will return an INVALID_SPAN, which is always not recording
155+
RequestsInstrumentor().instrument(
156+
tracer_provider=self.original_tracer_provider
157+
)
158+
mock_span.is_recording.return_value = False
159+
result = self.perform_request(self.URL)
160+
self.assertEqual(result.text, "Hello!")
161+
self.assert_span(None, 0)
162+
self.assertFalse(mock_span.is_recording())
163+
self.assertTrue(mock_span.is_recording.called)
164+
self.assertFalse(mock_span.set_attribute.called)
165+
self.assertFalse(mock_span.set_status.called)
166+
150167
def test_distributed_context(self):
151168
previous_propagator = propagators.get_global_textmap()
152169
try:

Diff for: tests/util/setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ package_dir=
3939
packages=find_namespace:
4040
install_requires =
4141
opentelemetry-api
42+
opentelemetry-sdk
4243

4344
[options.extras_require]
4445
test = flask~=1.0

0 commit comments

Comments
 (0)