Skip to content

Commit 38ec650

Browse files
authored
Revert "Move add_breadcrumb and session function from Hub to Scope (#2544)" (#2574)
This reverts commit 354d7bb.
1 parent 75f89b8 commit 38ec650

File tree

3 files changed

+56
-124
lines changed

3 files changed

+56
-124
lines changed

sentry_sdk/client.py

-19
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
from typing import Dict
4444
from typing import Optional
4545
from typing import Sequence
46-
from typing import Type
47-
from typing import Union
4846

49-
from sentry_sdk.integrations import Integration
5047
from sentry_sdk.scope import Scope
5148
from sentry_sdk._types import Event, Hint
5249
from sentry_sdk.session import Session
@@ -656,22 +653,6 @@ def capture_session(
656653
else:
657654
self.session_flusher.add_session(session)
658655

659-
def get_integration(
660-
self, name_or_class # type: Union[str, Type[Integration]]
661-
):
662-
# type: (...) -> Any
663-
"""Returns the integration for this client by name or class.
664-
If the client does not have that integration then `None` is returned.
665-
"""
666-
if isinstance(name_or_class, str):
667-
integration_name = name_or_class
668-
elif name_or_class.identifier is not None:
669-
integration_name = name_or_class.identifier
670-
else:
671-
raise ValueError("Integration has no name")
672-
673-
return self.integrations.get(integration_name)
674-
675656
def close(
676657
self,
677658
timeout=None, # type: Optional[float]

sentry_sdk/hub.py

+52-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from contextlib import contextmanager
55

6-
from sentry_sdk._compat import with_metaclass
6+
from sentry_sdk._compat import datetime_utcnow, with_metaclass
77
from sentry_sdk.consts import INSTRUMENTER
88
from sentry_sdk.scope import Scope
99
from sentry_sdk.client import Client
@@ -15,6 +15,7 @@
1515
BAGGAGE_HEADER_NAME,
1616
SENTRY_TRACE_HEADER_NAME,
1717
)
18+
from sentry_sdk.session import Session
1819
from sentry_sdk.tracing_utils import (
1920
has_tracing_enabled,
2021
normalize_incoming_data,
@@ -293,9 +294,18 @@ def get_integration(
293294
If the return value is not `None` the hub is guaranteed to have a
294295
client attached.
295296
"""
297+
if isinstance(name_or_class, str):
298+
integration_name = name_or_class
299+
elif name_or_class.identifier is not None:
300+
integration_name = name_or_class.identifier
301+
else:
302+
raise ValueError("Integration has no name")
303+
296304
client = self.client
297305
if client is not None:
298-
return client.get_integration(name_or_class)
306+
rv = client.integrations.get(integration_name)
307+
if rv is not None:
308+
return rv
299309

300310
@property
301311
def client(self):
@@ -420,9 +430,31 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
420430
logger.info("Dropped breadcrumb because no client bound")
421431
return
422432

423-
kwargs["client"] = client
433+
crumb = dict(crumb or ()) # type: Breadcrumb
434+
crumb.update(kwargs)
435+
if not crumb:
436+
return
437+
438+
hint = dict(hint or ()) # type: Hint
439+
440+
if crumb.get("timestamp") is None:
441+
crumb["timestamp"] = datetime_utcnow()
442+
if crumb.get("type") is None:
443+
crumb["type"] = "default"
444+
445+
if client.options["before_breadcrumb"] is not None:
446+
new_crumb = client.options["before_breadcrumb"](crumb, hint)
447+
else:
448+
new_crumb = crumb
449+
450+
if new_crumb is not None:
451+
scope._breadcrumbs.append(new_crumb)
452+
else:
453+
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)
424454

425-
scope.add_breadcrumb(crumb, hint, **kwargs)
455+
max_breadcrumbs = client.options["max_breadcrumbs"] # type: int
456+
while len(scope._breadcrumbs) > max_breadcrumbs:
457+
scope._breadcrumbs.popleft()
426458

427459
def start_span(self, span=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
428460
# type: (Optional[Span], str, Any) -> Span
@@ -680,17 +712,26 @@ def start_session(
680712
):
681713
# type: (...) -> None
682714
"""Starts a new session."""
715+
self.end_session()
683716
client, scope = self._stack[-1]
684-
scope.start_session(
685-
client=client,
717+
scope._session = Session(
718+
release=client.options["release"] if client else None,
719+
environment=client.options["environment"] if client else None,
720+
user=scope._user,
686721
session_mode=session_mode,
687722
)
688723

689724
def end_session(self):
690725
# type: (...) -> None
691726
"""Ends the current session if there is one."""
692727
client, scope = self._stack[-1]
693-
scope.end_session(client=client)
728+
session = scope._session
729+
self.scope._session = None
730+
731+
if session is not None:
732+
session.close()
733+
if client is not None:
734+
client.capture_session(session)
694735

695736
def stop_auto_session_tracking(self):
696737
# type: (...) -> None
@@ -699,17 +740,18 @@ def stop_auto_session_tracking(self):
699740
This temporarily session tracking for the current scope when called.
700741
To resume session tracking call `resume_auto_session_tracking`.
701742
"""
743+
self.end_session()
702744
client, scope = self._stack[-1]
703-
scope.stop_auto_session_tracking(client=client)
745+
scope._force_auto_session_tracking = False
704746

705747
def resume_auto_session_tracking(self):
706748
# type: (...) -> None
707749
"""Resumes automatic session tracking for the current scope if
708750
disabled earlier. This requires that generally automatic session
709751
tracking is enabled.
710752
"""
711-
scope = self._stack[-1][1]
712-
scope.resume_auto_session_tracking()
753+
client, scope = self._stack[-1]
754+
scope._force_auto_session_tracking = None
713755

714756
def flush(
715757
self,

sentry_sdk/scope.py

+4-95
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import uuid
66

77
from sentry_sdk.attachments import Attachment
8-
from sentry_sdk._compat import datetime_utcnow
9-
from sentry_sdk.consts import FALSE_VALUES
108
from sentry_sdk._functools import wraps
11-
from sentry_sdk.session import Session
129
from sentry_sdk.tracing_utils import (
1310
Baggage,
1411
extract_sentrytrace_data,
@@ -23,6 +20,9 @@
2320
from sentry_sdk._types import TYPE_CHECKING
2421
from sentry_sdk.utils import logger, capture_internal_exceptions
2522

23+
from sentry_sdk.consts import FALSE_VALUES
24+
25+
2626
if TYPE_CHECKING:
2727
from typing import Any
2828
from typing import Dict
@@ -36,7 +36,6 @@
3636

3737
from sentry_sdk._types import (
3838
Breadcrumb,
39-
BreadcrumbHint,
4039
Event,
4140
EventProcessor,
4241
ErrorProcessor,
@@ -47,6 +46,7 @@
4746

4847
from sentry_sdk.profiler import Profile
4948
from sentry_sdk.tracing import Span
49+
from sentry_sdk.session import Session
5050

5151
F = TypeVar("F", bound=Callable[..., Any])
5252
T = TypeVar("T")
@@ -517,97 +517,6 @@ def add_attachment(
517517
)
518518
)
519519

520-
def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
521-
# type: (Optional[Breadcrumb], Optional[BreadcrumbHint], Any) -> None
522-
"""
523-
Adds a breadcrumb.
524-
525-
:param crumb: Dictionary with the data as the sentry v7/v8 protocol expects.
526-
527-
:param hint: An optional value that can be used by `before_breadcrumb`
528-
to customize the breadcrumbs that are emitted.
529-
"""
530-
client = kwargs.pop("client", None)
531-
if client is None:
532-
return
533-
534-
before_breadcrumb = client.options.get("before_breadcrumb")
535-
max_breadcrumbs = client.options.get("max_breadcrumbs")
536-
537-
crumb = dict(crumb or ()) # type: Breadcrumb
538-
crumb.update(kwargs)
539-
if not crumb:
540-
return
541-
542-
hint = dict(hint or ()) # type: Hint
543-
544-
if crumb.get("timestamp") is None:
545-
crumb["timestamp"] = datetime_utcnow()
546-
if crumb.get("type") is None:
547-
crumb["type"] = "default"
548-
549-
if before_breadcrumb is not None:
550-
new_crumb = before_breadcrumb(crumb, hint)
551-
else:
552-
new_crumb = crumb
553-
554-
if new_crumb is not None:
555-
self._breadcrumbs.append(new_crumb)
556-
else:
557-
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)
558-
559-
while len(self._breadcrumbs) > max_breadcrumbs:
560-
self._breadcrumbs.popleft()
561-
562-
def start_session(self, *args, **kwargs):
563-
# type: (*Any, **Any) -> None
564-
"""Starts a new session."""
565-
client = kwargs.pop("client", None)
566-
session_mode = kwargs.pop("session_mode", "application")
567-
568-
self.end_session(client=client)
569-
570-
self._session = Session(
571-
release=client.options["release"] if client else None,
572-
environment=client.options["environment"] if client else None,
573-
user=self._user,
574-
session_mode=session_mode,
575-
)
576-
577-
def end_session(self, *args, **kwargs):
578-
# type: (*Any, **Any) -> None
579-
"""Ends the current session if there is one."""
580-
client = kwargs.pop("client", None)
581-
582-
session = self._session
583-
self._session = None
584-
585-
if session is not None:
586-
session.close()
587-
if client is not None:
588-
client.capture_session(session)
589-
590-
def stop_auto_session_tracking(self, *args, **kwargs):
591-
# type: (*Any, **Any) -> None
592-
"""Stops automatic session tracking.
593-
594-
This temporarily session tracking for the current scope when called.
595-
To resume session tracking call `resume_auto_session_tracking`.
596-
"""
597-
client = kwargs.pop("client", None)
598-
599-
self.end_session(client=client)
600-
601-
self._force_auto_session_tracking = False
602-
603-
def resume_auto_session_tracking(self):
604-
# type: (...) -> None
605-
"""Resumes automatic session tracking for the current scope if
606-
disabled earlier. This requires that generally automatic session
607-
tracking is enabled.
608-
"""
609-
self._force_auto_session_tracking = None
610-
611520
def add_event_processor(
612521
self, func # type: EventProcessor
613522
):

0 commit comments

Comments
 (0)