Skip to content

Commit

Permalink
ref(profiling): Deprecate hub in Profile (#3270)
Browse files Browse the repository at this point in the history
Related to #3265
  • Loading branch information
szokeasaurusrex authored Jul 11, 2024
1 parent 1e82809 commit 06d5da1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
32 changes: 31 additions & 1 deletion sentry_sdk/profiler/transaction_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import threading
import time
import uuid
import warnings
from abc import ABC, abstractmethod
from collections import deque

Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/profiler/test_transaction_profiler.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 06d5da1

Please sign in to comment.