Skip to content

Commit 06d5da1

Browse files
ref(profiling): Deprecate hub in Profile (#3270)
Related to #3265
1 parent 1e82809 commit 06d5da1

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

sentry_sdk/profiler/transaction_profiler.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import threading
3434
import time
3535
import uuid
36+
import warnings
3637
from abc import ABC, abstractmethod
3738
from collections import deque
3839

@@ -213,7 +214,6 @@ def __init__(
213214
):
214215
# type: (...) -> None
215216
self.scheduler = _scheduler if scheduler is None else scheduler
216-
self.hub = hub
217217

218218
self.event_id = uuid.uuid4().hex # type: str
219219

@@ -240,6 +240,16 @@ def __init__(
240240

241241
self.unique_samples = 0
242242

243+
# Backwards compatibility with the old hub property
244+
self._hub = None # type: Optional[sentry_sdk.Hub]
245+
if hub is not None:
246+
self._hub = hub
247+
warnings.warn(
248+
"The `hub` parameter is deprecated. Please do not use it.",
249+
DeprecationWarning,
250+
stacklevel=2,
251+
)
252+
243253
def update_active_thread_id(self):
244254
# type: () -> None
245255
self.active_thread_id = get_current_thread_meta()[0]
@@ -506,6 +516,26 @@ def valid(self):
506516

507517
return True
508518

519+
@property
520+
def hub(self):
521+
# type: () -> Optional[sentry_sdk.Hub]
522+
warnings.warn(
523+
"The `hub` attribute is deprecated. Please do not access it.",
524+
DeprecationWarning,
525+
stacklevel=2,
526+
)
527+
return self._hub
528+
529+
@hub.setter
530+
def hub(self, value):
531+
# type: (Optional[sentry_sdk.Hub]) -> None
532+
warnings.warn(
533+
"The `hub` attribute is deprecated. Please do not set it.",
534+
DeprecationWarning,
535+
stacklevel=2,
536+
)
537+
self._hub = value
538+
509539

510540
class Scheduler(ABC):
511541
mode = "unknown" # type: ProfilerMode

tests/profiler/test_transaction_profiler.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import inspect
22
import os
3+
import sentry_sdk
34
import sys
45
import threading
56
import time
7+
import warnings
68
from collections import defaultdict
79
from unittest import mock
810

@@ -813,3 +815,27 @@ def test_profile_processing(
813815
assert processed["frames"] == expected["frames"]
814816
assert processed["stacks"] == expected["stacks"]
815817
assert processed["samples"] == expected["samples"]
818+
819+
820+
def test_hub_backwards_compatibility():
821+
hub = sentry_sdk.Hub()
822+
823+
with pytest.warns(DeprecationWarning):
824+
profile = Profile(True, 0, hub=hub)
825+
826+
with pytest.warns(DeprecationWarning):
827+
assert profile.hub is hub
828+
829+
new_hub = sentry_sdk.Hub()
830+
831+
with pytest.warns(DeprecationWarning):
832+
profile.hub = new_hub
833+
834+
with pytest.warns(DeprecationWarning):
835+
assert profile.hub is new_hub
836+
837+
838+
def test_no_warning_without_hub():
839+
with warnings.catch_warnings():
840+
warnings.simplefilter("error")
841+
Profile(True, 0)

0 commit comments

Comments
 (0)