Skip to content

Commit 3ca8a74

Browse files
committed
Capture full URL in urllib3 spans
Spotted whilst debugging a customer's traces.
1 parent 631f243 commit 3ca8a74

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Pending
4+
5+
### Fixed
6+
7+
- Capture the full URL in `urllib3` spans.
8+
39
## [2.16.2] 2020-09-17
410

511
- Moved core agent on Linux to default to the musl version, rather than try

src/scout_apm/instruments/urllib3.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,22 @@ def ensure_installed():
4343

4444
@wrapt.decorator
4545
def wrapped_urlopen(wrapped, instance, args, kwargs):
46-
def _extract_method(method, *args, **kwargs):
47-
return method
46+
def _extract_method_url(method, url, *args, **kwargs):
47+
return method, url
4848

4949
try:
50-
method = _extract_method(*args, **kwargs)
50+
method, url = _extract_method_url(*args, **kwargs)
5151
except TypeError:
5252
method = "Unknown"
53-
54-
try:
55-
url = text_type(instance._absolute_url("/"))
56-
except Exception:
57-
logger.exception("Could not get URL for HTTPConnectionPool")
5853
url = "Unknown"
54+
else:
55+
try:
56+
url = text_type(instance._absolute_url(url))
57+
except Exception:
58+
logger.exception("Could not get URL for HTTPConnectionPool")
59+
url = "Unknown"
5960

6061
tracked_request = TrackedRequest.instance()
6162
with tracked_request.span(operation="HTTP/{}".format(method)) as span:
62-
span.tag("url", text_type(url))
63+
span.tag("url", url)
6364
return wrapped(*args, **kwargs)

tests/integration/instruments/test_urllib3.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ def test_request(tracked_request):
9191
assert span.tags["url"] == "https://example.com:443/"
9292

9393

94+
def test_second_request(tracked_request):
95+
ensure_installed()
96+
with tracked_request.span("Test"), httpretty.enabled(allow_net_connect=False):
97+
httpretty.register_uri(
98+
httpretty.GET, "https://example.com/foo", body="Hello World!"
99+
)
100+
httpretty.register_uri(
101+
httpretty.GET, "https://example.org/bar", body="Hello World!"
102+
)
103+
104+
http = urllib3_cert_pool_manager()
105+
http.request("GET", "https://example.com/foo")
106+
http.request("GET", "https://example.org/bar")
107+
108+
assert len(tracked_request.complete_spans) == 3
109+
assert (
110+
tracked_request.complete_spans[0].tags["url"] == "https://example.com:443/foo"
111+
)
112+
assert (
113+
tracked_request.complete_spans[1].tags["url"] == "https://example.org:443/bar"
114+
)
115+
116+
94117
def test_request_type_error(tracked_request):
95118
ensure_installed()
96119
with pytest.raises(TypeError):
@@ -101,7 +124,7 @@ def test_request_type_error(tracked_request):
101124
assert len(tracked_request.complete_spans) == 1
102125
span = tracked_request.complete_spans[0]
103126
assert span.operation == "HTTP/Unknown"
104-
assert span.tags["url"] == "https://example.com:443/"
127+
assert span.tags["url"] == "Unknown"
105128

106129

107130
def test_request_no_absolute_url(caplog, tracked_request):

0 commit comments

Comments
 (0)