Skip to content

Commit b9a2afe

Browse files
committed
feat(urllib3)!: add method and url parameters to the request hook
1 parent 7e48ee7 commit b9a2afe

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
([#2580](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2580))
3939
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `asgi` middleware
4040
([#2610](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2610))
41-
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `fastapi` middleware
42-
([#2682](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2682))
41+
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `fastapi` middleware
42+
([#2682](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2682))
43+
- `urllib3`: add `method` and `url` parameters to the request hook
4344

4445
### Fixed
4546
- Handle `redis.exceptions.WatchError` as a non-error event in redis instrumentation

instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,21 @@ def strip_query_params(url: str) -> str:
4747
4848
.. code:: python
4949
50-
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
51-
def request_hook(span, request):
50+
def request_hook(
51+
span: Span,
52+
instance: urllib3.connectionpool.HTTPConnectionPool,
53+
method: str,
54+
url: str,
55+
headers: dict[str, str],
56+
body: str | None,
57+
):
5258
pass
5359
54-
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
55-
# `response` is an instance of urllib3.response.HTTPResponse
56-
def response_hook(span, request, response):
60+
def response_hook(
61+
span: Span,
62+
instance: urllib3.connectionpool.HTTPConnectionPool,
63+
response: urllib3.response.HTTPResponse,
64+
):
5765
pass
5866
5967
URLLib3Instrumentor().instrument(
@@ -117,6 +125,8 @@ def response_hook(span, request, response):
117125
[
118126
Span,
119127
urllib3.connectionpool.HTTPConnectionPool,
128+
str,
129+
str,
120130
typing.Dict,
121131
typing.Optional[str],
122132
],
@@ -243,7 +253,7 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
243253
span_name, kind=SpanKind.CLIENT, attributes=span_attributes
244254
) as span, set_ip_on_next_http_connection(span):
245255
if callable(request_hook):
246-
request_hook(span, instance, headers, body)
256+
request_hook(span, instance, method, url, headers, body)
247257
inject(headers)
248258

249259
with suppress_http_instrumentation():

instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ def test_credential_removal(self):
315315
self.assert_success_span(response, self.HTTP_URL)
316316

317317
def test_hooks(self):
318-
def request_hook(span, request, body, headers):
318+
def request_hook(span, instance, method, url, body, headers):
319319
span.update_name("name set from hook")
320320

321-
def response_hook(span, request, response):
321+
def response_hook(span, instance, response):
322322
span.set_attribute("response_hook_attr", "value")
323323

324324
URLLib3Instrumentor().uninstrument()
@@ -335,7 +335,9 @@ def response_hook(span, request, response):
335335
self.assertEqual(span.attributes["response_hook_attr"], "value")
336336

337337
def test_request_hook_params(self):
338-
def request_hook(span, request, headers, body):
338+
def request_hook(span, instance, method, url, headers, body):
339+
span.set_attribute("request_hook_method", method)
340+
span.set_attribute("request_hook_url", url)
339341
span.set_attribute(
340342
"request_hook_headers", json.dumps(dict(headers))
341343
)
@@ -358,6 +360,10 @@ def request_hook(span, request, headers, body):
358360

359361
span = self.assert_span()
360362

363+
self.assertEqual(span.attributes["request_hook_method"], "POST")
364+
self.assertEqual(
365+
span.attributes["request_hook_url"], "http://mock/status/200"
366+
)
361367
self.assertIn("request_hook_headers", span.attributes)
362368
self.assertEqual(
363369
span.attributes["request_hook_headers"], json.dumps(headers)
@@ -366,7 +372,7 @@ def request_hook(span, request, headers, body):
366372
self.assertEqual(span.attributes["request_hook_body"], body)
367373

368374
def test_request_positional_body(self):
369-
def request_hook(span, request, headers, body):
375+
def request_hook(span, instance, method, url, headers, body):
370376
span.set_attribute("request_hook_body", body)
371377

372378
URLLib3Instrumentor().uninstrument()

0 commit comments

Comments
 (0)