Skip to content

Commit

Permalink
feat(urllib3)!: add method and url parameters to the request hook
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jul 16, 2024
1 parent 7e48ee7 commit cab3f75
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2580](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2580))
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `asgi` middleware
([#2610](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2610))
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `fastapi` middleware
([#2682](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2682))
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope for `fastapi` middleware
([#2682](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2682))
- `urllib3`: add `method` and `url` parameters to the request hook

### Fixed
- Handle `redis.exceptions.WatchError` as a non-error event in redis instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ def strip_query_params(url: str) -> str:
.. code:: python
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
def request_hook(span, request):
def request_hook(
span: Span,
instance: urllib3.connectionpool.HTTPConnectionPool,
method: str,
url: str,
headers: dict[str, str],
body: str | None,
):
pass
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
# `response` is an instance of urllib3.response.HTTPResponse
def response_hook(span, request, response):
def response_hook(
span: Span,
instance: urllib3.connectionpool.HTTPConnectionPool,
response: urllib3.response.HTTPResponse,
):
pass
URLLib3Instrumentor().instrument(
Expand Down Expand Up @@ -117,6 +125,8 @@ def response_hook(span, request, response):
[
Span,
urllib3.connectionpool.HTTPConnectionPool,
str,
str,
typing.Dict,
typing.Optional[str],
],
Expand Down Expand Up @@ -243,7 +253,7 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
span_name, kind=SpanKind.CLIENT, attributes=span_attributes
) as span, set_ip_on_next_http_connection(span):
if callable(request_hook):
request_hook(span, instance, headers, body)
request_hook(span, instance, method, url, headers, body)
inject(headers)

with suppress_http_instrumentation():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ def test_credential_removal(self):
self.assert_success_span(response, self.HTTP_URL)

def test_hooks(self):
def request_hook(span, request, body, headers):
def request_hook(span, instance, method, url, body, headers):
span.update_name("name set from hook")

def response_hook(span, request, response):
def response_hook(span, instance, response):
span.set_attribute("response_hook_attr", "value")

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

def test_request_hook_params(self):
def request_hook(span, request, headers, body):
def request_hook(span, instance, method, url, headers, body):
span.set_attribute("request_hook_method", method)
span.set_attribute("request_hook_url", url)
span.set_attribute(
"request_hook_headers", json.dumps(dict(headers))
)
Expand All @@ -358,6 +360,10 @@ def request_hook(span, request, headers, body):

span = self.assert_span()

self.assertEqual(span.attributes["request_hook_method"], "POST")
self.assertEqual(
span.attributes["request_hook_url"], "http://mock/status/200"
)
self.assertIn("request_hook_headers", span.attributes)
self.assertEqual(
span.attributes["request_hook_headers"], json.dumps(headers)
Expand All @@ -366,7 +372,7 @@ def request_hook(span, request, headers, body):
self.assertEqual(span.attributes["request_hook_body"], body)

def test_request_positional_body(self):
def request_hook(span, request, headers, body):
def request_hook(span, instance, method, url, headers, body):
span.set_attribute("request_hook_body", body)

URLLib3Instrumentor().uninstrument()
Expand Down

0 comments on commit cab3f75

Please sign in to comment.