Skip to content
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

(3) Move tracing related functions from Hub to Scope #2558

Merged
merged 44 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f2a4a3a
Moved get_integration from Hub to Client
antonpirker Nov 29, 2023
4869365
Moved add_breadcrumb from Hub to Scope
antonpirker Nov 29, 2023
e5f9e9d
Moved session functions from Hub to Scope
antonpirker Nov 29, 2023
a338099
Fixed import
antonpirker Nov 29, 2023
d900a1b
Fixed Python 2.7 syntax
antonpirker Nov 29, 2023
0afb3ab
Merge branch 'master' into antonpirker/refactor-hub
antonpirker Nov 30, 2023
fe77d04
Give the client to the scope function. Want to establish a pattern.
antonpirker Dec 1, 2023
4ba7dce
Merge branch 'antonpirker/refactor-hub' of github.com:getsentry/sentr…
antonpirker Dec 1, 2023
b15613a
Moved capture_* functions from Hub to Client
antonpirker Dec 1, 2023
8331bd0
Renamed scope_args to scope_kwargs
antonpirker Dec 1, 2023
ce1759f
oops
antonpirker Dec 1, 2023
9a08b6e
preserve old behavior when called from client directly (not from hub)
antonpirker Dec 1, 2023
f9b8d5a
update test
antonpirker Dec 1, 2023
232ce23
Moved trancing related functions from Hub to Scope
antonpirker Dec 1, 2023
fc95050
Sort imports
antonpirker Dec 1, 2023
b0b3cec
Fixed some tests
antonpirker Dec 4, 2023
dec7dbf
Fixed test
antonpirker Dec 4, 2023
cfd3d54
Trying to fix broken tests because some something was released that b…
antonpirker Dec 4, 2023
ea13b55
fix aiohttp tests
antonpirker Dec 4, 2023
812c791
Merge branch 'master' into antonpirker/refactor-hub-tracing
antonpirker Dec 4, 2023
d233ae5
Merge branch 'master' into antonpirker/refactor-hub-capture
antonpirker Dec 4, 2023
2932adc
Merge branch 'master' into antonpirker/refactor-hub
antonpirker Dec 4, 2023
3a7af73
Merge branch 'antonpirker/refactor-hub' into antonpirker/refactor-hub…
antonpirker Dec 4, 2023
c3baed0
Merge branch 'antonpirker/refactor-hub-capture' into antonpirker/refa…
antonpirker Dec 4, 2023
345c419
Merge branch 'master' into antonpirker/refactor-hub-capture
sentrivana Dec 6, 2023
bb3250a
_update_scope belongs into the scope not the client
antonpirker Dec 11, 2023
3a22dd6
Made top_scope separate parameter
antonpirker Dec 11, 2023
c78b1f5
Hub calls capture_* on scope that calls it on client. Later with new …
antonpirker Dec 13, 2023
0901e72
Merge branch 'feat/new-scopes' into antonpirker/refactor-hub-capture
antonpirker Dec 13, 2023
269fd56
Moved everything to scope and only have capture_event in client
antonpirker Dec 13, 2023
0354065
small fix
antonpirker Dec 13, 2023
3d5825e
Updated test
antonpirker Dec 13, 2023
4e78d1f
Fixed test
antonpirker Dec 13, 2023
ba72216
Fixed test
antonpirker Dec 14, 2023
c5a7ae7
Fixed test for old python
antonpirker Dec 14, 2023
bbb93e6
Formatting
antonpirker Dec 14, 2023
12e632d
Passing the client as separate argument and updating api docs.
antonpirker Dec 14, 2023
eb56a12
ApiDocs
antonpirker Dec 14, 2023
074a9ef
Fix
antonpirker Dec 14, 2023
ac3955b
Update sentry_sdk/scope.py
antonpirker Dec 14, 2023
6733114
Remove condition from _merge_scope (also not present in current imple…
antonpirker Dec 19, 2023
c4ad19a
(4) Move transaction/span related functions from Hub to Scope (#2564)
antonpirker Dec 19, 2023
e8fb2af
Merge branch 'antonpirker/refactor-hub-capture' into antonpirker/refa…
antonpirker Dec 19, 2023
890fc04
Merge branch 'feat/new-scopes' into antonpirker/refactor-hub-tracing
antonpirker Dec 19, 2023
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
12 changes: 6 additions & 6 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,31 @@ def capture_event(
event, # type: Event
hint=None, # type: Optional[Hint]
scope=None, # type: Optional[Any]
**scope_args # type: Any
**scope_kwargs # type: Any
):
# type: (...) -> Optional[str]
return Hub.current.capture_event(event, hint, scope=scope, **scope_args)
return Hub.current.capture_event(event, hint, scope=scope, **scope_kwargs)


@hubmethod
def capture_message(
message, # type: str
level=None, # type: Optional[str]
scope=None, # type: Optional[Any]
**scope_args # type: Any
**scope_kwargs # type: Any
):
# type: (...) -> Optional[str]
return Hub.current.capture_message(message, level, scope=scope, **scope_args)
return Hub.current.capture_message(message, level, scope=scope, **scope_kwargs)


@hubmethod
def capture_exception(
error=None, # type: Optional[Union[BaseException, ExcInfo]]
scope=None, # type: Optional[Any]
**scope_args # type: Any
**scope_kwargs # type: Any
):
# type: (...) -> Optional[str]
return Hub.current.capture_exception(error, scope=scope, **scope_args)
return Hub.current.capture_exception(error, scope=scope, **scope_kwargs)


@hubmethod
Expand Down
111 changes: 110 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from importlib import import_module
import copy
import os
import sys
import uuid
import random
import socket
Expand All @@ -9,6 +11,8 @@
capture_internal_exceptions,
current_stacktrace,
disable_capture_event,
event_from_exception,
exc_info_from_error,
format_timestamp,
get_sdk_name,
get_type_name,
Expand Down Expand Up @@ -43,9 +47,12 @@
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._types import Event, ExcInfo, Hint
from sentry_sdk.session import Session


Expand Down Expand Up @@ -140,6 +147,24 @@ def _get_options(*args, **kwargs):
return rv


def _update_scope(base, scope_change, scope_kwargs):
# type: (Scope, Optional[Any], Dict[str, Any]) -> Scope
if scope_change and scope_kwargs:
raise TypeError("cannot provide scope and kwargs")
if scope_change is not None:
final_scope = copy.copy(base)
if callable(scope_change):
scope_change(final_scope)
else:
final_scope.update_from_scope(scope_change)
elif scope_kwargs:
final_scope = copy.copy(base)
final_scope.update_from_kwargs(**scope_kwargs)
else:
final_scope = base
return final_scope


try:
# Python 3.6+
module_not_found_error = ModuleNotFoundError
Expand Down Expand Up @@ -541,6 +566,7 @@ def capture_event(
event, # type: Event
hint=None, # type: Optional[Hint]
scope=None, # type: Optional[Scope]
**scope_kwargs # type: Any
):
# type: (...) -> Optional[str]
"""Captures an event.
Expand All @@ -552,11 +578,18 @@ def capture_event(
:param scope: An optional scope to use for determining whether this event
should be captured.

:param scope_kwargs: For supported `**scope_kwargs` see
:py:meth:`sentry_sdk.Scope.update_from_kwargs`.

:returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
"""
if disable_capture_event.get(False):
return None

if scope_kwargs is not None and "top_scope" in scope_kwargs:
top_scope = scope_kwargs.pop("top_scope")
scope = _update_scope(top_scope, scope, scope_kwargs)

if hint is None:
hint = {}
event_id = event.get("event_id")
Expand Down Expand Up @@ -644,6 +677,66 @@ def capture_event(

return event_id

def capture_message(self, message, level=None, scope=None, **scope_kwargs):
# type: (str, Optional[str], Optional[Scope], Any) -> Optional[str]
"""
Captures a message.

:param message: The string to send as the message.

:param level: If no level is provided, the default level is `info`.

:param scope: An optional :py:class:`sentry_sdk.Scope` to use.

:param scope_kwargs: For supported `**scope_kwargs` see
:py:meth:`sentry_sdk.Scope.update_from_kwargs`.

:returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
"""
if level is None:
level = "info"

return self.capture_event(
{"message": message, "level": level}, scope=scope, **scope_kwargs
)

def capture_exception(self, error=None, scope=None, **scope_kwargs):
# type: (Optional[Union[BaseException, ExcInfo]], Optional[Scope], Any) -> Optional[str]
"""Captures an exception.

:param error: An exception to catch. If `None`, `sys.exc_info()` will be used.

:param scope_kwargs: For supported `**scope_kwargs` see
:py:meth:`sentry_sdk.Scope.update_from_kwargs`.

:returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
"""
if error is not None:
exc_info = exc_info_from_error(error)
else:
exc_info = sys.exc_info()

event, hint = event_from_exception(exc_info, client_options=self.options)

try:
return self.capture_event(event, hint=hint, scope=scope, **scope_kwargs)
except Exception:
self._capture_internal_exception(sys.exc_info())

return None

def _capture_internal_exception(
self, exc_info # type: Any
):
# type: (...) -> Any
"""
Capture an exception that is likely caused by a bug in the SDK
itself.

These exceptions do not end up in Sentry and are just logged instead.
"""
logger.error("Internal error in sentry_sdk", exc_info=exc_info)

def capture_session(
self, session # type: Session
):
Expand All @@ -653,6 +746,22 @@ 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
Loading
Loading