Skip to content

Commit ae1326b

Browse files
committed
only add span attrs for proxies
1 parent 63034d6 commit ae1326b

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ def _install_httplib() -> None:
6969
def putrequest(
7070
self: "HTTPConnection", method: str, url: str, *args: "Any", **kwargs: "Any"
7171
) -> "Any":
72-
# proxy info is in _tunnel_host/_tunnel_port
73-
host = getattr(self, "_tunnel_host", None) or self.host
74-
7572
default_port = self.default_port
76-
tunnel_port = getattr(self, "_tunnel_port", None)
77-
if tunnel_port:
78-
port = tunnel_port
79-
# need to override default_port for correct url recording
80-
if tunnel_port == 443:
81-
default_port = 443
73+
74+
# proxies go through set_tunnel
75+
tunnel_host = getattr(self, "_tunnel_host", None)
76+
if tunnel_host:
77+
host = tunnel_host
78+
port = getattr(self, "_tunnel_port", None)
8279
else:
80+
host = self.host
8381
port = self.port
8482

83+
8584
client = sentry_sdk.get_client()
8685
if client.get_integration(StdlibIntegration) is None or is_sentry_url(
8786
client, host
@@ -113,10 +112,10 @@ def putrequest(
113112
span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
114113
span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
115114

116-
# Set network peer address and port (the actual connection endpoint)
117115
# for proxies, these point to the proxy host/port
118-
span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, self.host)
119-
span.set_data(SPANDATA.NETWORK_PEER_PORT, self.port)
116+
if tunnel_host:
117+
span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, self.host)
118+
span.set_data(SPANDATA.NETWORK_PEER_PORT, self.port)
120119

121120
rv = real_putrequest(self, method, url, *args, **kwargs)
122121

tests/integrations/stdlib/test_httplib.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,23 @@ def test_http_timeout(monkeypatch, sentry_init, capture_envelopes):
671671
assert span["description"] == f"GET http://localhost:{PORT}/bla" # noqa: E231
672672

673673

674-
def test_proxy_https_tunnel(sentry_init, capture_events):
674+
@pytest.mark.parametrize("tunnel_port", [8080, None])
675+
def test_proxy_http_tunnel(sentry_init, capture_events, tunnel_port):
675676
sentry_init(traces_sample_rate=1.0)
676677
events = capture_events()
677678

678679
with start_transaction(name="test_transaction"):
679680
conn = HTTPConnection("localhost", PROXY_PORT)
680-
conn.set_tunnel("api.example.com", 443)
681+
conn.set_tunnel("api.example.com", tunnel_port)
681682
conn.request("GET", "/foo")
682683
conn.getresponse()
683684

684685
(event,) = events
685686
(span,) = event["spans"]
686687

687-
assert span["description"] == "GET https://api.example.com/foo"
688-
assert span["data"]["url"] == "https://api.example.com/foo"
688+
port_modifier = f":{tunnel_port}" if tunnel_port else ''
689+
assert span["description"] == f"GET http://api.example.com{port_modifier}/foo"
690+
assert span["data"]["url"] == f"http://api.example.com{port_modifier}/foo"
689691
assert span["data"][SPANDATA.HTTP_METHOD] == "GET"
690692
assert span["data"][SPANDATA.NETWORK_PEER_ADDRESS] == "localhost"
691693
assert span["data"][SPANDATA.NETWORK_PEER_PORT] == PROXY_PORT

0 commit comments

Comments
 (0)