diff --git a/sentry_sdk/profiler/transaction_profiler.py b/sentry_sdk/profiler/transaction_profiler.py index bdd6c5fa8c..e8ebfa6450 100644 --- a/sentry_sdk/profiler/transaction_profiler.py +++ b/sentry_sdk/profiler/transaction_profiler.py @@ -33,6 +33,7 @@ import threading import time import uuid +import warnings from abc import ABC, abstractmethod from collections import deque @@ -213,7 +214,6 @@ def __init__( ): # type: (...) -> None self.scheduler = _scheduler if scheduler is None else scheduler - self.hub = hub self.event_id = uuid.uuid4().hex # type: str @@ -240,6 +240,16 @@ def __init__( self.unique_samples = 0 + # Backwards compatibility with the old hub property + self._hub = None # type: Optional[sentry_sdk.Hub] + if hub is not None: + self._hub = hub + warnings.warn( + "The `hub` parameter is deprecated. Please do not use it.", + DeprecationWarning, + stacklevel=2, + ) + def update_active_thread_id(self): # type: () -> None self.active_thread_id = get_current_thread_meta()[0] @@ -506,6 +516,26 @@ def valid(self): return True + @property + def hub(self): + # type: () -> Optional[sentry_sdk.Hub] + warnings.warn( + "The `hub` attribute is deprecated. Please do not access it.", + DeprecationWarning, + stacklevel=2, + ) + return self._hub + + @hub.setter + def hub(self, value): + # type: (Optional[sentry_sdk.Hub]) -> None + warnings.warn( + "The `hub` attribute is deprecated. Please do not set it.", + DeprecationWarning, + stacklevel=2, + ) + self._hub = value + class Scheduler(ABC): mode = "unknown" # type: ProfilerMode diff --git a/tests/profiler/test_transaction_profiler.py b/tests/profiler/test_transaction_profiler.py index ec506cfa67..d657bec506 100644 --- a/tests/profiler/test_transaction_profiler.py +++ b/tests/profiler/test_transaction_profiler.py @@ -1,8 +1,10 @@ import inspect import os +import sentry_sdk import sys import threading import time +import warnings from collections import defaultdict from unittest import mock @@ -813,3 +815,27 @@ def test_profile_processing( assert processed["frames"] == expected["frames"] assert processed["stacks"] == expected["stacks"] assert processed["samples"] == expected["samples"] + + +def test_hub_backwards_compatibility(): + hub = sentry_sdk.Hub() + + with pytest.warns(DeprecationWarning): + profile = Profile(True, 0, hub=hub) + + with pytest.warns(DeprecationWarning): + assert profile.hub is hub + + new_hub = sentry_sdk.Hub() + + with pytest.warns(DeprecationWarning): + profile.hub = new_hub + + with pytest.warns(DeprecationWarning): + assert profile.hub is new_hub + + +def test_no_warning_without_hub(): + with warnings.catch_warnings(): + warnings.simplefilter("error") + Profile(True, 0)