diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 10d4fd81ac..5833e5f290 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -1,19 +1,19 @@ import sys from copy import deepcopy from functools import wraps +from typing import TYPE_CHECKING import sentry_sdk from sentry_sdk.integrations import DidNotEnable from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.utils import transaction_from_function -from typing import TYPE_CHECKING - if TYPE_CHECKING: from typing import Any, Callable, Dict + from sentry_sdk._types import Event try: @@ -95,9 +95,7 @@ def _sentry_call(*args: "Any", **kwargs: "Any") -> "Any": if has_span_streaming_enabled(client.options): current_span = current_scope.streamed_span - if isinstance(current_span, StreamedSpan) and not isinstance( - current_span, NoOpStreamedSpan - ): + if type(current_span) is StreamedSpan: segment = current_span._segment segment._update_active_thread() diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 68ff98cc02..0018d87b09 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -1,17 +1,18 @@ import functools import json -import warnings import sys +import warnings from collections.abc import Set from copy import deepcopy from json import JSONDecodeError +from typing import TYPE_CHECKING import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import ( + _DEFAULT_FAILED_REQUEST_STATUS_CODES, DidNotEnable, Integration, - _DEFAULT_FAILED_REQUEST_STATUS_CODES, ) from sentry_sdk.integrations._wsgi_common import ( DEFAULT_HTTP_METHODS_TO_CAPTURE, @@ -21,7 +22,8 @@ ) from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.traces import NoOpStreamedSpan, StreamedSpan, _get_current_streamed_span +from sentry_sdk.traces import _get_current_streamed_span +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, TransactionSource, @@ -36,8 +38,6 @@ transaction_from_function, ) -from typing import TYPE_CHECKING - if TYPE_CHECKING: from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union @@ -54,7 +54,8 @@ ) from starlette.requests import Request # type: ignore from starlette.routing import Match # type: ignore - from starlette.types import ASGIApp, Receive, Scope as StarletteScope, Send # type: ignore + from starlette.types import ASGIApp, Receive, Send # type: ignore + from starlette.types import Scope as StarletteScope except ImportError: raise DidNotEnable("Starlette is not installed") @@ -255,12 +256,7 @@ def _set_request_body_data_on_streaming_segment( info: "Optional[Dict[str, Any]]", ) -> None: current_span = _get_current_streamed_span() - if ( - info - and "data" in info - and isinstance(current_span, StreamedSpan) - and not isinstance(current_span, NoOpStreamedSpan) - ): + if info and "data" in info and type(current_span) is StreamedSpan: with capture_internal_exceptions(): current_span._segment.set_attribute( "http.request.body.data", @@ -557,9 +553,7 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any": if span_streaming: current_span = current_scope.streamed_span - if isinstance(current_span, StreamedSpan) and not isinstance( - current_span, NoOpStreamedSpan - ): + if type(current_span) is StreamedSpan: current_span._segment._update_active_thread() elif current_scope.transaction is not None: current_scope.transaction.update_active_thread() diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 656fabc564..878d61c0a1 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -1,13 +1,14 @@ import os import sys import warnings -from copy import copy, deepcopy from collections import deque from contextlib import contextmanager -from enum import Enum +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, cast import sentry_sdk from sentry_sdk._types import AnnotatedValue @@ -18,7 +19,7 @@ INSTRUMENTER, SPANDATA, ) -from sentry_sdk.feature_flags import FlagBuffer, DEFAULT_FLAG_CAPACITY +from sentry_sdk.feature_flags import DEFAULT_FLAG_CAPACITY, FlagBuffer from sentry_sdk.profiler.continuous_profiler import ( get_profiler_id, try_autostart_continuous_profiler, @@ -26,15 +27,11 @@ ) from sentry_sdk.profiler.transaction_profiler import Profile from sentry_sdk.session import Session -from sentry_sdk.tracing_utils import ( - Baggage, - has_tracing_enabled, - has_span_streaming_enabled, - is_ignored_span, - _make_sampling_decision, - PropagationContext, +from sentry_sdk.traces import ( + _DEFAULT_PARENT_SPAN, + NoOpStreamedSpan, + StreamedSpan, ) -from sentry_sdk.traces import _DEFAULT_PARENT_SPAN, StreamedSpan, NoOpStreamedSpan from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, @@ -42,40 +39,48 @@ Span, Transaction, ) +from sentry_sdk.tracing_utils import ( + Baggage, + PropagationContext, + _make_sampling_decision, + has_span_streaming_enabled, + has_tracing_enabled, + is_ignored_span, +) from sentry_sdk.utils import ( + ContextVar, capture_internal_exception, capture_internal_exceptions, - ContextVar, datetime_from_isoformat, disable_capture_event, event_from_exception, exc_info_from_error, format_attribute, - logger, has_logs_enabled, has_metrics_enabled, + logger, ) -from typing import TYPE_CHECKING, cast - if TYPE_CHECKING: from collections.abc import Mapping - - from typing import Any - from typing import Callable - from typing import Deque - from typing import Dict - from typing import Generator - from typing import Iterator - from typing import List - from typing import Optional - from typing import ParamSpec - from typing import Tuple - from typing import TypeVar - from typing import Union + from typing import ( + Any, + Callable, + Deque, + Dict, + Generator, + Iterator, + List, + Optional, + ParamSpec, + Tuple, + TypeVar, + Union, + ) from typing_extensions import Unpack + import sentry_sdk from sentry_sdk._types import ( Attributes, AttributeValue, @@ -92,11 +97,8 @@ SamplingContext, Type, ) - from sentry_sdk.tracing import TransactionKwargs - import sentry_sdk - P = ParamSpec("P") R = TypeVar("R") @@ -585,11 +587,7 @@ def get_traceparent(self, *args: "Any", **kwargs: "Any") -> "Optional[str]": span_streaming = has_span_streaming_enabled(client.options) # If we have an active span, return traceparent from there - if ( - span_streaming - and self.streamed_span is not None - and not isinstance(self.streamed_span, NoOpStreamedSpan) - ): + if span_streaming and type(self.streamed_span) is StreamedSpan: return self.streamed_span._to_traceparent() elif not span_streaming and self.span is not None: return self.span._to_traceparent() @@ -609,11 +607,7 @@ def get_baggage(self, *args: "Any", **kwargs: "Any") -> "Optional[Baggage]": span_streaming = has_span_streaming_enabled(client.options) # If we have an active span, return baggage from there - if ( - span_streaming - and self.streamed_span is not None - and not isinstance(self.streamed_span, NoOpStreamedSpan) - ): + if span_streaming and type(self.streamed_span) is StreamedSpan: return self.streamed_span._to_baggage() elif not span_streaming and self.span is not None: return self.span._to_baggage() @@ -918,11 +912,7 @@ def streamed_span(self, span: "Optional[StreamedSpan]") -> None: # Also set _transaction and _transaction_info in streaming mode as this # is used for populating events and linking them to segments - if ( - isinstance(span, StreamedSpan) - and not isinstance(span, NoOpStreamedSpan) - and span._is_segment() - ): + if type(span) is StreamedSpan and span._is_segment(): self._transaction = span.name if span._attributes.get("sentry.span.source"): self._transaction_info["source"] = str( diff --git a/sentry_sdk/traces.py b/sentry_sdk/traces.py index 47c532c523..56fab54fd2 100644 --- a/sentry_sdk/traces.py +++ b/sentry_sdk/traces.py @@ -31,7 +31,16 @@ ) if TYPE_CHECKING: - from typing import Any, Callable, Iterator, Optional, ParamSpec, TypeVar, Union + from typing import ( + Any, + Callable, + Iterator, + Optional, + ParamSpec, + TypeVar, + Union, + ) + from sentry_sdk._types import Attributes, AttributeValue from sentry_sdk.profiler.continuous_profiler import ContinuousProfile