Skip to content

ref(profiling): Deprecate hub in Profile #3270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading