Skip to content
Draft
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
92 changes: 34 additions & 58 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sentry_sdk
from sentry_sdk._werkzeug import _get_headers, get_host
from sentry_sdk.api import continue_trace
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
from sentry_sdk.integrations._wsgi_common import (
Expand All @@ -15,9 +14,7 @@
)
from sentry_sdk.scope import Scope, should_send_default_pii, use_isolation_scope
from sentry_sdk.sessions import track_session
from sentry_sdk.traces import SegmentNameSource, StreamedSpan
from sentry_sdk.tracing import Span, TransactionSource
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.traces import SegmentNameSource
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
Expand All @@ -40,6 +37,7 @@
)

from sentry_sdk._types import Event, EventProcessor
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.utils import ExcInfo

WsgiResponseIter = TypeVar("WsgiResponseIter")
Expand Down Expand Up @@ -113,7 +111,6 @@ def __call__(
return self.app(environ, start_response)

client = sentry_sdk.get_client()
span_streaming = has_span_streaming_enabled(client.options)

_wsgi_middleware_applied.set(True)
try:
Expand All @@ -130,60 +127,42 @@ def __call__(

method = environ.get("REQUEST_METHOD", "").upper()

span_ctx: "Optional[ContextManager[Union[Span, StreamedSpan, None]]]" = None
span_ctx: "Optional[ContextManager[Union[StreamedSpan, None]]]" = (
None
)

if method in self.http_methods_to_capture:
if span_streaming:
sentry_sdk.traces.continue_trace(
dict(_get_headers(environ))
)
Scope.set_custom_sampling_context({"wsgi_environ": environ})

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
client_ip = get_client_ip(environ)
if client_ip:
scope.set_attribute(
SPANDATA.USER_IP_ADDRESS, client_ip
)
elif should_send_default_pii():
sentry_sdk.traces.continue_trace(dict(_get_headers(environ)))
Scope.set_custom_sampling_context({"wsgi_environ": environ})

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
client_ip = get_client_ip(environ)
if client_ip:
scope.set_attribute(
SPANDATA.USER_IP_ADDRESS, client_ip
)
elif should_send_default_pii():
client_ip = get_client_ip(environ)
if client_ip:
scope.set_attribute(SPANDATA.USER_IP_ADDRESS, client_ip)

span_ctx = sentry_sdk.traces.start_span(
name=_DEFAULT_TRANSACTION_NAME,
attributes={
"sentry.segment.name.source": SegmentNameSource.ROUTE,
"sentry.origin": self.span_origin,
"sentry.op": OP.HTTP_SERVER,
},
parent_span=None,
)

span_ctx = sentry_sdk.traces.start_span(
name=_DEFAULT_TRANSACTION_NAME,
attributes={
"sentry.segment.name.source": SegmentNameSource.ROUTE,
"sentry.origin": self.span_origin,
"sentry.op": OP.HTTP_SERVER,
},
parent_span=None,
)
else:
transaction = continue_trace(
environ,
op=OP.HTTP_SERVER,
name=_DEFAULT_TRANSACTION_NAME,
source=TransactionSource.ROUTE,
origin=self.span_origin,
)

span_ctx = sentry_sdk.start_transaction(
transaction,
custom_sampling_context={"wsgi_environ": environ},
)

span_ctx = span_ctx or nullcontext()

with span_ctx as span:
if isinstance(span, StreamedSpan):
with capture_internal_exceptions():
for attr, value in _get_request_attributes(
environ, self.use_x_forwarded_for
).items():
span.set_attribute(attr, value)
with span_ctx or nullcontext() as span:
with capture_internal_exceptions():
for attr, value in _get_request_attributes(
environ, self.use_x_forwarded_for
).items():
span.set_attribute(attr, value)

try:
response = self.app(
Expand Down Expand Up @@ -223,19 +202,16 @@ def __call__(

def _sentry_start_response(
old_start_response: "StartResponse",
span: "Optional[Union[Span, StreamedSpan]]",
span: "Optional[StreamedSpan]",
status: str,
response_headers: "WsgiResponseHeaders",
exc_info: "Optional[WsgiExcInfo]" = None,
) -> "WsgiResponseIter": # type: ignore[type-var]
with capture_internal_exceptions():
status_int = int(status.split(" ", 1)[0])
if span is not None:
if isinstance(span, StreamedSpan):
span.status = "error" if status_int >= 400 else "ok"
span.set_attribute("http.response.status_code", status_int)
else:
span.set_http_status(status_int)
span.status = "error" if status_int >= 400 else "ok"
span.set_attribute("http.response.status_code", status_int)

if exc_info is None:
# The Django Rest Framework WSGI test client, and likely other
Expand Down
Loading
Loading