Skip to content
Merged
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
10 changes: 4 additions & 6 deletions sentry_sdk/integrations/fastapi.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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()

Expand Down
24 changes: 9 additions & 15 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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")

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down
82 changes: 36 additions & 46 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,64 +19,68 @@
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,
try_profile_lifecycle_trace_start,
)
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,
NoOpSpan,
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,
Expand All @@ -92,11 +97,8 @@
SamplingContext,
Type,
)

from sentry_sdk.tracing import TransactionKwargs

import sentry_sdk

P = ParamSpec("P")
R = TypeVar("R")

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 10 additions & 1 deletion sentry_sdk/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading