Skip to content

Commit 9fe64ac

Browse files
ref(profiling): Deprecate hub in Profile
Related to #3265
1 parent 2cb2dca commit 9fe64ac

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

sentry_sdk/profiler/transaction_profiler.py

+29-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
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,24 @@ def valid(self):
506516

507517
return True
508518

519+
@property
520+
def hub(self):
521+
warnings.warn(
522+
"The `hub` attribute is deprecated. Please do not access it.",
523+
DeprecationWarning,
524+
stacklevel=2,
525+
)
526+
return self._hub
527+
528+
@hub.setter
529+
def hub(self, value):
530+
warnings.warn(
531+
"The `hub` attribute is deprecated. Please do not set it.",
532+
DeprecationWarning,
533+
stacklevel=2,
534+
)
535+
self._hub = value
536+
509537

510538
class Scheduler(ABC):
511539
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)