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

New scope - step 1 #2601

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions docs/apidocs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ API Docs
.. autoclass:: sentry_sdk.client._Client
:members:

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

.. autoclass:: sentry_sdk.Transport
:members:

Expand Down
62 changes: 62 additions & 0 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
from sentry_sdk._types import (
Event,
Hint,
Expand Down Expand Up @@ -77,6 +79,66 @@ def scopemethod(f):
return f


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

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


@scopemethod
def get_client():
# type: () -> Union[Client, NoopClient]
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()


def set_current_scope(new_current_scope):
# type: (Scope) -> None
"""
Sets the given scope as the new current scope overwritting the existing current scope.

: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)


def set_isolation_scope(new_isolation_scope):
# type: (Scope) -> None
"""
Sets the given scope as the new isolation scope overwritting the existing isolation scope.

: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)


@hubmethod
def capture_event(
event, # type: Event
Expand Down
161 changes: 137 additions & 24 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,36 @@
import socket

from sentry_sdk._compat import datetime_utcnow, string_types, text_type, iteritems
from sentry_sdk.utils import (
capture_internal_exceptions,
current_stacktrace,
disable_capture_event,
format_timestamp,
get_sdk_name,
get_type_name,
get_default_release,
handle_in_app,
logger,
)
from sentry_sdk.serializer import serialize
from sentry_sdk.tracing import trace, has_tracing_enabled
from sentry_sdk.transport import make_transport
from sentry_sdk.consts import (
ClientConstructor,
DEFAULT_MAX_VALUE_LENGTH,
DEFAULT_OPTIONS,
INSTRUMENTER,
VERSION,
ClientConstructor,
)
from sentry_sdk.integrations import _DEFAULT_INTEGRATIONS, setup_integrations
from sentry_sdk.utils import ContextVar
from sentry_sdk.sessions import SessionFlusher
from sentry_sdk.envelope import Envelope
from sentry_sdk.integrations import _DEFAULT_INTEGRATIONS, setup_integrations
from sentry_sdk.monitor import Monitor
from sentry_sdk.profiler import has_profiling_enabled, setup_profiler
from sentry_sdk.scrubber import EventScrubber
from sentry_sdk.monitor import Monitor
from sentry_sdk.sessions import SessionFlusher
from sentry_sdk.spotlight import setup_spotlight

from sentry_sdk.tracing import trace, has_tracing_enabled
from sentry_sdk.transport import make_transport
from sentry_sdk.serializer import serialize
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.utils import (
capture_internal_exceptions,
ContextVar,
current_stacktrace,
disable_capture_event,
format_timestamp,
get_default_release,
get_sdk_name,
get_type_name,
handle_in_app,
logger,
)

if TYPE_CHECKING:
from typing import Any
Expand All @@ -46,12 +45,11 @@
from typing import Type
from typing import Union

from sentry_sdk._types import Event, Hint
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


_client_init_debug = ContextVar("client_init_debug")


Expand Down Expand Up @@ -151,13 +149,119 @@ def _get_options(*args, **kwargs):
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]
monitor = None # type: Optional[Any]
transport = None # type: Optional[Any]

def __repr__(self):
# type: () -> str
return "<{} id={}>".format(self.__class__.__name__, id(self))

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

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

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

return False

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

def __getstate__(self, *args, **kwargs):
# type: (*Any, **Any) -> Any
return {"options": {}}

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

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

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

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

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

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

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

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

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

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) -> Any
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: () -> NoopClient
return self

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


class _Client(NoopClient):
"""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):
Expand Down Expand Up @@ -297,6 +401,15 @@ def _capture_envelope(envelope):

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)

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

@property
def dsn(self):
# type: () -> Optional[str]
Expand Down
Loading
Loading