Skip to content

Commit ea832e5

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

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,11 @@ 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-
75-
default_port = self.default_port
72+
tunnel_host = getattr(self, "_tunnel_host", None)
73+
host = tunnel_host or self.host
7674
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
82-
else:
83-
port = self.port
75+
port = tunnel_port or self.port
76+
default_port = self.default_port
8477

8578
client = sentry_sdk.get_client()
8679
if client.get_integration(StdlibIntegration) is None or is_sentry_url(
@@ -113,10 +106,11 @@ def putrequest(
113106
span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
114107
span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
115108

116-
# Set network peer address and port (the actual connection endpoint)
117109
# 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)
110+
if tunnel_host:
111+
span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, self.host)
112+
if tunnel_port:
113+
span.set_data(SPANDATA.NETWORK_PEER_PORT, self.port)
120114

121115
rv = real_putrequest(self, method, url, *args, **kwargs)
122116

tests/integrations/stdlib/test_httplib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,21 @@ 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+
def test_proxy_http_tunnel(sentry_init, capture_events):
675675
sentry_init(traces_sample_rate=1.0)
676676
events = capture_events()
677677

678678
with start_transaction(name="test_transaction"):
679679
conn = HTTPConnection("localhost", PROXY_PORT)
680-
conn.set_tunnel("api.example.com", 443)
680+
conn.set_tunnel("api.example.com", 8080)
681681
conn.request("GET", "/foo")
682682
conn.getresponse()
683683

684684
(event,) = events
685685
(span,) = event["spans"]
686686

687-
assert span["description"] == "GET https://api.example.com/foo"
688-
assert span["data"]["url"] == "https://api.example.com/foo"
687+
assert span["description"] == "GET http://api.example.com:8080/foo"
688+
assert span["data"]["url"] == "http://api.example.com:8080/foo"
689689
assert span["data"][SPANDATA.HTTP_METHOD] == "GET"
690690
assert span["data"][SPANDATA.NETWORK_PEER_ADDRESS] == "localhost"
691691
assert span["data"][SPANDATA.NETWORK_PEER_PORT] == PROXY_PORT

0 commit comments

Comments
 (0)