Skip to content

Commit c1dd69e

Browse files
authored
Fix tests for urllib3 v2.0.1+ (#1822)
* Fix tests for urllib3 >2.0 * Update urllib3 version pins * CHANGELOG
1 parent 21fb04a commit c1dd69e

File tree

7 files changed

+22
-7
lines changed

7 files changed

+22
-7
lines changed

CHANGELOG.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ endif::[]
4242
* Add S3 bucket and key name to OTel attributes {pull}1790[#1790]
4343
* Implement partial transaction support in AWS lambda {pull}1784[#1784]
4444
* Add instrumentation for redis.asyncio {pull}1807[#1807]
45+
* Add support for urllib3 v2.0.1+ {pull}1822[#1822]
4546
4647
[float]
4748
===== Bug fixes

elasticapm/instrumentation/packages/urllib3.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ def update_headers(args, kwargs, instance, transaction, trace_parent):
6161
:param trace_parent: the TraceParent object
6262
:return: an (args, kwargs) tuple
6363
"""
64-
if len(args) >= 4 and args[3]:
64+
from urllib3._version import __version__ as urllib3_version
65+
66+
if urllib3_version.startswith("2") and len(args) >= 5 and args[4]:
67+
headers = args[4].copy()
68+
args = tuple(itertools.chain((args[:4]), (headers,), args[5:]))
69+
elif len(args) >= 4 and args[3]:
6570
headers = args[3].copy()
6671
args = tuple(itertools.chain((args[:3]), (headers,), args[4:]))
6772
elif "headers" in kwargs and kwargs["headers"]:

setup.cfg

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ packages = find:
3636
include_package_data = true
3737
zip_safe = false
3838
install_requires =
39-
urllib3<2.0
39+
urllib3!=2.0.0,<3.0.0
4040
certifi
4141
wrapt>=1.14.1
4242
test_suite=tests
@@ -71,7 +71,7 @@ tests_require =
7171
py-cpuinfo==3.3.0
7272
statistics==1.0.3.5
7373

74-
urllib3<2.0
74+
urllib3!=2.0.0,<3.0.0
7575
certifi
7676
Jinja2
7777
Logbook

tests/client/client_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def test_check_server_version(elasticapm_client):
530530
indirect=["elasticapm_client"],
531531
)
532532
def test_user_agent(elasticapm_client, expected):
533-
assert elasticapm_client.get_user_agent() == "apm-agent-python/unknown (myapp{})".format(expected)
533+
assert elasticapm_client.get_user_agent() == "apm-agent-python/{} (myapp{})".format(elasticapm.VERSION, expected)
534534

535535

536536
def test_label_without_client():

tests/client/exception_tests.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ def test_message_event(elasticapm_client):
122122
assert "stacktrace" in event["log"]
123123
# check that only frames from `tests` module are not marked as library frames
124124
for frame in event["log"]["stacktrace"]:
125-
assert frame["library_frame"] or frame["module"].startswith(("tests", "__main__")), (
125+
# vscode's python extension runs tests using libraries from the
126+
# extension's dir, which results in false positives here. This is where
127+
# runpy, _pydevd, and debugpy come from.
128+
assert frame["library_frame"] or frame["module"].startswith(
129+
("tests", "__main__", "runpy", "_pydevd", "debugpy")
130+
), (
126131
frame["module"],
127132
frame["abs_path"],
128133
)

tests/instrumentation/urllib3_tests.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import pytest
3333
import urllib3
34+
from urllib3._version import __version__ as urllib3_version
3435

3536
from elasticapm.conf import constants
3637
from elasticapm.conf.constants import SPAN, TRANSACTION
@@ -273,7 +274,10 @@ def test_instance_headers_are_respected(
273274
headers={"instance": "true"} if instance_headers else None,
274275
)
275276
if header_arg:
276-
args = ("GET", url, None, {"args": "true"})
277+
if urllib3_version.startswith("2"):
278+
args = ("GET", url, None, None, {"args": "true"})
279+
else:
280+
args = ("GET", url, None, {"args": "true"})
277281
else:
278282
args = ("GET", url)
279283
if header_kwarg:

tests/requirements/reqs-base.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jsonschema==3.2.0 ; python_version == '3.6'
1818
jsonschema==4.17.3 ; python_version > '3.6'
1919

2020

21-
urllib3<2.0
21+
urllib3!=2.0.0,<3.0.0
2222
certifi
2323
Logbook
2424
mock

0 commit comments

Comments
 (0)