Skip to content

Commit 31efa62

Browse files
ref(transport): Stop using Hub in HttpTransport (#3247)
Also, add deprecation warnings for `HttpTransport.hub_cls`. Fixes #3232
1 parent defb448 commit 31efa62

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

sentry_sdk/transport.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import urllib3
1313
import certifi
1414

15+
import sentry_sdk
1516
from sentry_sdk.consts import EndpointType
1617
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
1718
from sentry_sdk.worker import BackgroundWorker
@@ -37,7 +38,6 @@
3738

3839
DataCategory = Optional[str]
3940

40-
4141
KEEP_ALIVE_SOCKET_OPTIONS = []
4242
for option in [
4343
(socket.SOL_SOCKET, lambda: getattr(socket, "SO_KEEPALIVE"), 1), # noqa: B009
@@ -218,9 +218,8 @@ def __init__(
218218
proxy_headers=options["proxy_headers"],
219219
)
220220

221-
from sentry_sdk import Hub
222-
223-
self.hub_cls = Hub
221+
# Backwards compatibility for deprecated `self.hub_class` attribute
222+
self._hub_cls = sentry_sdk.Hub
224223

225224
def record_lost_event(
226225
self,
@@ -548,14 +547,11 @@ def capture_envelope(
548547
self, envelope # type: Envelope
549548
):
550549
# type: (...) -> None
551-
hub = self.hub_cls.current
552-
553550
def send_envelope_wrapper():
554551
# type: () -> None
555-
with hub:
556-
with capture_internal_exceptions():
557-
self._send_envelope(envelope)
558-
self._flush_client_reports()
552+
with capture_internal_exceptions():
553+
self._send_envelope(envelope)
554+
self._flush_client_reports()
559555

560556
if not self._worker.submit(send_envelope_wrapper):
561557
self.on_dropped_event("full_queue")
@@ -579,6 +575,30 @@ def kill(self):
579575
logger.debug("Killing HTTP transport")
580576
self._worker.kill()
581577

578+
@staticmethod
579+
def _warn_hub_cls():
580+
# type: () -> None
581+
"""Convenience method to warn users about the deprecation of the `hub_cls` attribute."""
582+
warnings.warn(
583+
"The `hub_cls` attribute is deprecated and will be removed in a future release.",
584+
DeprecationWarning,
585+
stacklevel=3,
586+
)
587+
588+
@property
589+
def hub_cls(self):
590+
# type: () -> type[sentry_sdk.Hub]
591+
"""DEPRECATED: This attribute is deprecated and will be removed in a future release."""
592+
HttpTransport._warn_hub_cls()
593+
return self._hub_cls
594+
595+
@hub_cls.setter
596+
def hub_cls(self, value):
597+
# type: (type[sentry_sdk.Hub]) -> None
598+
"""DEPRECATED: This attribute is deprecated and will be removed in a future release."""
599+
HttpTransport._warn_hub_cls()
600+
self._hub_cls = value
601+
582602

583603
class _FunctionTransport(Transport):
584604
"""

tests/test_transport.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import gzip
44
import io
55
import socket
6-
from collections import namedtuple
6+
from collections import defaultdict, namedtuple
77
from datetime import datetime, timedelta, timezone
88
from unittest import mock
99

@@ -17,7 +17,6 @@
1717
from sentry_sdk.transport import KEEP_ALIVE_SOCKET_OPTIONS, _parse_rate_limits
1818
from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger
1919

20-
2120
CapturedData = namedtuple("CapturedData", ["path", "event", "envelope", "compressed"])
2221

2322

@@ -585,3 +584,21 @@ def test_metric_bucket_limits_with_all_namespaces(
585584
assert report["discarded_events"] == [
586585
{"category": "metric_bucket", "reason": "ratelimit_backoff", "quantity": 1},
587586
]
587+
588+
589+
def test_hub_cls_backwards_compat():
590+
class TestCustomHubClass(sentry_sdk.Hub):
591+
pass
592+
593+
transport = sentry_sdk.transport.HttpTransport(
594+
defaultdict(lambda: None, {"dsn": "https://[email protected]/123"})
595+
)
596+
597+
with pytest.deprecated_call():
598+
assert transport.hub_cls is sentry_sdk.Hub
599+
600+
with pytest.deprecated_call():
601+
transport.hub_cls = TestCustomHubClass
602+
603+
with pytest.deprecated_call():
604+
assert transport.hub_cls is TestCustomHubClass

0 commit comments

Comments
 (0)