Skip to content

Commit 5e7825c

Browse files
authored
Merge branch 'master' into ivana/dont-iter-random-objects
2 parents 4f1c4e6 + 1c22130 commit 5e7825c

13 files changed

Lines changed: 666 additions & 430 deletions

File tree

scripts/test-lambda-locally/uv.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry_sdk/consts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ class SPANDATA:
882882
The messaging system's name, e.g. `kafka`, `aws_sqs`
883883
"""
884884

885+
MIDDLEWARE_NAME = "middleware.name"
886+
"""
887+
The middleware's name, e.g. `AuthenticationMiddleware`
888+
"""
889+
885890
NETWORK_PROTOCOL_NAME = "network.protocol.name"
886891
"""
887892
The application layer protocol name used for the network connection.

sentry_sdk/integrations/aiohttp.py

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -201,60 +201,41 @@ async def sentry_app_handle(
201201

202202
with span_ctx as span:
203203
try:
204-
try:
205-
response = await old_handle(self, request)
206-
except HTTPException as e:
207-
if isinstance(span, StreamedSpan) and not isinstance(
208-
span, NoOpStreamedSpan
209-
):
210-
span.set_attribute(
211-
"http.response.status_code", e.status_code
212-
)
213-
214-
if e.status_code >= 400:
215-
span.status = SpanStatus.ERROR.value
216-
else:
217-
span.status = SpanStatus.OK.value
218-
else:
219-
# Since a NoOpStreamedSpan can end up here, we have to guard against it
220-
# so this only gets set in the legacy transaction approach.
221-
if not isinstance(span, NoOpStreamedSpan):
222-
span.set_http_status(e.status_code)
223-
224-
if (
225-
e.status_code
226-
in integration._failed_request_status_codes
227-
):
228-
_capture_exception()
229-
raise
230-
except (asyncio.CancelledError, ConnectionResetError):
231-
if isinstance(span, StreamedSpan):
232-
span.status = SpanStatus.ERROR.value
233-
else:
234-
span.set_status(SPANSTATUS.CANCELLED)
235-
raise
236-
except Exception:
237-
# This will probably map to a 500 but seems like we
238-
# have no way to tell. Do not set span status.
239-
reraise(*_capture_exception())
240-
finally:
241-
# The handler has had a chance to read the body, so
242-
# request._read_bytes may now be populated. Capture
243-
# body data on the segment regardless of outcome.
204+
response = await old_handle(self, request)
205+
except HTTPException as e:
244206
if isinstance(span, StreamedSpan) and not isinstance(
245207
span, NoOpStreamedSpan
246208
):
247-
with capture_internal_exceptions():
248-
raw_data = get_aiohttp_request_data(request)
249-
body_data = (
250-
raw_data.value
251-
if isinstance(raw_data, AnnotatedValue)
252-
else raw_data
253-
)
254-
if body_data is not None:
255-
span._segment.set_attribute(
256-
"http.request.body.data", body_data
257-
)
209+
span.set_attribute(
210+
"http.response.status_code", e.status_code
211+
)
212+
213+
if e.status_code >= 400:
214+
span.status = SpanStatus.ERROR.value
215+
else:
216+
span.status = SpanStatus.OK.value
217+
else:
218+
# Since a NoOpStreamedSpan can end up here, we have to guard against it
219+
# so this only gets set in the legacy transaction approach.
220+
if not isinstance(span, NoOpStreamedSpan):
221+
span.set_http_status(e.status_code)
222+
223+
if (
224+
e.status_code
225+
in integration._failed_request_status_codes
226+
):
227+
_capture_exception()
228+
raise
229+
except (asyncio.CancelledError, ConnectionResetError):
230+
if isinstance(span, StreamedSpan):
231+
span.status = SpanStatus.ERROR.value
232+
else:
233+
span.set_status(SPANSTATUS.CANCELLED)
234+
raise
235+
except Exception:
236+
# This will probably map to a 500 but seems like we
237+
# have no way to tell. Do not set span status.
238+
reraise(*_capture_exception())
258239

259240
try:
260241
# A valid response handler will return a valid response with a status. But, if the handler

sentry_sdk/integrations/flask.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ def setup_once() -> None:
9595
def sentry_patched_wsgi_app(
9696
self: "Any", environ: "Dict[str, str]", start_response: "Callable[..., Any]"
9797
) -> "_ScopedResponse":
98-
if sentry_sdk.get_client().get_integration(FlaskIntegration) is None:
99-
return old_app(self, environ, start_response)
100-
10198
integration = sentry_sdk.get_client().get_integration(FlaskIntegration)
99+
if integration is None:
100+
return old_app(self, environ, start_response)
102101

103102
middleware = SentryWsgiMiddleware(
104103
lambda *a, **kw: old_app(self, *a, **kw),

sentry_sdk/integrations/socket.py

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import sentry_sdk
44
from sentry_sdk._types import MYPY
5-
from sentry_sdk.consts import OP
5+
from sentry_sdk.consts import OP, SPANDATA
66
from sentry_sdk.integrations import Integration
7+
from sentry_sdk.tracing_utils import has_span_streaming_enabled
78

89
if MYPY:
910
from socket import AddressFamily, SocketKind
@@ -50,22 +51,39 @@ def create_connection(
5051
timeout: "Optional[float]" = socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore
5152
source_address: "Optional[Tuple[Union[bytearray, bytes, str], int]]" = None,
5253
) -> "socket.socket":
53-
integration = sentry_sdk.get_client().get_integration(SocketIntegration)
54+
client = sentry_sdk.get_client()
55+
integration = client.get_integration(SocketIntegration)
5456
if integration is None:
5557
return real_create_connection(address, timeout, source_address)
5658

57-
with sentry_sdk.start_span(
58-
op=OP.SOCKET_CONNECTION,
59-
name=_get_span_description(address[0], address[1]),
60-
origin=SocketIntegration.origin,
61-
) as span:
62-
span.set_data("address", address)
63-
span.set_data("timeout", timeout)
64-
span.set_data("source_address", source_address)
65-
66-
return real_create_connection(
67-
address=address, timeout=timeout, source_address=source_address
68-
)
59+
if has_span_streaming_enabled(client.options):
60+
with sentry_sdk.traces.start_span(
61+
name=_get_span_description(address[0], address[1]),
62+
attributes={
63+
"sentry.op": OP.SOCKET_CONNECTION,
64+
"sentry.origin": SocketIntegration.origin,
65+
},
66+
) as span:
67+
if address[0] is not None:
68+
span.set_attribute(SPANDATA.SERVER_ADDRESS, address[0])
69+
span.set_attribute(SPANDATA.SERVER_PORT, address[1])
70+
71+
return real_create_connection(
72+
address=address, timeout=timeout, source_address=source_address
73+
)
74+
else:
75+
with sentry_sdk.start_span(
76+
op=OP.SOCKET_CONNECTION,
77+
name=_get_span_description(address[0], address[1]),
78+
origin=SocketIntegration.origin,
79+
) as span:
80+
span.set_data("address", address)
81+
span.set_data("timeout", timeout)
82+
span.set_data("source_address", source_address)
83+
84+
return real_create_connection(
85+
address=address, timeout=timeout, source_address=source_address
86+
)
6987

7088
socket.create_connection = create_connection # type: ignore
7189

@@ -81,18 +99,44 @@ def getaddrinfo(
8199
proto: int = 0,
82100
flags: int = 0,
83101
) -> "List[Tuple[AddressFamily, SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int], Tuple[int, bytes]]]]":
84-
integration = sentry_sdk.get_client().get_integration(SocketIntegration)
102+
client = sentry_sdk.get_client()
103+
integration = client.get_integration(SocketIntegration)
85104
if integration is None:
86105
return real_getaddrinfo(host, port, family, type, proto, flags)
87106

88-
with sentry_sdk.start_span(
89-
op=OP.SOCKET_DNS,
90-
name=_get_span_description(host, port),
91-
origin=SocketIntegration.origin,
92-
) as span:
93-
span.set_data("host", host)
94-
span.set_data("port", port)
95-
96-
return real_getaddrinfo(host, port, family, type, proto, flags)
107+
if has_span_streaming_enabled(client.options):
108+
with sentry_sdk.traces.start_span(
109+
name=_get_span_description(host, port),
110+
attributes={
111+
"sentry.op": OP.SOCKET_DNS,
112+
"sentry.origin": SocketIntegration.origin,
113+
},
114+
) as span:
115+
if isinstance(host, str):
116+
span.set_attribute(SPANDATA.SERVER_ADDRESS, host)
117+
elif isinstance(host, bytes):
118+
span.set_attribute(
119+
SPANDATA.SERVER_ADDRESS, host.decode(errors="replace")
120+
)
121+
122+
if isinstance(port, int):
123+
span.set_attribute(SPANDATA.SERVER_PORT, port)
124+
elif port is not None:
125+
try:
126+
span.set_attribute(SPANDATA.SERVER_PORT, int(port))
127+
except (ValueError, TypeError):
128+
pass
129+
130+
return real_getaddrinfo(host, port, family, type, proto, flags)
131+
else:
132+
with sentry_sdk.start_span(
133+
op=OP.SOCKET_DNS,
134+
name=_get_span_description(host, port),
135+
origin=SocketIntegration.origin,
136+
) as span:
137+
span.set_data("host", host)
138+
span.set_data("port", port)
139+
140+
return real_getaddrinfo(host, port, family, type, proto, flags)
97141

98142
socket.getaddrinfo = getaddrinfo

sentry_sdk/integrations/starlite.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from copy import deepcopy
22

33
import sentry_sdk
4-
from sentry_sdk.consts import OP
4+
from sentry_sdk.consts import OP, SPANDATA
55
from sentry_sdk.integrations import DidNotEnable, Integration
66
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
77
from sentry_sdk.scope import should_send_default_pii
88
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource
9+
from sentry_sdk.tracing_utils import has_span_streaming_enabled
910
from sentry_sdk.utils import (
1011
ensure_integration_enabled,
1112
event_from_exception,
@@ -141,29 +142,47 @@ async def _create_span_call(
141142
receive: "Receive",
142143
send: "Send",
143144
) -> None:
144-
if sentry_sdk.get_client().get_integration(StarliteIntegration) is None:
145+
client = sentry_sdk.get_client()
146+
if client.get_integration(StarliteIntegration) is None:
145147
return await old_call(self, scope, receive, send)
146148

147149
middleware_name = self.__class__.__name__
148-
with sentry_sdk.start_span(
149-
op=OP.MIDDLEWARE_STARLITE,
150-
name=middleware_name,
151-
origin=StarliteIntegration.origin,
150+
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
151+
152+
def _start_middleware_span(op: str, name: str) -> "Any":
153+
if is_span_streaming_enabled:
154+
return sentry_sdk.traces.start_span(
155+
name=name,
156+
attributes={
157+
"sentry.op": op,
158+
"sentry.origin": StarliteIntegration.origin,
159+
SPANDATA.MIDDLEWARE_NAME: middleware_name,
160+
},
161+
)
162+
return sentry_sdk.start_span(
163+
op=op,
164+
name=name,
165+
origin=StarliteIntegration.origin,
166+
)
167+
168+
with _start_middleware_span(
169+
op=OP.MIDDLEWARE_STARLITE, name=middleware_name
152170
) as middleware_span:
153-
middleware_span.set_tag("starlite.middleware_name", middleware_name)
171+
if not is_span_streaming_enabled:
172+
middleware_span.set_tag("starlite.middleware_name", middleware_name)
154173

155174
# Creating spans for the "receive" callback
156175
async def _sentry_receive(
157176
*args: "Any", **kwargs: "Any"
158177
) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]":
159178
if sentry_sdk.get_client().get_integration(StarliteIntegration) is None:
160179
return await receive(*args, **kwargs)
161-
with sentry_sdk.start_span(
180+
with _start_middleware_span(
162181
op=OP.MIDDLEWARE_STARLITE_RECEIVE,
163182
name=getattr(receive, "__qualname__", str(receive)),
164-
origin=StarliteIntegration.origin,
165183
) as span:
166-
span.set_tag("starlite.middleware_name", middleware_name)
184+
if not is_span_streaming_enabled:
185+
span.set_tag("starlite.middleware_name", middleware_name)
167186
return await receive(*args, **kwargs)
168187

169188
receive_name = getattr(receive, "__name__", str(receive))
@@ -174,12 +193,12 @@ async def _sentry_receive(
174193
async def _sentry_send(message: "Message") -> None:
175194
if sentry_sdk.get_client().get_integration(StarliteIntegration) is None:
176195
return await send(message)
177-
with sentry_sdk.start_span(
196+
with _start_middleware_span(
178197
op=OP.MIDDLEWARE_STARLITE_SEND,
179198
name=getattr(send, "__qualname__", str(send)),
180-
origin=StarliteIntegration.origin,
181199
) as span:
182-
span.set_tag("starlite.middleware_name", middleware_name)
200+
if not is_span_streaming_enabled:
201+
span.set_tag("starlite.middleware_name", middleware_name)
183202
return await send(message)
184203

185204
send_name = getattr(send, "__name__", str(send))

0 commit comments

Comments
 (0)