Skip to content

Commit

Permalink
Merge branch 'master' into ivana/reduce-py-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana committed Dec 11, 2023
2 parents 8c4d9cb + b656f79 commit 96bebfd
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 166 deletions.
19 changes: 0 additions & 19 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Type
from typing import Union

from sentry_sdk.integrations import Integration
from sentry_sdk.scope import Scope
from sentry_sdk._types import Event, Hint
from sentry_sdk.session import Session
Expand Down Expand Up @@ -656,22 +653,6 @@ def capture_session(
else:
self.session_flusher.add_session(session)

def get_integration(
self, name_or_class # type: Union[str, Type[Integration]]
):
# type: (...) -> Any
"""Returns the integration for this client by name or class.
If the client does not have that integration then `None` is returned.
"""
if isinstance(name_or_class, str):
integration_name = name_or_class
elif name_or_class.identifier is not None:
integration_name = name_or_class.identifier
else:
raise ValueError("Integration has no name")

return self.integrations.get(integration_name)

def close(
self,
timeout=None, # type: Optional[float]
Expand Down
62 changes: 52 additions & 10 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from contextlib import contextmanager

from sentry_sdk._compat import with_metaclass
from sentry_sdk._compat import datetime_utcnow, with_metaclass
from sentry_sdk.consts import INSTRUMENTER
from sentry_sdk.scope import Scope
from sentry_sdk.client import Client
Expand All @@ -15,6 +15,7 @@
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
)
from sentry_sdk.session import Session
from sentry_sdk.tracing_utils import (
has_tracing_enabled,
normalize_incoming_data,
Expand Down Expand Up @@ -293,9 +294,18 @@ def get_integration(
If the return value is not `None` the hub is guaranteed to have a
client attached.
"""
if isinstance(name_or_class, str):
integration_name = name_or_class
elif name_or_class.identifier is not None:
integration_name = name_or_class.identifier
else:
raise ValueError("Integration has no name")

client = self.client
if client is not None:
return client.get_integration(name_or_class)
rv = client.integrations.get(integration_name)
if rv is not None:
return rv

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

kwargs["client"] = client
crumb = dict(crumb or ()) # type: Breadcrumb
crumb.update(kwargs)
if not crumb:
return

hint = dict(hint or ()) # type: Hint

if crumb.get("timestamp") is None:
crumb["timestamp"] = datetime_utcnow()
if crumb.get("type") is None:
crumb["type"] = "default"

if client.options["before_breadcrumb"] is not None:
new_crumb = client.options["before_breadcrumb"](crumb, hint)
else:
new_crumb = crumb

if new_crumb is not None:
scope._breadcrumbs.append(new_crumb)
else:
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)

scope.add_breadcrumb(crumb, hint, **kwargs)
max_breadcrumbs = client.options["max_breadcrumbs"] # type: int
while len(scope._breadcrumbs) > max_breadcrumbs:
scope._breadcrumbs.popleft()

def start_span(self, span=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
# type: (Optional[Span], str, Any) -> Span
Expand Down Expand Up @@ -680,17 +712,26 @@ def start_session(
):
# type: (...) -> None
"""Starts a new session."""
self.end_session()
client, scope = self._stack[-1]
scope.start_session(
client=client,
scope._session = Session(
release=client.options["release"] if client else None,
environment=client.options["environment"] if client else None,
user=scope._user,
session_mode=session_mode,
)

def end_session(self):
# type: (...) -> None
"""Ends the current session if there is one."""
client, scope = self._stack[-1]
scope.end_session(client=client)
session = scope._session
self.scope._session = None

if session is not None:
session.close()
if client is not None:
client.capture_session(session)

def stop_auto_session_tracking(self):
# type: (...) -> None
Expand All @@ -699,17 +740,18 @@ def stop_auto_session_tracking(self):
This temporarily session tracking for the current scope when called.
To resume session tracking call `resume_auto_session_tracking`.
"""
self.end_session()
client, scope = self._stack[-1]
scope.stop_auto_session_tracking(client=client)
scope._force_auto_session_tracking = False

def resume_auto_session_tracking(self):
# type: (...) -> None
"""Resumes automatic session tracking for the current scope if
disabled earlier. This requires that generally automatic session
tracking is enabled.
"""
scope = self._stack[-1][1]
scope.resume_auto_session_tracking()
client, scope = self._stack[-1]
scope._force_auto_session_tracking = None

def flush(
self,
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def apply_async(*args, **kwargs):

try:
task_started_from_beat = args[1][0] == "BEAT"
except IndexError:
except (IndexError, TypeError):
task_started_from_beat = False

task = args[0]
Expand Down
Loading

0 comments on commit 96bebfd

Please sign in to comment.