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

(1) Add new scopes related API (but not yet calling it) #2609

Closed
wants to merge 97 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
9487038
(1) Move `add_breadcrumb` and session function from Hub to Scope (#2578)
antonpirker Dec 11, 2023
a17cb67
Merge branch 'master' into feat/new-scopes
antonpirker Dec 11, 2023
b4183bd
Merge branch 'master' into feat/new-scopes
antonpirker Dec 13, 2023
eff8f78
(5) Add `reset()` to thread local ContextVar and no-op `copy_context(…
antonpirker Dec 19, 2023
79e15f5
(2) Move `capture_*` from Hub to Client (#2555)
antonpirker Dec 19, 2023
5f332e3
(3) Move tracing related functions from Hub to Scope (#2558)
antonpirker Dec 19, 2023
eee394d
Added new API
antonpirker Dec 19, 2023
5816b54
formatting
antonpirker Dec 19, 2023
7a440fe
formatting
antonpirker Dec 19, 2023
0a40f62
formatting
antonpirker Dec 19, 2023
ea8f139
Removed duplicated import
antonpirker Dec 19, 2023
d9c88e7
formatting
antonpirker Dec 19, 2023
67eea1e
formatting
antonpirker Dec 19, 2023
76d352e
formatting
antonpirker Dec 19, 2023
0feab1a
formatting
antonpirker Dec 19, 2023
db610a8
Merge branch 'master' into new-scopes-add-api
antonpirker Jan 9, 2024
cabf8d9
Merge branch 'master' into feat/new-scopes
antonpirker Jan 9, 2024
7cc1f86
Merge branch 'feat/new-scopes' into new-scopes-add-api
antonpirker Jan 9, 2024
cce06c7
trigger ci
antonpirker Jan 9, 2024
1f06fbb
trigger ci
antonpirker Jan 9, 2024
692a2dc
Merge branch 'new-scopes-add-api' of github.com:getsentry/sentry-pyth…
antonpirker Jan 9, 2024
9d2a7b1
Fixed import
antonpirker Jan 10, 2024
c5b8522
Merge branch 'master' into feat/new-scopes
antonpirker Jan 10, 2024
1b17770
Renamed api
antonpirker Jan 10, 2024
e034aa9
Renamed scope vars
antonpirker Jan 10, 2024
ba74c1e
Made scope.client never None
antonpirker Jan 10, 2024
6a6bcf7
now for real
antonpirker Jan 10, 2024
94ef082
Better naming for scope context and global vars
antonpirker Jan 10, 2024
a213490
Merge branch 'master' into feat/new-scopes
antonpirker Jan 19, 2024
d2fb4b5
Removed private methods from NoopClient because they should never be …
antonpirker Jan 22, 2024
62889d5
Cleanup and mechanism to prevent recursion
antonpirker Jan 23, 2024
f51c691
Cleanup
antonpirker Jan 23, 2024
3eb2243
Improved scope api
antonpirker Jan 23, 2024
553cbbc
Bring back old _merge_scopes
antonpirker Jan 23, 2024
b63483c
docs
antonpirker Jan 23, 2024
af54208
Added some tests
antonpirker Jan 23, 2024
7366818
Added tests
antonpirker Jan 23, 2024
9ad022b
Apply suggestions from code review
antonpirker Jan 23, 2024
699ef78
Fixing stuff
antonpirker Jan 23, 2024
55a9f4f
Improved Client inheritance
antonpirker Jan 23, 2024
09737e1
Removed useless code
antonpirker Jan 23, 2024
0c47518
Removed useless context copying
antonpirker Jan 24, 2024
01b663a
Fixed import
antonpirker Jan 24, 2024
50afa9d
Added more tests
antonpirker Jan 24, 2024
c16be2b
Sorted __all__
antonpirker Jan 24, 2024
9115743
Deletion of thread local vars is not possible in older Python
antonpirker Jan 25, 2024
70b3d99
Fixed deletion of thread local var in Python 3.6+
antonpirker Jan 25, 2024
8eeeed3
Merge branch 'master' into feat/new-scopes
antonpirker Jan 25, 2024
9952097
Merge branch 'feat/new-scopes' into new-scopes-add-api
antonpirker Jan 25, 2024
a8f98b2
Merge branch 'master' into new-scopes-add-api
antonpirker Jan 25, 2024
059f051
Fixed docstring
antonpirker Jan 25, 2024
b027411
Fixed docstring
antonpirker Jan 25, 2024
8c4fddc
Fixed imports
antonpirker Jan 25, 2024
4d884e9
Back to normal delete of context var.
antonpirker Jan 25, 2024
1c1b911
Delete from the underlying __dict__. This is ok according to the docs.
antonpirker Jan 25, 2024
abd7c37
deactivated tests to try something
antonpirker Jan 25, 2024
3420b24
enable one test
antonpirker Jan 25, 2024
668a7a4
Enabled more tests
antonpirker Jan 25, 2024
95302bd
Another two tests
antonpirker Jan 25, 2024
c54ca92
one gone again
antonpirker Jan 25, 2024
efffd01
and testing the other one
antonpirker Jan 25, 2024
81eea9a
Check first test
antonpirker Jan 25, 2024
e957fdb
If this is green now, i am back to square one.
antonpirker Jan 25, 2024
f2ee7c4
this is weird
antonpirker Jan 25, 2024
8ed4e34
one more test
antonpirker Jan 25, 2024
e73d85c
forking more test
antonpirker Jan 25, 2024
1c96e1f
more forking
antonpirker Jan 25, 2024
753620a
Cleanup
antonpirker Jan 25, 2024
0172098
Made ScopeType an enum
antonpirker Jan 25, 2024
f82bb36
Improved test asserts
antonpirker Jan 25, 2024
387f433
Fixed typing
antonpirker Jan 25, 2024
4d747d6
Moved _copy_on_write to other PR. Makes more sense there because we c…
antonpirker Jan 26, 2024
cb27c4c
linting
antonpirker Jan 26, 2024
9ae58ee
reformat
antonpirker Jan 26, 2024
7a22de1
Merge branch 'master' into new-scopes-add-api
antonpirker Jan 26, 2024
fca14a3
Update test matrix
antonpirker Jan 29, 2024
041e004
updated apidocs
antonpirker Jan 29, 2024
689ad15
Cleanup
antonpirker Feb 5, 2024
47c973e
Merge branch 'master' into new-scopes-add-api
antonpirker Feb 6, 2024
d3917e0
formatting
antonpirker Feb 6, 2024
7189e5e
Merge branch 'feature/new-scopes' into new-scopes-add-api
antonpirker Feb 7, 2024
f6cb4ef
Update sentry_sdk/scope.py
antonpirker Feb 12, 2024
72365d4
Renamed NoopClient to NonRecordingClient to be more like otel
antonpirker Feb 12, 2024
ae39a43
Added some comments
antonpirker Feb 12, 2024
f908fe8
Added the right version number
antonpirker Feb 12, 2024
9247623
Added universal lock and use it when changing the global scope
antonpirker Feb 12, 2024
6be5e5f
Added tests for UniversalLock
antonpirker Feb 12, 2024
3b3537f
Made properties of BaseClient actual properties, not class vars
antonpirker Feb 12, 2024
27522d0
Fixed default value
antonpirker Feb 12, 2024
a821b93
Do not call async tests in python 2
antonpirker Feb 12, 2024
fcb3f68
Make it run in old python
antonpirker Feb 12, 2024
161a1ac
Make it work with old python
antonpirker Feb 12, 2024
b7c0db5
Make api consistent
antonpirker Feb 12, 2024
56b266b
Removed useless universal lock, because locking is not the responsibi…
antonpirker Feb 12, 2024
f7f8d42
Apply suggestions from code review
antonpirker Feb 12, 2024
b57cde6
Prevent infinite recursion by using default options.
antonpirker Feb 13, 2024
90d8d20
Merge branch 'new-scopes-add-api' of github.com:getsentry/sentry-pyth…
antonpirker Feb 13, 2024
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
13 changes: 13 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ Distributed Tracing
.. autofunction:: sentry_sdk.api.get_traceparent


New Scopes/Client APIs
======================

.. autofunction:: sentry_sdk.api.get_client
.. autofunction:: sentry_sdk.api.sentry_is_initialized
.. autofunction:: sentry_sdk.api.get_current_scope
.. autofunction:: sentry_sdk.api.get_isolation_scope
.. autofunction:: sentry_sdk.api.get_global_scope

.. autofunction:: sentry_sdk.api.set_current_scope
.. autofunction:: sentry_sdk.api.set_isolation_scope


Managing Scope (advanced)
=========================

Expand Down
6 changes: 6 additions & 0 deletions docs/apidocs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ API Docs
.. autoclass:: sentry_sdk.Client
:members:

.. autoclass:: sentry_sdk.client._Client
:members:

.. autoclass:: sentry_sdk.client.NoopClient
:members:

.. autoclass:: sentry_sdk.Transport
:members:

Expand Down
71 changes: 65 additions & 6 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect

from sentry_sdk import scope
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope
Expand All @@ -15,6 +16,7 @@
from typing import ContextManager
from typing import Union

from sentry_sdk.client import Client, NoopClient

Check warning on line 19 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L19

Added line #L19 was not covered by tests
from sentry_sdk._types import (
Event,
Hint,
Expand Down Expand Up @@ -77,36 +79,93 @@
return f


def sentry_is_initialized():
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
# type: () -> bool
"""
Returns whether Sentry has been initialized or not.
If an client is available Sentry is initialized.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 1.XX.0
"""
return Scope.get_client().is_active()

Check warning on line 90 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L90

Added line #L90 was not covered by tests


@scopemethod
def get_client():
# type: () -> Union[Client, NoopClient]
return Scope.get_client()

Check warning on line 96 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L96

Added line #L96 was not covered by tests


@scopemethod
def get_current_scope():
# type: () -> Scope
return Scope.get_current_scope()

Check warning on line 102 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L102

Added line #L102 was not covered by tests


@scopemethod
def get_isolation_scope():
# type: () -> Scope
return Scope.get_isolation_scope()

Check warning on line 108 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L108

Added line #L108 was not covered by tests


@scopemethod
def get_global_scope():
# type: () -> Scope
return Scope.get_global_scope()

Check warning on line 114 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L114

Added line #L114 was not covered by tests


def set_current_scope(new_current_scope):
# type: (Scope) -> None
"""
Sets the given scope as the new current scope overwritting the existing current scope.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
:param new_current_scope: The scope to set as the new current scope.

.. versionadded:: 1.XX.0
"""
scope.sentry_current_scope.set(new_current_scope)

Check warning on line 125 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L125

Added line #L125 was not covered by tests
antonpirker marked this conversation as resolved.
Show resolved Hide resolved


def set_isolation_scope(new_isolation_scope):
# type: (Scope) -> None
"""
Sets the given scope as the new isolation scope overwritting the existing isolation scope.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
:param new_isolation_scope: The scope to set as the new isolation scope.

.. versionadded:: 1.XX.0
"""
scope.sentry_isolation_scope.set(new_isolation_scope)

Check warning on line 136 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L136

Added line #L136 was not covered by tests
antonpirker marked this conversation as resolved.
Show resolved Hide resolved


@hubmethod
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
139 changes: 136 additions & 3 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Type
from typing import Union

Check warning on line 47 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L46-L47

Added lines #L46 - L47 were not covered by tests

from sentry_sdk.integrations import Integration

Check warning on line 49 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L49

Added line #L49 was not covered by tests
from sentry_sdk.scope import Scope
from sentry_sdk._types import Event, Hint
from sentry_sdk.session import Session
Expand Down Expand Up @@ -148,11 +151,116 @@
module_not_found_error = ImportError # type: ignore


class _Client(object):
class NoopClient:
"""
A client that does not send any events to Sentry. This is used as a fallback when the Sentry SDK is not yet initialized.

.. versionadded:: 1.XX.0
"""

options = _get_options() # type: Dict[str, Any]
metrics_aggregator = None # type: Optional[Any]
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
monitor = None # type: Optional[Any]
transport = None # type: Optional[Any]
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

def __repr__(self):
# type: () -> str
return "<{} id={}>".format(self.__class__.__name__, id(self))
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

def should_send_default_pii(self):
# type: () -> bool
return False

Check warning on line 172 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L172

Added line #L172 was not covered by tests

def __init__(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 176 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L176

Added line #L176 was not covered by tests

antonpirker marked this conversation as resolved.
Show resolved Hide resolved
def __getstate__(self, *args, **kwargs):
# type: (*Any, **Any) -> Any
return {"options": {}}

Check warning on line 180 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L180

Added line #L180 was not covered by tests

def __setstate__(self, *args, **kwargs):
# type: (*Any, **Any) -> None
pass

Check warning on line 184 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L184

Added line #L184 was not covered by tests

def _setup_instrumentation(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 188 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L188

Added line #L188 was not covered by tests

def _init_impl(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 192 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L192

Added line #L192 was not covered by tests

def is_active(self):
# type: () -> bool
"""
Returns weither the client is active (able to send data to Sentry)
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 1.XX.0
"""
return False

Check warning on line 201 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L201

Added line #L201 was not covered by tests

@property
def dsn(self):
# type: () -> Optional[str]
return None

Check warning on line 206 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L206

Added line #L206 was not covered by tests

def _prepare_event(self, *args, **kwargs):
# type: (*Any, **Any) -> Optional[Any]
return None

Check warning on line 210 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L210

Added line #L210 was not covered by tests

def _is_ignored_error(self, *args, **kwargs):
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
# type: (*Any, **Any) -> bool
return True

Check warning on line 214 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L214

Added line #L214 was not covered by tests

def _should_capture(self, *args, **kwargs):
# type: (*Any, **Any) -> bool
return False

Check warning on line 218 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L218

Added line #L218 was not covered by tests

def _should_sample_error(self, *args, **kwargs):
# type: (*Any, **Any) -> bool
return False

Check warning on line 222 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L222

Added line #L222 was not covered by tests

def _update_session_from_event(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 226 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L226

Added line #L226 was not covered by tests

def capture_event(self, *args, **kwargs):
# type: (*Any, **Any) -> Optional[str]
return None

Check warning on line 230 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L230

Added line #L230 was not covered by tests

def capture_session(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 234 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L234

Added line #L234 was not covered by tests

def get_integration(self, *args, **kwargs):
# type: (*Any, **Any) -> Any
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
return None

Check warning on line 238 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L238

Added line #L238 was not covered by tests

def close(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 242 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L242

Added line #L242 was not covered by tests

def flush(self, *args, **kwargs):
# type: (*Any, **Any) -> None
return None

Check warning on line 246 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L246

Added line #L246 was not covered by tests

def __enter__(self):
# type: () -> NoopClient
return self

Check warning on line 250 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L250

Added line #L250 was not covered by tests

def __exit__(self, exc_type, exc_value, tb):
# type: (Any, Any, Any) -> None
return None

Check warning on line 254 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L254

Added line #L254 was not covered by tests


class _Client(NoopClient):
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
"""The client is internally responsible for capturing the events and
forwarding them to sentry through the configured transport. It takes
the client options as keyword arguments and optionally the DSN as first
argument.

Alias of :py:class:`sentry_sdk.Client`. (Was created for better intelisense support)
"""

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -292,6 +400,15 @@

self._setup_instrumentation(self.options.get("functions_to_trace", []))

def is_active(self):
# type: () -> bool
"""
Returns weither the client is active (able to send data to Sentry)
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 1.XX.0
"""
return True

Check warning on line 410 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L410

Added line #L410 was not covered by tests

@property
def dsn(self):
# type: () -> Optional[str]
Expand Down Expand Up @@ -557,8 +674,8 @@

:param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.

:param scope: An optional scope to use for determining whether this event
should be captured.
:param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
The `scope` and `scope_kwargs` parameters are mutually exclusive.

: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.
"""
Expand Down Expand Up @@ -661,6 +778,22 @@
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

Check warning on line 789 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L789

Added line #L789 was not covered by tests
elif name_or_class.identifier is not None:
integration_name = name_or_class.identifier
else:
raise ValueError("Integration has no name")

Check warning on line 793 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L793

Added line #L793 was not covered by tests

return self.integrations.get(integration_name)

def close(
self,
timeout=None, # type: Optional[float]
Expand Down
Loading
Loading