Skip to content

Commit

Permalink
ref(transport): Stop using Hub in HttpTransport (#3247)
Browse files Browse the repository at this point in the history
Also, add deprecation warnings for `HttpTransport.hub_cls`.

Fixes #3232
  • Loading branch information
szokeasaurusrex authored Jul 4, 2024
1 parent defb448 commit 31efa62
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
40 changes: 30 additions & 10 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import urllib3
import certifi

import sentry_sdk
from sentry_sdk.consts import EndpointType
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
from sentry_sdk.worker import BackgroundWorker
Expand All @@ -37,7 +38,6 @@

DataCategory = Optional[str]


KEEP_ALIVE_SOCKET_OPTIONS = []
for option in [
(socket.SOL_SOCKET, lambda: getattr(socket, "SO_KEEPALIVE"), 1), # noqa: B009
Expand Down Expand Up @@ -218,9 +218,8 @@ def __init__(
proxy_headers=options["proxy_headers"],
)

from sentry_sdk import Hub

self.hub_cls = Hub
# Backwards compatibility for deprecated `self.hub_class` attribute
self._hub_cls = sentry_sdk.Hub

def record_lost_event(
self,
Expand Down Expand Up @@ -548,14 +547,11 @@ def capture_envelope(
self, envelope # type: Envelope
):
# type: (...) -> None
hub = self.hub_cls.current

def send_envelope_wrapper():
# type: () -> None
with hub:
with capture_internal_exceptions():
self._send_envelope(envelope)
self._flush_client_reports()
with capture_internal_exceptions():
self._send_envelope(envelope)
self._flush_client_reports()

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

@staticmethod
def _warn_hub_cls():
# type: () -> None
"""Convenience method to warn users about the deprecation of the `hub_cls` attribute."""
warnings.warn(
"The `hub_cls` attribute is deprecated and will be removed in a future release.",
DeprecationWarning,
stacklevel=3,
)

@property
def hub_cls(self):
# type: () -> type[sentry_sdk.Hub]
"""DEPRECATED: This attribute is deprecated and will be removed in a future release."""
HttpTransport._warn_hub_cls()
return self._hub_cls

@hub_cls.setter
def hub_cls(self, value):
# type: (type[sentry_sdk.Hub]) -> None
"""DEPRECATED: This attribute is deprecated and will be removed in a future release."""
HttpTransport._warn_hub_cls()
self._hub_cls = value


class _FunctionTransport(Transport):
"""
Expand Down
21 changes: 19 additions & 2 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gzip
import io
import socket
from collections import namedtuple
from collections import defaultdict, namedtuple
from datetime import datetime, timedelta, timezone
from unittest import mock

Expand All @@ -17,7 +17,6 @@
from sentry_sdk.transport import KEEP_ALIVE_SOCKET_OPTIONS, _parse_rate_limits
from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger


CapturedData = namedtuple("CapturedData", ["path", "event", "envelope", "compressed"])


Expand Down Expand Up @@ -585,3 +584,21 @@ def test_metric_bucket_limits_with_all_namespaces(
assert report["discarded_events"] == [
{"category": "metric_bucket", "reason": "ratelimit_backoff", "quantity": 1},
]


def test_hub_cls_backwards_compat():
class TestCustomHubClass(sentry_sdk.Hub):
pass

transport = sentry_sdk.transport.HttpTransport(
defaultdict(lambda: None, {"dsn": "https://123abc@example.com/123"})
)

with pytest.deprecated_call():
assert transport.hub_cls is sentry_sdk.Hub

with pytest.deprecated_call():
transport.hub_cls = TestCustomHubClass

with pytest.deprecated_call():
assert transport.hub_cls is TestCustomHubClass

0 comments on commit 31efa62

Please sign in to comment.