Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2e671a

Browse files
authored
ext/requests - apply custom attributes callback (open-telemetry#656)
Implement passing an optional span_callback callback when instrumenting requests. The callback will be called just before returning the result and will be invoked with the span and the result (type of requests.Response). This mimics the same functionality as the js http plugin.
1 parent f15230b commit b2e671a

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

ext/opentelemetry-ext-requests/src/opentelemetry/ext/requests/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656

5757
# pylint: disable=unused-argument
58-
def _instrument(tracer_provider=None):
58+
def _instrument(tracer_provider=None, span_callback=None):
5959
"""Enables tracing of all requests calls that go through
6060
:code:`requests.session.Session.request` (this includes
6161
:code:`requests.get`, etc.)."""
@@ -101,6 +101,8 @@ def instrumented_request(self, method, url, *args, **kwargs):
101101
span.set_status(
102102
Status(_http_status_to_canonical_code(result.status_code))
103103
)
104+
if span_callback is not None:
105+
span_callback(span, result)
104106

105107
return result
106108

@@ -156,8 +158,22 @@ def _http_status_to_canonical_code(code: int, allow_redirect: bool = True):
156158

157159

158160
class RequestsInstrumentor(BaseInstrumentor):
161+
"""An instrumentor for requests
162+
See `BaseInstrumentor`
163+
"""
164+
159165
def _instrument(self, **kwargs):
160-
_instrument(tracer_provider=kwargs.get("tracer_provider"))
166+
"""Instruments requests module
167+
168+
Args:
169+
**kwargs: Optional arguments
170+
``tracer_provider``: a TracerProvider, defaults to global
171+
``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response
172+
"""
173+
_instrument(
174+
tracer_provider=kwargs.get("tracer_provider"),
175+
span_callback=kwargs.get("span_callback"),
176+
)
161177

162178
def _uninstrument(self, **kwargs):
163179
_uninstrument()

ext/opentelemetry-ext-requests/tests/test_requests_integration.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,37 @@ def test_distributed_context(self):
183183
finally:
184184
propagators.set_global_httptextformat(previous_propagator)
185185

186+
def test_span_callback(self):
187+
RequestsInstrumentor().uninstrument()
188+
189+
def span_callback(span, result: requests.Response):
190+
span.set_attribute(
191+
"http.response.body", result.content.decode("utf-8")
192+
)
193+
194+
RequestsInstrumentor().instrument(
195+
tracer_provider=self.tracer_provider, span_callback=span_callback,
196+
)
197+
198+
result = requests.get(self.URL)
199+
self.assertEqual(result.text, "Hello!")
200+
201+
span_list = self.memory_exporter.get_finished_spans()
202+
self.assertEqual(len(span_list), 1)
203+
span = span_list[0]
204+
205+
self.assertEqual(
206+
span.attributes,
207+
{
208+
"component": "http",
209+
"http.method": "GET",
210+
"http.url": self.URL,
211+
"http.status_code": 200,
212+
"http.status_text": "OK",
213+
"http.response.body": "Hello!",
214+
},
215+
)
216+
186217
def test_custom_tracer_provider(self):
187218
resource = resources.Resource.create({})
188219
result = self.create_tracer_provider(resource=resource)

0 commit comments

Comments
 (0)