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 all 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.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.BaseClient
:members:

.. autoclass:: sentry_sdk.client.NonRecordingClient
:members:

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

Expand Down
31 changes: 20 additions & 11 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,35 @@
"init",
"integrations",
# From sentry_sdk.api
"add_breadcrumb",
"capture_event",
"capture_message",
"capture_exception",
"add_breadcrumb",
"capture_message",
"configure_scope",
"push_scope",
"continue_trace",
"flush",
"get_baggage",
"get_client",
"get_current_scope",
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
"get_current_span",
"get_global_scope",
"get_isolation_scope",
"get_traceparent",
"is_initialized",
"isolated_scope",
"last_event_id",
"start_span",
"start_transaction",
"set_tag",
"new_scope",
"push_scope",
"set_context",
"set_current_scope",
"set_extra",
"set_user",
"set_isolation_scope",
"set_level",
"set_measurement",
"get_current_span",
"get_traceparent",
"get_baggage",
"continue_trace",
"set_tag",
"set_user",
"start_span",
"start_transaction",
"trace",
]

Expand Down
81 changes: 69 additions & 12 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope
from sentry_sdk.scope import Scope, new_scope, isolated_scope
from sentry_sdk.tracing import NoOpSpan, Transaction

if TYPE_CHECKING:
Expand All @@ -15,6 +15,7 @@
from typing import ContextManager
from typing import Union

from sentry_sdk.client import BaseClient
from sentry_sdk._types import (
Event,
Hint,
Expand All @@ -36,26 +37,35 @@ def overload(x):

# When changing this, update __all__ in __init__.py too
__all__ = [
"add_breadcrumb",
"capture_event",
"capture_message",
"capture_exception",
"add_breadcrumb",
"capture_message",
"configure_scope",
"push_scope",
"continue_trace",
"flush",
"get_baggage",
"get_client",
"get_current_scope",
"get_current_span",
"get_global_scope",
"get_isolation_scope",
"get_traceparent",
"is_initialized",
"isolated_scope",
"last_event_id",
"start_span",
"start_transaction",
"set_tag",
"new_scope",
"push_scope",
"set_context",
"set_current_scope",
"set_extra",
"set_user",
"set_isolation_scope",
"set_level",
"set_measurement",
"get_current_span",
"get_traceparent",
"get_baggage",
"continue_trace",
"set_tag",
"set_user",
"start_span",
"start_transaction",
]


Expand All @@ -77,6 +87,53 @@ def scopemethod(f):
return f


def is_initialized():
# type: () -> bool
"""
Returns whether Sentry has been initialized or not.
If a client is available Sentry is initialized.

.. versionadded:: 2.0.0
"""
return Scope.get_client().is_active()


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


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


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


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


@scopemethod
def set_current_scope(new_current_scope):
# type: (Scope) -> None
return Scope.set_current_scope(new_current_scope)


@scopemethod
def set_isolation_scope(new_isolation_scope):
# type: (Scope) -> None
return Scope.set_isolation_scope(new_isolation_scope)


@hubmethod
def capture_event(
event, # type: Event
Expand Down
106 changes: 99 additions & 7 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
from typing import Type
from typing import Union

from sentry_sdk._types import Event, Hint
from sentry_sdk.integrations import Integration
from sentry_sdk.metrics import MetricsAggregator
from sentry_sdk.scope import Scope
from sentry_sdk._types import Event, Hint
from sentry_sdk.session import Session
from sentry_sdk.transport import Transport


_client_init_debug = ContextVar("client_init_debug")
Expand Down Expand Up @@ -151,19 +153,101 @@ def _get_options(*args, **kwargs):
module_not_found_error = ImportError # type: ignore


class _Client(object):
"""The client is internally responsible for capturing the events and
class BaseClient(object):
"""
The basic definition of a client that is used for sending data to Sentry.

.. versionadded:: 2.0.0
"""

def __init__(self, options=None):
# type: (Optional[Dict[str, Any]]) -> None
self.options = (
options if options is not None else DEFAULT_OPTIONS
) # type: Dict[str, Any]

self.transport = None # type: Optional[Transport]
self.monitor = None # type: Optional[Monitor]
self.metrics_aggregator = None # type: Optional[MetricsAggregator]

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

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

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

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

def is_active(self):
# type: () -> bool
"""
Returns whether the client is active (able to send data to Sentry)

.. versionadded:: 2.0.0
"""
return False

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

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

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

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

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

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

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


class NonRecordingClient(BaseClient):
"""
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:: 2.0.0
"""

pass


class _Client(BaseClient):
"""
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:`Client`. (Was created for better intelisense support)
Alias of :py:class:`sentry_sdk.Client`. (Was created for better intelisense support)
"""

def __init__(self, *args, **kwargs):
# type: (*Any, **Any) -> None
self.options = get_options(*args, **kwargs) # type: Dict[str, Any]

super(_Client, self).__init__(options=get_options(*args, **kwargs))
self._init_impl()

def __getstate__(self):
Expand Down Expand Up @@ -303,6 +387,15 @@ def _capture_envelope(envelope):

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

def is_active(self):
# type: () -> bool
"""
Returns whether the client is active (able to send data to Sentry)

.. versionadded:: 2.0.0
"""
return True

@property
def dsn(self):
# type: () -> Optional[str]
Expand Down Expand Up @@ -569,7 +662,6 @@ def capture_event(
: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 :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
Loading
Loading