-
Notifications
You must be signed in to change notification settings - Fork 582
feat(stdlib): Handle proxy tunnels in httlib integration #5303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import os | ||
| import datetime | ||
| import socket | ||
| from http.client import HTTPConnection, HTTPSConnection | ||
| from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| from socket import SocketIO | ||
| from threading import Thread | ||
| from urllib.error import HTTPError | ||
| from urllib.request import urlopen | ||
| from unittest import mock | ||
|
|
@@ -12,11 +15,35 @@ | |
| from sentry_sdk.consts import MATCH_ALL, SPANDATA | ||
| from sentry_sdk.integrations.stdlib import StdlibIntegration | ||
|
|
||
| from tests.conftest import ApproxDict, create_mock_http_server | ||
| from tests.conftest import ApproxDict, create_mock_http_server, get_free_port | ||
|
|
||
| PORT = create_mock_http_server() | ||
|
|
||
|
|
||
| class MockProxyRequestHandler(BaseHTTPRequestHandler): | ||
| def do_CONNECT(self): | ||
| self.send_response(200, "Connection Established") | ||
| self.end_headers() | ||
|
|
||
| self.rfile.readline() | ||
|
|
||
| response = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" | ||
| self.wfile.write(response) | ||
| self.wfile.flush() | ||
|
|
||
|
|
||
| def create_mock_proxy_server(): | ||
| proxy_port = get_free_port() | ||
| proxy_server = HTTPServer(("localhost", proxy_port), MockProxyRequestHandler) | ||
| proxy_thread = Thread(target=proxy_server.serve_forever) | ||
| proxy_thread.daemon = True | ||
| proxy_thread.start() | ||
| return proxy_port | ||
|
|
||
|
|
||
| PROXY_PORT = create_mock_proxy_server() | ||
|
|
||
|
|
||
| def test_crumb_capture(sentry_init, capture_events): | ||
| sentry_init(integrations=[StdlibIntegration()]) | ||
| events = capture_events() | ||
|
|
@@ -642,3 +669,25 @@ def test_http_timeout(monkeypatch, sentry_init, capture_envelopes): | |
| span = transaction["spans"][0] | ||
| assert span["op"] == "http.client" | ||
| assert span["description"] == f"GET http://localhost:{PORT}/bla" # noqa: E231 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("tunnel_port", [8080, None]) | ||
| def test_proxy_http_tunnel(sentry_init, capture_events, tunnel_port): | ||
| sentry_init(traces_sample_rate=1.0) | ||
| events = capture_events() | ||
|
|
||
| with start_transaction(name="test_transaction"): | ||
| conn = HTTPConnection("localhost", PROXY_PORT) | ||
| conn.set_tunnel("api.example.com", tunnel_port) | ||
| conn.request("GET", "/foo") | ||
| conn.getresponse() | ||
|
|
||
| (event,) = events | ||
| (span,) = event["spans"] | ||
|
|
||
| port_modifier = f":{tunnel_port}" if tunnel_port else "" | ||
| assert span["description"] == f"GET http://api.example.com{port_modifier}/foo" | ||
| assert span["data"]["url"] == f"http://api.example.com{port_modifier}/foo" | ||
| assert span["data"][SPANDATA.HTTP_METHOD] == "GET" | ||
| assert span["data"][SPANDATA.NETWORK_PEER_ADDRESS] == "localhost" | ||
| assert span["data"][SPANDATA.NETWORK_PEER_PORT] == PROXY_PORT | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.