import os
import platform
import sys
from collections import deque
from contextlib import contextmanager
from contextvars import ContextVar
from copy import copy, deepcopy
from datetime import datetime, timezone
from enum import Enum
from functools import wraps
from itertools import chain
from typing import TYPE_CHECKING
import sentry_sdk
from sentry_sdk._types import AnnotatedValue
from sentry_sdk.attachments import Attachment
from sentry_sdk.consts import (
DEFAULT_MAX_BREADCRUMBS,
FALSE_VALUES,
SPANDATA,
)
from sentry_sdk.feature_flags import DEFAULT_FLAG_CAPACITY, FlagBuffer
from sentry_sdk.session import Session
from sentry_sdk.traces import (
_DEFAULT_PARENT_SPAN,
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
NoOpSpan,
Span,
)
from sentry_sdk.tracing_utils import (
Baggage,
PropagationContext,
_make_sampling_decision,
has_tracing_enabled,
is_ignored_span,
)
from sentry_sdk.utils import (
capture_internal_exception,
capture_internal_exceptions,
datetime_from_isoformat,
disable_capture_event,
event_from_exception,
exc_info_from_error,
format_attribute,
logger,
)
if TYPE_CHECKING:
from collections.abc import Mapping
from typing import (
Any,
Callable,
Deque,
Dict,
Generator,
List,
Optional,
ParamSpec,
Tuple,
TypeVar,
Union,
)
import sentry_sdk
from sentry_sdk._types import (
Attributes,
AttributeValue,
Breadcrumb,
BreadcrumbHint,
ErrorProcessor,
Event,
EventProcessor,
ExcInfo,
Hint,
Log,
LogLevelStr,
Metric,
SpanLink,
Type,
)
P = ParamSpec("P")
R = TypeVar("R")
F = TypeVar("F", bound=Callable[..., Any])
T = TypeVar("T")
# Holds data that will be added to **all** events sent by this process.
# In case this is a http server (think web framework) with multiple users
# the data will be added to events of all users.
# Typically this is used for process wide data such as the release.
_global_scope: "Optional[Scope]" = None
# Holds data for the active request.
# This is used to isolate data for different requests or users.
# The isolation scope is usually created by integrations, but may also
# be created manually
_isolation_scope: "ContextVar[Optional[Scope]]" = ContextVar(
"isolation_scope", default=None
)
# Holds data for the active span.
# This can be used to manually add additional data to a span.
_current_scope: "ContextVar[Optional[Scope]]" = ContextVar(
"current_scope", default=None
)
global_event_processors: "List[EventProcessor]" = []
# A function returning a (trace_id, span_id) tuple
# from an external tracing source (such as otel)
_external_propagation_context_fn: "Optional[Callable[[], Optional[Tuple[str, str]]]]" = None
class ScopeType(Enum):
CURRENT = "current"
ISOLATION = "isolation"
GLOBAL = "global"
MERGED = "merged"
class _ScopeManager:
def __init__(self) -> None:
self._old_scopes: "List[Scope]" = []
def __enter__(self) -> "Scope":
isolation_scope = Scope.get_isolation_scope()
self._old_scopes.append(isolation_scope)
forked_scope = isolation_scope.fork()
_isolation_scope.set(forked_scope)
return forked_scope
def __exit__(self, exc_type: "Any", exc_value: "Any", tb: "Any") -> None:
old_scope = self._old_scopes.pop()
_isolation_scope.set(old_scope)
def add_global_event_processor(processor: "EventProcessor") -> None:
global_event_processors.append(processor)
def register_external_propagation_context(
fn: "Callable[[], Optional[Tuple[str, str]]]",
) -> None:
global _external_propagation_context_fn
_external_propagation_context_fn = fn
def remove_external_propagation_context() -> None:
global _external_propagation_context_fn
_external_propagation_context_fn = None
def get_external_propagation_context() -> "Optional[Tuple[str, str]]":
return (
_external_propagation_context_fn() if _external_propagation_context_fn else None
)
def has_external_propagation_context() -> bool:
return _external_propagation_context_fn is not None
def _attr_setter(fn: "Any") -> "Any":
return property(fset=fn, doc=fn.__doc__)
def _disable_capture(fn: "F") -> "F":
@wraps(fn)
def wrapper(self: "Any", *args: "Dict[str, Any]", **kwargs: "Any") -> "Any":
if not self._should_capture:
return
try:
self._should_capture = False
return fn(self, *args, **kwargs)
finally:
self._should_capture = True
return wrapper # type: ignore
[docs]
class Scope:
"""The scope holds extra information that should be sent with all
events that belong to it.
"""
# NOTE: Even though it should not happen, the scope needs to not crash when
# accessed by multiple threads. It's fine if it's full of races, but those
# races should never make the user application crash.
#
# The same needs to hold for any accesses of the scope the SDK makes.
__slots__ = (
"_level",
"_name",
"_fingerprint",
# note that for legacy reasons, _transaction is the transaction *name*,
# not a Transaction object (the object is stored in _span)
"_transaction",
"_transaction_info",
"_user",
"_tags",
"_contexts",
"_extras",
"_breadcrumbs",
"_n_breadcrumbs_truncated",
"_gen_ai_conversation_id",
"_event_processors",
"_error_processors",
"_should_capture",
"_span",
"_server_segment_span",
"_agent_framework_chat_generation_entered",
"_session",
"_attachments",
"_force_auto_session_tracking",
"_propagation_context",
"client",
"_type",
"_last_event_id",
"_flags",
"_attributes",
)
def __init__(
self,
ty: "Optional[ScopeType]" = None,
client: "Optional[sentry_sdk.Client]" = None,
) -> None:
self._type = ty
self._event_processors: "List[EventProcessor]" = []
self._error_processors: "List[ErrorProcessor]" = []
self._name: "Optional[str]" = None
self._propagation_context: "Optional[PropagationContext]" = None
self._n_breadcrumbs_truncated: int = 0
self._server_segment_span: "Optional[Span]" = None
self._agent_framework_chat_generation_entered: "bool" = False
self.client: "sentry_sdk.client.BaseClient" = NonRecordingClient()
if client is not None:
self.set_client(client)
self.clear()
incoming_trace_information = self._load_trace_data_from_env()
self.generate_propagation_context(incoming_data=incoming_trace_information)
def __copy__(self) -> "Scope":
"""
Returns a copy of this scope.
This also creates a copy of all referenced data structures.
"""
rv: "Scope" = object.__new__(self.__class__)
rv._type = self._type
rv.client = self.client
rv._level = self._level
rv._name = self._name
rv._fingerprint = self._fingerprint
rv._transaction = self._transaction
rv._transaction_info = self._transaction_info.copy()
rv._user = self._user
rv._tags = self._tags.copy()
rv._contexts = self._contexts.copy()
rv._extras = self._extras.copy()
rv._breadcrumbs = copy(self._breadcrumbs)
rv._n_breadcrumbs_truncated = self._n_breadcrumbs_truncated
rv._event_processors = self._event_processors.copy()
rv._error_processors = self._error_processors.copy()
rv._propagation_context = self._propagation_context
rv._should_capture = self._should_capture
rv._span = self._span
rv._server_segment_span = self._server_segment_span
rv._agent_framework_chat_generation_entered = (
self._agent_framework_chat_generation_entered
)
rv._session = self._session
rv._force_auto_session_tracking = self._force_auto_session_tracking
rv._attachments = self._attachments.copy()
rv._last_event_id = self._last_event_id
rv._flags = deepcopy(self._flags)
rv._attributes = self._attributes.copy()
rv._gen_ai_conversation_id = self._gen_ai_conversation_id
return rv
[docs]
@classmethod
def get_current_scope(cls) -> "Scope":
"""
.. versionadded:: 2.0.0
Returns the current scope.
"""
current_scope = _current_scope.get()
if current_scope is None:
current_scope = Scope(ty=ScopeType.CURRENT)
_current_scope.set(current_scope)
return current_scope
[docs]
@classmethod
def set_current_scope(cls, new_current_scope: "Scope") -> None:
"""
.. versionadded:: 2.0.0
Sets the given scope as the new current scope overwriting the existing current scope.
:param new_current_scope: The scope to set as the new current scope.
"""
_current_scope.set(new_current_scope)
[docs]
@classmethod
def get_isolation_scope(cls) -> "Scope":
"""
.. versionadded:: 2.0.0
Returns the isolation scope.
"""
isolation_scope = _isolation_scope.get()
if isolation_scope is None:
isolation_scope = Scope(ty=ScopeType.ISOLATION)
_isolation_scope.set(isolation_scope)
return isolation_scope
[docs]
@classmethod
def set_isolation_scope(cls, new_isolation_scope: "Scope") -> None:
"""
.. versionadded:: 2.0.0
Sets the given scope as the new isolation scope overwriting the existing isolation scope.
:param new_isolation_scope: The scope to set as the new isolation scope.
"""
_isolation_scope.set(new_isolation_scope)
[docs]
@classmethod
def get_global_scope(cls) -> "Scope":
"""
.. versionadded:: 2.0.0
Returns the global scope.
"""
global _global_scope
if _global_scope is None:
_global_scope = Scope(ty=ScopeType.GLOBAL)
return _global_scope
def set_global_attributes(self) -> None:
from sentry_sdk.client import SDK_INFO
self.set_attribute(SPANDATA.SENTRY_SDK_NAME, SDK_INFO["name"])
self.set_attribute(SPANDATA.SENTRY_SDK_VERSION, SDK_INFO["version"])
self.set_attribute(
"process.runtime.name",
platform.python_implementation(),
)
self.set_attribute(
"process.runtime.version",
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
)
options = sentry_sdk.get_client().options
server_name = options.get("server_name")
if server_name:
self.set_attribute(SPANDATA.SERVER_ADDRESS, server_name)
environment = options.get("environment")
if environment:
self.set_attribute(SPANDATA.SENTRY_ENVIRONMENT, environment)
release = options.get("release")
if release:
self.set_attribute(SPANDATA.SENTRY_RELEASE, release)
[docs]
@classmethod
def last_event_id(cls) -> "Optional[str]":
"""
.. versionadded:: 2.2.0
Returns event ID of the event most recently captured by the isolation scope, or None if no event
has been captured. We do not consider events that are dropped, e.g. by a before_send hook.
Transactions also are not considered events in this context.
The event corresponding to the returned event ID is NOT guaranteed to actually be sent to Sentry;
whether the event is sent depends on the transport. The event could be sent later or not at all.
Even a sent event could fail to arrive in Sentry due to network issues, exhausted quotas, or
various other reasons.
"""
return cls.get_isolation_scope()._last_event_id
def _merge_scopes(
self,
additional_scope: "Optional[Scope]" = None,
additional_scope_kwargs: "Optional[Dict[str, Any]]" = None,
) -> "Scope":
"""
Merges global, isolation and current scope into a new scope and
adds the given additional scope or additional scope kwargs to it.
"""
if additional_scope and additional_scope_kwargs:
raise TypeError("cannot provide scope and kwargs")
final_scope = copy(_global_scope) if _global_scope is not None else Scope()
final_scope._type = ScopeType.MERGED
isolation_scope = _isolation_scope.get()
if isolation_scope is not None:
final_scope.update_from_scope(isolation_scope)
current_scope = _current_scope.get()
if current_scope is not None:
final_scope.update_from_scope(current_scope)
if self != current_scope and self != isolation_scope:
final_scope.update_from_scope(self)
if additional_scope is not None:
if callable(additional_scope):
additional_scope(final_scope)
else:
final_scope.update_from_scope(additional_scope)
elif additional_scope_kwargs:
final_scope.update_from_kwargs(**additional_scope_kwargs)
return final_scope
[docs]
@classmethod
def get_client(cls) -> "sentry_sdk.client.BaseClient":
"""
.. versionadded:: 2.0.0
Returns the currently used :py:class:`sentry_sdk.Client`.
This checks the current scope, the isolation scope and the global scope for a client.
If no client is available a :py:class:`sentry_sdk.client.NonRecordingClient` is returned.
"""
current_scope = _current_scope.get()
try:
client = current_scope.client # type: ignore[union-attr]
except AttributeError:
client = None
if client is not None and client.is_active():
return client
isolation_scope = _isolation_scope.get()
try:
client = isolation_scope.client # type: ignore[union-attr]
except AttributeError:
client = None
if client is not None and client.is_active():
return client
try:
client = _global_scope.client # type: ignore
except AttributeError:
client = None
if client is not None and client.is_active():
return client
return NonRecordingClient()
[docs]
def set_client(
self, client: "Optional[sentry_sdk.client.BaseClient]" = None
) -> None:
"""
.. versionadded:: 2.0.0
Sets the client for this scope.
:param client: The client to use in this scope.
If `None` the client of the scope will be replaced by a :py:class:`sentry_sdk.NonRecordingClient`.
"""
if client is not None:
self.client = client
# We need a client to set the initial global attributes on the global
# scope since they mostly come from client options, so populate them
# as soon as a client is set
sentry_sdk.get_global_scope().set_global_attributes()
else:
self.client = NonRecordingClient()
[docs]
def fork(self) -> "Scope":
"""
.. versionadded:: 2.0.0
Returns a fork of this scope.
"""
forked_scope = copy(self)
return forked_scope
def _load_trace_data_from_env(self) -> "Optional[Dict[str, str]]":
"""
Load Sentry trace id and baggage from environment variables.
Can be disabled by setting SENTRY_USE_ENVIRONMENT to "false".
"""
incoming_trace_information: "Optional[Dict[str, str]]" = None
sentry_use_environment = (
os.environ.get("SENTRY_USE_ENVIRONMENT") or ""
).lower()
use_environment = sentry_use_environment not in FALSE_VALUES
if use_environment:
incoming_trace_information = {}
if os.environ.get("SENTRY_TRACE"):
incoming_trace_information[SENTRY_TRACE_HEADER_NAME] = (
os.environ.get("SENTRY_TRACE") or ""
)
if os.environ.get("SENTRY_BAGGAGE"):
incoming_trace_information[BAGGAGE_HEADER_NAME] = (
os.environ.get("SENTRY_BAGGAGE") or ""
)
return incoming_trace_information or None
[docs]
def set_new_propagation_context(self) -> None:
"""
Creates a new propagation context and sets it as `_propagation_context`. Overwriting existing one.
"""
self._propagation_context = PropagationContext()
[docs]
def generate_propagation_context(
self, incoming_data: "Optional[Dict[str, str]]" = None
) -> None:
"""
Makes sure the propagation context is set on the scope.
If there is `incoming_data` overwrite existing propagation context.
If there is no `incoming_data` create new propagation context, but do NOT overwrite if already existing.
"""
if incoming_data is not None:
self._propagation_context = PropagationContext.from_incoming_data(
incoming_data
)
# TODO-neel this below is a BIG code smell but requires a bunch of other refactoring
if self._type != ScopeType.CURRENT:
if self._propagation_context is None:
self.set_new_propagation_context()
[docs]
def get_traceparent(self, *args: "Any", **kwargs: "Any") -> "Optional[str]":
"""
Returns the Sentry "sentry-trace" header (aka the traceparent) from the
currently active span or the scopes Propagation Context.
"""
client = self.get_client()
if not has_tracing_enabled(client.options):
return self.get_active_propagation_context().to_traceparent()
# If we have an active span, return traceparent from there
if self.span is not None:
return self.span._to_traceparent()
# else return traceparent from the propagation context
return self.get_active_propagation_context().to_traceparent()
[docs]
def get_baggage(self, *args: "Any", **kwargs: "Any") -> "Optional[Baggage]":
"""
Returns the Sentry "baggage" header containing trace information from the
currently active span or the scopes Propagation Context.
"""
client = self.get_client()
if not has_tracing_enabled(client.options):
return self.get_active_propagation_context().get_baggage()
# If we have an active span, return baggage from there
if self.span is not None:
return self.span._to_baggage()
# else return baggage from the propagation context
return self.get_active_propagation_context().get_baggage()
[docs]
def get_trace_context(self) -> "Dict[str, Any]":
"""
Returns the Sentry "trace" context from the Propagation Context.
"""
if has_tracing_enabled(self.get_client().options) and self._span is not None:
return self._span._get_trace_context()
# if we are tracing externally (otel), those values take precedence
external_propagation_context = get_external_propagation_context()
if external_propagation_context:
trace_id, span_id = external_propagation_context
return {"trace_id": trace_id, "span_id": span_id}
propagation_context = self.get_active_propagation_context()
return {
"trace_id": propagation_context.trace_id,
"span_id": propagation_context.span_id,
"parent_span_id": propagation_context.parent_span_id,
"dynamic_sampling_context": propagation_context.dynamic_sampling_context,
}
def get_active_propagation_context(self) -> "PropagationContext":
if self._propagation_context is not None:
return self._propagation_context
current_scope = self.get_current_scope()
if current_scope._propagation_context is not None:
return current_scope._propagation_context
isolation_scope = self.get_isolation_scope()
# should actually never happen, but just in case someone calls scope.clear
if isolation_scope._propagation_context is None:
isolation_scope._propagation_context = PropagationContext()
return isolation_scope._propagation_context
@classmethod
def set_custom_sampling_context(
cls, custom_sampling_context: "dict[str, Any]"
) -> None:
cls.get_current_scope().get_active_propagation_context()._set_custom_sampling_context(
custom_sampling_context
)
[docs]
def clear(self) -> None:
"""Clears the entire scope."""
self._level: "Optional[LogLevelStr]" = None
self._fingerprint: "Optional[List[str]]" = None
self._transaction: "Optional[str]" = None
self._transaction_info: "dict[str, str]" = {}
self._user: "Optional[Dict[str, Any]]" = None
self._tags: "Dict[str, Any]" = {}
self._contexts: "Dict[str, Dict[str, Any]]" = {}
self._extras: "dict[str, Any]" = {}
self._attachments: "List[Attachment]" = []
self.clear_breadcrumbs()
self._should_capture: bool = True
self._span: "Optional[Span]" = None
self._session: "Optional[Session]" = None
self._force_auto_session_tracking: "Optional[bool]" = None
self._propagation_context = None
# self._last_event_id is only applicable to isolation scopes
self._last_event_id: "Optional[str]" = None
self._flags: "Optional[FlagBuffer]" = None
self._attributes: "Attributes" = {}
self._gen_ai_conversation_id: "Optional[str]" = None
[docs]
def set_level(self, value: "LogLevelStr") -> None:
"""
Sets the level for the scope.
:param value: The level to set.
"""
self._level = value
@_attr_setter
def fingerprint(self, value: "Optional[List[str]]") -> None:
"""When set this overrides the default fingerprint."""
self._fingerprint = value
[docs]
def set_transaction_name(self, name: str, source: "Optional[str]" = None) -> None:
"""Set the transaction name and optionally the transaction source."""
self._transaction = name
if self._span:
self._span._segment.name = name
if source:
self._span._segment.set_attribute(
"sentry.segment.name.source", getattr(source, "value", source)
)
if source:
self._transaction_info["source"] = source
[docs]
def set_user(self, value: "Optional[Dict[str, Any]]") -> None:
"""Sets a user for the scope."""
self._user = value
session = self.get_isolation_scope()._session
if session is not None:
session.update(user=value)
@property
def span(self) -> "Optional[Span]":
"""Get/set current tracing span."""
return self._span if isinstance(self._span, Span) else None
@span.setter
def span(self, span: "Optional[Span]") -> None:
self._span = span
# Also set _transaction and _transaction_info in streaming mode as this
# is used for populating events and linking them to segments
if not isinstance(span, Span) or not span._is_segment():
return
if type(span) is Span:
self._transaction = span.name
if span._attributes.get("sentry.segment.name.source"):
self._transaction_info["source"] = str(
span._attributes["sentry.segment.name.source"]
)
return
if type(span) is NoOpSpan:
if span._name is not None:
self._transaction = span.name
if span._attributes.get("sentry.segment.name.source"):
self._transaction_info["source"] = str(
span._attributes["sentry.segment.name.source"]
)
[docs]
def set_tag(self, key: str, value: "Any") -> None:
"""
Sets a tag for a key to a specific value.
:param key: Key of the tag to set.
:param value: Value of the tag to set.
"""
self._tags[key] = value
[docs]
def remove_tag(self, key: str) -> None:
"""
Removes a specific tag.
:param key: Key of the tag to remove.
"""
self._tags.pop(key, None)
[docs]
def set_context(
self,
key: str,
value: "Dict[str, Any]",
) -> None:
"""
Binds a context at a certain key to a specific value.
"""
self._contexts[key] = value
[docs]
def remove_context(
self,
key: str,
) -> None:
"""Removes a context."""
self._contexts.pop(key, None)
[docs]
def set_conversation_id(self, conversation_id: str) -> None:
"""
Sets the conversation ID for gen_ai spans.
:param conversation_id: The conversation ID to set.
"""
self._gen_ai_conversation_id = conversation_id
[docs]
def get_conversation_id(self) -> "Optional[str]":
"""
Gets the conversation ID for gen_ai spans.
:returns: The conversation ID, or None if not set.
"""
return self._gen_ai_conversation_id
[docs]
def remove_conversation_id(self) -> None:
"""Removes the conversation ID."""
self._gen_ai_conversation_id = None
[docs]
def clear_breadcrumbs(self) -> None:
"""Clears breadcrumb buffer."""
self._breadcrumbs: "Deque[Breadcrumb]" = deque()
self._n_breadcrumbs_truncated = 0
[docs]
def add_attachment(
self,
bytes: "Union[None, bytes, Callable[[], bytes]]" = None,
filename: "Optional[str]" = None,
path: "Optional[str]" = None,
content_type: "Optional[str]" = None,
) -> None:
"""Adds an attachment to future events sent from this scope.
The parameters are the same as for the :py:class:`sentry_sdk.attachments.Attachment` constructor.
"""
self._attachments.append(
Attachment(
bytes=bytes,
path=path,
filename=filename,
content_type=content_type,
)
)
[docs]
def add_breadcrumb(
self,
crumb: "Optional[Breadcrumb]" = None,
hint: "Optional[BreadcrumbHint]" = None,
**kwargs: "Any",
) -> None:
"""
Adds a breadcrumb.
:param crumb: Dictionary with the data as the sentry v7/v8 protocol expects.
:param hint: An optional value that can be used by `before_breadcrumb`
to customize the breadcrumbs that are emitted.
"""
client = self.get_client()
if not client.is_active():
logger.info("Dropped breadcrumb because no client bound")
return
before_breadcrumb = client.options.get("before_breadcrumb")
max_breadcrumbs = client.options.get("max_breadcrumbs", DEFAULT_MAX_BREADCRUMBS)
crumb = dict(crumb or ())
crumb.update(kwargs)
if not crumb:
return
hint = dict(hint or ())
if crumb.get("timestamp") is None:
crumb["timestamp"] = datetime.now(timezone.utc)
if crumb.get("type") is None:
crumb["type"] = "default"
new_crumb = crumb
if before_breadcrumb is not None:
new_crumb = None
with capture_internal_exceptions():
new_crumb = before_breadcrumb(crumb, hint)
if new_crumb is not None:
self._breadcrumbs.append(new_crumb)
else:
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)
while len(self._breadcrumbs) > max_breadcrumbs:
self._breadcrumbs.popleft()
self._n_breadcrumbs_truncated += 1
[docs]
def start_span(
self,
name: str,
attributes: "Optional[Attributes]",
parent_span: "Optional[Span]",
active: bool,
links: "Optional[list[SpanLink]]" = None,
) -> "Span":
"""
Start a span.
The span's parent, unless provided explicitly via the `parent_span` argument,
will be the current active span, if any. If there is none, this span will
become the root of a new span tree. If you explicitly want this span to be
top-level without a parent, set `parent_span=None`.
``start_span()`` can either be used as context manager or you can use the span
object it returns and explicitly end it via ``span.end()``. The following is
equivalent::
import sentry_sdk
with sentry_sdk.start_span(name="My Span"):
# do something
# The span automatically finishes once the ``with`` block is exited
::
import sentry_sdk
span = sentry_sdk.start_span(name="My Span")
# do something
span.end()
To continue a trace from another service, call
`sentry_sdk.continue_trace()` prior to creating a top-level span.
:param name: The name to identify this span by.
:type name: str
:param attributes: Key-value attributes to set on the span from the start.
These will also be accessible in the traces sampler.
:type attributes: "Optional[Attributes]"
:param parent_span: A span instance that the new span should consider its
parent. If not provided, the parent will be set to the currently active
span, if any. If set to `None`, this span will become a new root-level
span.
:type parent_span: "Optional[Span]"
:param active: Controls whether spans started while this span is running
will automatically become its children. That's the default behavior. If
you want to create a span that shouldn't have any children (unless
provided explicitly via the `parent_span` argument), set this to `False`.
:type active: bool
:param links: Spans to link the span to, without making them its parent,
e.g. in other traces. More can be added with `span.add_link()` or
`span.add_links()`.
:type links: "Optional[list[SpanLink]]"
:return: The span that has been started.
:rtype: Span
"""
if isinstance(parent_span, NoOpSpan):
# parent_span is only set if the user explicitly set it
logger.debug(
"Ignored parent span provided. Span will be parented to the "
"currently active span instead."
)
if parent_span is _DEFAULT_PARENT_SPAN or isinstance(parent_span, NoOpSpan):
parent_span = self.span
# If no eligible parent_span was provided and there is no currently
# active span, this is a new segment
if parent_span is None:
propagation_context = self.get_active_propagation_context()
if is_ignored_span(name, attributes):
return NoOpSpan(
name=name,
attributes=attributes,
scope=self,
segment=None,
trace_id=propagation_context.trace_id,
parent_span_id=propagation_context.parent_span_id,
parent_sampled=propagation_context.parent_sampled,
baggage=propagation_context.baggage,
unsampled_reason="ignored",
)
sampled, sample_rate, sample_rand, outcome = _make_sampling_decision(
name,
attributes,
self,
)
if sample_rate is not None:
self._update_sample_rate(sample_rate)
if sampled is False or sampled is None:
return NoOpSpan(
name=name,
attributes=attributes,
scope=self,
segment=None,
trace_id=propagation_context.trace_id,
parent_span_id=propagation_context.parent_span_id,
parent_sampled=propagation_context.parent_sampled,
baggage=propagation_context.baggage,
sampled=sampled,
unsampled_reason=outcome,
sample_rand=sample_rand,
sample_rate=sample_rate,
)
return Span(
name=name,
attributes=attributes,
active=active,
scope=self,
segment=None,
trace_id=propagation_context.trace_id,
parent_span_id=propagation_context.parent_span_id,
parent_sampled=propagation_context.parent_sampled,
baggage=propagation_context.baggage,
sample_rand=sample_rand,
sample_rate=sample_rate,
links=links,
)
# This is a child span; take propagation context from the parent span
with new_scope():
if is_ignored_span(name, attributes):
return NoOpSpan(
name=name,
attributes=attributes,
segment=parent_span._segment,
trace_id=parent_span.trace_id,
parent_span_id=parent_span.span_id,
parent_sampled=parent_span.sampled,
unsampled_reason="ignored",
)
if isinstance(parent_span, NoOpSpan):
return NoOpSpan(
name=name,
attributes=attributes,
segment=parent_span._segment,
trace_id=parent_span.trace_id,
parent_span_id=parent_span.span_id,
parent_sampled=parent_span.sampled,
unsampled_reason=parent_span._unsampled_reason,
)
return Span(
name=name,
attributes=attributes,
active=active,
scope=self,
segment=parent_span._segment,
trace_id=parent_span.trace_id,
parent_span_id=parent_span.span_id,
parent_sampled=parent_span.sampled,
links=links,
)
def _update_sample_rate(self, sample_rate: float) -> None:
# If we had to adjust the sample rate when setting the sampling decision
# for a span, it needs to be updated in the propagation context too
propagation_context = self.get_active_propagation_context()
baggage = propagation_context.baggage
if baggage is not None and baggage.sentry_items.get("sample_rate"):
baggage.sentry_items["sample_rate"] = str(sample_rate)
[docs]
def capture_event(
self,
event: "Event",
hint: "Optional[Hint]" = None,
scope: "Optional[Scope]" = None,
**scope_kwargs: "Any",
) -> "Optional[str]":
"""
Captures an event.
Merges given scope data and calls :py:meth:`sentry_sdk.client._Client.capture_event`.
:param event: A ready-made event that can be directly sent to Sentry.
: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.
:param scope_kwargs: Optional data to apply to event.
For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
The `scope` and `scope_kwargs` parameters are mutually exclusive.
:returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
"""
if disable_capture_event.get(False):
return None
scope = self._merge_scopes(scope, scope_kwargs)
event_id = self.get_client().capture_event(event=event, hint=hint, scope=scope)
if event_id is not None:
self.get_isolation_scope()._last_event_id = event_id
return event_id
def _capture_log(self, log: "Optional[Log]") -> None:
if log is None:
return
client = self.get_client()
merged_scope = self._merge_scopes()
debug = client.options.get("debug", False)
if debug:
logger.debug(
f"[Sentry Logs] [{log.get('severity_text')}] {log.get('body')}"
)
client._capture_log(log, scope=merged_scope)
def _capture_metric(self, metric: "Optional[Metric]") -> None:
if metric is None:
return
client = self.get_client()
merged_scope = self._merge_scopes()
debug = client.options.get("debug", False)
if debug:
logger.debug(
f"[Sentry Metrics] [{metric.get('type')}] {metric.get('name')}: {metric.get('value')}"
)
client._capture_metric(metric, scope=merged_scope)
def _capture_span(self, span: "Optional[Span]") -> None:
if span is None:
return
merged_scope = self._merge_scopes()
client = self.get_client()
client._capture_span(span, scope=merged_scope)
[docs]
def capture_message(
self,
message: str,
level: "Optional[LogLevelStr]" = None,
scope: "Optional[Scope]" = None,
**scope_kwargs: "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 apply to events.
The `scope` and `scope_kwargs` parameters are mutually exclusive.
:param scope_kwargs: Optional data to apply to event.
For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
The `scope` and `scope_kwargs` parameters are mutually exclusive.
:returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
"""
if disable_capture_event.get(False):
return None
if level is None:
level = "info"
event: "Event" = {
"message": message,
"level": level,
}
return self.capture_event(event, scope=scope, **scope_kwargs)
[docs]
def capture_exception(
self,
error: "Optional[Union[BaseException, ExcInfo]]" = None,
scope: "Optional[Scope]" = None,
**scope_kwargs: "Any",
) -> "Optional[str]":
"""Captures an exception.
:param error: An exception to capture. If `None`, `sys.exc_info()` will be used.
:param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
The `scope` and `scope_kwargs` parameters are mutually exclusive.
:param scope_kwargs: Optional data to apply to event.
For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
The `scope` and `scope_kwargs` parameters are mutually exclusive.
:returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`).
"""
if disable_capture_event.get(False):
return None
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)
try:
return self.capture_event(event, hint=hint, scope=scope, **scope_kwargs)
except Exception:
capture_internal_exception(sys.exc_info())
return None
[docs]
def start_session(self, *args: "Any", **kwargs: "Any") -> None:
"""Starts a new session."""
session_mode = kwargs.pop("session_mode", "application")
self.end_session()
client = self.get_client()
self._session = Session(
release=client.options.get("release"),
environment=client.options.get("environment"),
user=self._user,
session_mode=session_mode,
)
[docs]
def end_session(self, *args: "Any", **kwargs: "Any") -> None:
"""Ends the current session if there is one."""
session = self._session
self._session = None
if session is not None:
session.close()
self.get_client().capture_session(session)
[docs]
def stop_auto_session_tracking(self, *args: "Any", **kwargs: "Any") -> None:
"""Stops automatic session tracking.
This temporarily session tracking for the current scope when called.
To resume session tracking call `resume_auto_session_tracking`.
"""
self.end_session()
self._force_auto_session_tracking = False
[docs]
def resume_auto_session_tracking(self) -> None:
"""Resumes automatic session tracking for the current scope if
disabled earlier. This requires that generally automatic session
tracking is enabled.
"""
self._force_auto_session_tracking = None
[docs]
def add_event_processor(
self,
func: "EventProcessor",
) -> None:
"""Register a scope local event processor on the scope.
:param func: This function behaves like `before_send.`
"""
if len(self._event_processors) > 20:
logger.warning(
"Too many event processors on scope! Clearing list to free up some memory: %r",
self._event_processors,
)
del self._event_processors[:]
self._event_processors.append(func)
[docs]
def add_error_processor(
self,
func: "ErrorProcessor",
cls: "Optional[Type[BaseException]]" = None,
) -> None:
"""Register a scope local error processor on the scope.
:param func: A callback that works similar to an event processor but is invoked with the original exception info triple as second argument.
:param cls: Optionally, only process exceptions of this type.
"""
if cls is not None:
cls_ = cls # For mypy.
real_func = func
def func(event: "Event", exc_info: "ExcInfo") -> "Optional[Event]":
try:
is_inst = isinstance(exc_info[1], cls_)
except Exception:
is_inst = False
if is_inst:
return real_func(event, exc_info)
return event
self._error_processors.append(func)
def _apply_level_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if self._level is not None:
event["level"] = self._level
def _apply_breadcrumbs_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
event.setdefault("breadcrumbs", {})
# This check is just for mypy -
if not isinstance(event["breadcrumbs"], AnnotatedValue):
event["breadcrumbs"].setdefault("values", [])
event["breadcrumbs"]["values"].extend(self._breadcrumbs)
# Attempt to sort timestamps
try:
if not isinstance(event["breadcrumbs"], AnnotatedValue):
for crumb in event["breadcrumbs"]["values"]:
if isinstance(crumb["timestamp"], str):
crumb["timestamp"] = datetime_from_isoformat(crumb["timestamp"])
event["breadcrumbs"]["values"].sort(
key=lambda crumb: crumb["timestamp"]
)
except Exception as err:
logger.debug("Error when sorting breadcrumbs", exc_info=err)
pass
def _apply_user_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if event.get("user") is None and self._user is not None:
event["user"] = self._user
def _apply_transaction_name_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if event.get("transaction") is None and self._transaction is not None:
event["transaction"] = self._transaction
def _apply_transaction_info_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if event.get("transaction_info") is None and self._transaction_info is not None:
event["transaction_info"] = self._transaction_info
def _apply_fingerprint_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if event.get("fingerprint") is None and self._fingerprint is not None:
event["fingerprint"] = self._fingerprint
def _apply_extra_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if self._extras:
event.setdefault("extra", {}).update(self._extras)
def _apply_tags_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if self._tags:
event.setdefault("tags", {}).update(self._tags)
def _apply_contexts_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
if self._contexts:
event.setdefault("contexts", {}).update(self._contexts)
contexts = event.setdefault("contexts", {})
# Add "trace" context
if contexts.get("trace") is None:
contexts["trace"] = self.get_trace_context()
def _apply_flags_to_event(
self, event: "Event", hint: "Hint", options: "Optional[Dict[str, Any]]"
) -> None:
flags = self.flags.get()
if len(flags) > 0:
event.setdefault("contexts", {}).setdefault("flags", {}).update(
{"values": flags}
)
def _apply_scope_attributes_to_telemetry(
self, telemetry: "Union[Log, Metric, Span]"
) -> None:
# TODO: turn Logs, Metrics into actual classes
if isinstance(telemetry, dict):
attributes = telemetry["attributes"]
else:
attributes = telemetry._attributes
for attribute, value in self._attributes.items():
if attribute not in attributes:
attributes[attribute] = value
def _apply_user_attributes_to_telemetry(
self, telemetry: "Union[Log, Metric, Span]"
) -> None:
if isinstance(telemetry, dict):
attributes = telemetry["attributes"]
else:
attributes = telemetry._attributes
client_options = sentry_sdk.get_client().options
if not client_options["data_collection"]["user_info"] or self._user is None:
return
for attribute_name, user_attribute in (
("user.id", "id"),
("user.name", "username"),
("user.email", "email"),
("user.ip_address", "ip_address"),
):
if (
user_attribute in self._user
and attribute_name not in attributes
and self._user[user_attribute] is not None
):
attributes[attribute_name] = self._user[user_attribute]
[docs]
def run_error_processors(self, event: "Event", hint: "Hint") -> "Optional[Event]":
"""
Runs the error processors on the event and returns the modified event.
"""
exc_info = hint.get("exc_info")
if exc_info is not None:
error_processors = chain(
self.get_global_scope()._error_processors,
self.get_isolation_scope()._error_processors,
self.get_current_scope()._error_processors,
)
for error_processor in error_processors:
new_event = None
exception_raised = False
with capture_internal_exceptions():
try:
new_event = error_processor(event, exc_info)
except Exception:
exception_raised = True
raise
if new_event is None:
logger.info("%s (error processor) dropped event", error_processor)
transport = sentry_sdk.get_client().transport
if transport:
transport.record_lost_event(
"callback_error" if exception_raised else "event_processor",
data_category="error",
)
return None
event = new_event
return event
[docs]
def run_event_processors(self, event: "Event", hint: "Hint") -> "Optional[Event]":
"""
Runs the event processors on the event and returns the modified event.
"""
ty = event.get("type")
is_check_in = ty == "check_in"
if not is_check_in:
# Get scopes without creating them to prevent infinite recursion
isolation_scope = _isolation_scope.get()
current_scope = _current_scope.get()
event_processors = chain(
global_event_processors,
_global_scope and _global_scope._event_processors or [],
isolation_scope and isolation_scope._event_processors or [],
current_scope and current_scope._event_processors or [],
)
for event_processor in event_processors:
new_event = None
exception_raised = False
with capture_internal_exceptions():
try:
new_event = event_processor(event, hint)
except Exception:
exception_raised = True
raise
if new_event is None:
logger.info("%s (event processor) dropped event", event_processor)
transport = sentry_sdk.get_client().transport
if transport:
transport.record_lost_event(
"callback_error" if exception_raised else "event_processor",
data_category="error",
)
return None
event = new_event
return event
[docs]
@_disable_capture
def apply_to_event(
self,
event: "Event",
hint: "Hint",
options: "Optional[Dict[str, Any]]" = None,
) -> "Optional[Event]":
"""Applies the information contained on the scope to the given event."""
ty = event.get("type")
is_check_in = ty == "check_in"
# put all attachments into the hint. This lets callbacks play around
# with attachments. We also later pull this out of the hint when we
# create the envelope.
attachments_to_send = hint.get("attachments") or []
for attachment in self._attachments:
attachments_to_send.append(attachment)
hint["attachments"] = attachments_to_send
self._apply_contexts_to_event(event, hint, options)
if is_check_in:
# Check-ins only support the trace context, strip all others
event["contexts"] = {
"trace": event.setdefault("contexts", {}).get("trace", {})
}
if not is_check_in:
self._apply_level_to_event(event, hint, options)
self._apply_fingerprint_to_event(event, hint, options)
self._apply_user_to_event(event, hint, options)
self._apply_transaction_name_to_event(event, hint, options)
self._apply_transaction_info_to_event(event, hint, options)
self._apply_tags_to_event(event, hint, options)
self._apply_extra_to_event(event, hint, options)
self._apply_breadcrumbs_to_event(event, hint, options)
self._apply_flags_to_event(event, hint, options)
event = self.run_error_processors(event, hint)
if event is None:
return None
event = self.run_event_processors(event, hint)
if event is None:
return None
return event
@_disable_capture
def apply_to_telemetry(self, telemetry: "Union[Log, Metric, Span]") -> None:
# Attributes-based events and telemetry go through here (logs, metrics,
# spansV2)
if not isinstance(telemetry, Span):
trace_context = self.get_trace_context()
trace_id = trace_context.get("trace_id")
if telemetry.get("trace_id") is None and trace_id is not None:
telemetry["trace_id"] = trace_id
# span_id should only be populated if there's an active span. We can't
# use the trace_context here because it synthesizes a span_id if there
# isn't one
if telemetry.get("span_id") is None:
if self._span is not None and not isinstance(self._span, NoOpSpan):
telemetry["span_id"] = self._span.span_id
else:
external_propagation_context = get_external_propagation_context()
if external_propagation_context:
_, span_id = external_propagation_context
if span_id is not None:
telemetry["span_id"] = span_id
self._apply_scope_attributes_to_telemetry(telemetry)
self._apply_user_attributes_to_telemetry(telemetry)
[docs]
def update_from_scope(self, scope: "Scope") -> None:
"""Update the scope with another scope's data."""
if scope._level is not None:
self._level = scope._level
if scope._fingerprint is not None:
self._fingerprint = scope._fingerprint
if scope._transaction is not None:
self._transaction = scope._transaction
if scope._transaction_info is not None:
self._transaction_info.update(scope._transaction_info)
if scope._user is not None:
self._user = scope._user
if scope._tags:
self._tags.update(scope._tags)
if scope._contexts:
self._contexts.update(scope._contexts)
if scope._extras:
self._extras.update(scope._extras)
if scope._breadcrumbs:
self._breadcrumbs.extend(scope._breadcrumbs)
if scope._n_breadcrumbs_truncated:
self._n_breadcrumbs_truncated = (
self._n_breadcrumbs_truncated + scope._n_breadcrumbs_truncated
)
if scope._gen_ai_conversation_id:
self._gen_ai_conversation_id = scope._gen_ai_conversation_id
if scope._span:
self._span = scope._span
if scope._attachments:
self._attachments.extend(scope._attachments)
if scope._propagation_context:
self._propagation_context = scope._propagation_context
if scope._session:
self._session = scope._session
if scope._flags:
if not self._flags:
self._flags = deepcopy(scope._flags)
else:
for flag in scope._flags.get():
self._flags.set(flag["flag"], flag["result"])
if scope._attributes:
self._attributes.update(scope._attributes)
[docs]
def update_from_kwargs(
self,
user: "Optional[Any]" = None,
level: "Optional[LogLevelStr]" = None,
extras: "Optional[Dict[str, Any]]" = None,
contexts: "Optional[Dict[str, Dict[str, Any]]]" = None,
tags: "Optional[Dict[str, str]]" = None,
fingerprint: "Optional[List[str]]" = None,
attributes: "Optional[Attributes]" = None,
) -> None:
"""Update the scope's attributes."""
if level is not None:
self._level = level
if user is not None:
self._user = user
if extras is not None:
self._extras.update(extras)
if contexts is not None:
self._contexts.update(contexts)
if tags is not None:
self._tags.update(tags)
if fingerprint is not None:
self._fingerprint = fingerprint
if attributes is not None:
self._attributes.update(attributes)
def __repr__(self) -> str:
return "<%s id=%s name=%s type=%s>" % (
self.__class__.__name__,
hex(id(self)),
self._name,
self._type,
)
@property
def flags(self) -> "FlagBuffer":
if self._flags is None:
max_flags = (
self.get_client().options["_experiments"].get("max_flags")
or DEFAULT_FLAG_CAPACITY
)
self._flags = FlagBuffer(capacity=max_flags)
return self._flags
[docs]
def set_attribute(self, attribute: str, value: "AttributeValue") -> None:
"""
Set an attribute on the scope.
Any attributes-based telemetry (logs, metrics, streamed spans) captured
while this scope is active will inherit attributes set on the scope.
"""
self._attributes[attribute] = format_attribute(value)
[docs]
def set_attributes(self, attributes: "dict[str, AttributeValue]") -> None:
"""
Set multiple attributes on the scope.
Any attributes-based telemetry (logs, metrics, streamed spans) captured
while this scope is active will inherit attributes set on the scope.
"""
for attribute, value in attributes.items():
self.set_attribute(attribute, value)
[docs]
def remove_attribute(self, attribute: str) -> None:
"""Remove an attribute if set on the scope. No-op if there is no such attribute."""
try:
del self._attributes[attribute]
except KeyError:
pass
[docs]
@contextmanager
def new_scope() -> "Generator[Scope, None, None]":
"""
.. versionadded:: 2.0.0
Context manager that forks the current scope and runs the wrapped code in it.
After the wrapped code is executed, the original scope is restored.
Example Usage:
.. code-block:: python
import sentry_sdk
with sentry_sdk.new_scope() as scope:
scope.set_tag("color", "green")
sentry_sdk.capture_message("hello") # will include `color` tag.
sentry_sdk.capture_message("hello, again") # will NOT include `color` tag.
"""
# fork current scope
current_scope = Scope.get_current_scope()
new_scope = current_scope.fork()
token = _current_scope.set(new_scope)
try:
yield new_scope
finally:
try:
# restore original scope
_current_scope.reset(token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
@contextmanager
def use_scope(scope: "Scope") -> "Generator[Scope, None, None]":
"""
.. versionadded:: 2.0.0
Context manager that uses the given `scope` and runs the wrapped code in it.
After the wrapped code is executed, the original scope is restored.
Example Usage:
Suppose the variable `scope` contains a `Scope` object, which is not currently
the active scope.
.. code-block:: python
import sentry_sdk
with sentry_sdk.use_scope(scope):
scope.set_tag("color", "green")
sentry_sdk.capture_message("hello") # will include `color` tag.
sentry_sdk.capture_message("hello, again") # will NOT include `color` tag.
"""
# set given scope as current scope
token = _current_scope.set(scope)
try:
yield scope
finally:
try:
# restore original scope
_current_scope.reset(token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
@contextmanager
def isolation_scope() -> "Generator[Scope, None, None]":
"""
.. versionadded:: 2.0.0
Context manager that forks the current isolation scope and runs the wrapped code in it.
The current scope is also forked to not bleed data into the existing current scope.
After the wrapped code is executed, the original scopes are restored.
Example Usage:
.. code-block:: python
import sentry_sdk
with sentry_sdk.isolation_scope() as scope:
scope.set_tag("color", "green")
sentry_sdk.capture_message("hello") # will include `color` tag.
sentry_sdk.capture_message("hello, again") # will NOT include `color` tag.
"""
# fork current scope
current_scope = Scope.get_current_scope()
forked_current_scope = current_scope.fork()
current_token = _current_scope.set(forked_current_scope)
# fork isolation scope
isolation_scope = Scope.get_isolation_scope()
new_isolation_scope = isolation_scope.fork()
isolation_token = _isolation_scope.set(new_isolation_scope)
try:
yield new_isolation_scope
finally:
# restore original scopes
try:
_current_scope.reset(current_token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
try:
_isolation_scope.reset(isolation_token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
@contextmanager
def use_isolation_scope(isolation_scope: "Scope") -> "Generator[Scope, None, None]":
"""
.. versionadded:: 2.0.0
Context manager that uses the given `isolation_scope` and runs the wrapped code in it.
The current scope is also forked to not bleed data into the existing current scope.
After the wrapped code is executed, the original scopes are restored.
Example Usage:
.. code-block:: python
import sentry_sdk
with sentry_sdk.isolation_scope() as scope:
scope.set_tag("color", "green")
sentry_sdk.capture_message("hello") # will include `color` tag.
sentry_sdk.capture_message("hello, again") # will NOT include `color` tag.
"""
# fork current scope
current_scope = Scope.get_current_scope()
forked_current_scope = current_scope.fork()
current_token = _current_scope.set(forked_current_scope)
# set given scope as isolation scope
isolation_token = _isolation_scope.set(isolation_scope)
try:
yield isolation_scope
finally:
# restore original scopes
try:
_current_scope.reset(current_token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
try:
_isolation_scope.reset(isolation_token)
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())
# Circular imports
from sentry_sdk.client import NonRecordingClient
if TYPE_CHECKING:
import sentry_sdk.client