diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index d07bec23da..3393f491d4 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -81,6 +81,7 @@ def __init__( auto_session_tracking=True, # type: bool send_client_reports=True, # type: bool _experiments={}, # type: Experiments # noqa: B006 + proxy_headers=None, # type: Optional[Dict[str, str]] ): # type: (...) -> None pass diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index fca6fa8aec..4937668cc7 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -156,6 +156,7 @@ def __init__( http_proxy=options["http_proxy"], https_proxy=options["https_proxy"], ca_certs=options["ca_certs"], + proxy_headers=options["proxy_headers"], ) from sentry_sdk import Hub @@ -420,6 +421,7 @@ def _make_pool( http_proxy, # type: Optional[str] https_proxy, # type: Optional[str] ca_certs, # type: Optional[Any] + proxy_headers, # type: Optional[Dict[str, str]] ): # type: (...) -> Union[PoolManager, ProxyManager] proxy = None @@ -436,6 +438,9 @@ def _make_pool( opts = self._get_pool_options(ca_certs) if proxy: + if proxy_headers: + opts["proxy_headers"] = proxy_headers + return urllib3.ProxyManager(proxy, **opts) else: return urllib3.PoolManager(**opts) diff --git a/tests/test_client.py b/tests/test_client.py index 5523647870..c0f380d770 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -227,6 +227,16 @@ def test_transport_option(monkeypatch): "arg_https_proxy": "https://localhost/123", "expected_proxy_scheme": "https", }, + { + "dsn": "https://foo@sentry.io/123", + "env_http_proxy": None, + "env_https_proxy": None, + "env_no_proxy": "sentry.io,example.com", + "arg_http_proxy": None, + "arg_https_proxy": "https://localhost/123", + "expected_proxy_scheme": "https", + "arg_proxy_headers": {"Test-Header": "foo-bar"}, + }, ], ) def test_proxy(monkeypatch, testcase): @@ -241,12 +251,17 @@ def test_proxy(monkeypatch, testcase): kwargs["http_proxy"] = testcase["arg_http_proxy"] if testcase["arg_https_proxy"] is not None: kwargs["https_proxy"] = testcase["arg_https_proxy"] + if testcase.get("arg_proxy_headers") is not None: + kwargs["proxy_headers"] = testcase["arg_proxy_headers"] client = Client(testcase["dsn"], **kwargs) if testcase["expected_proxy_scheme"] is None: assert client.transport._pool.proxy is None else: assert client.transport._pool.proxy.scheme == testcase["expected_proxy_scheme"] + if testcase.get("arg_proxy_headers") is not None: + assert client.transport._pool.proxy_headers == testcase["arg_proxy_headers"] + def test_simple_transport(sentry_init): events = []