|
2 | 2 | from copy import deepcopy |
3 | 3 |
|
4 | 4 | import sentry_sdk |
5 | | -from sentry_sdk.consts import OP |
| 5 | +from sentry_sdk.consts import OP, SPANDATA |
6 | 6 | from sentry_sdk.integrations import ( |
7 | 7 | _DEFAULT_FAILED_REQUEST_STATUS_CODES, |
8 | 8 | DidNotEnable, |
|
12 | 12 | from sentry_sdk.integrations.logging import ignore_logger |
13 | 13 | from sentry_sdk.scope import should_send_default_pii |
14 | 14 | from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource |
| 15 | +from sentry_sdk.tracing_utils import has_span_streaming_enabled |
15 | 16 | from sentry_sdk.utils import ( |
16 | 17 | ensure_integration_enabled, |
17 | 18 | event_from_exception, |
@@ -157,52 +158,103 @@ async def _create_span_call( |
157 | 158 | receive: "Receive", |
158 | 159 | send: "Send", |
159 | 160 | ) -> None: |
160 | | - if sentry_sdk.get_client().get_integration(LitestarIntegration) is None: |
| 161 | + client = sentry_sdk.get_client() |
| 162 | + if client.get_integration(LitestarIntegration) is None: |
161 | 163 | return await old_call(self, scope, receive, send) |
162 | 164 |
|
163 | 165 | middleware_name = self.__class__.__name__ |
164 | | - with sentry_sdk.start_span( |
165 | | - op=OP.MIDDLEWARE_LITESTAR, |
166 | | - name=middleware_name, |
167 | | - origin=LitestarIntegration.origin, |
168 | | - ) as middleware_span: |
169 | | - middleware_span.set_tag("litestar.middleware_name", middleware_name) |
170 | | - |
171 | | - # Creating spans for the "receive" callback |
172 | | - async def _sentry_receive( |
173 | | - *args: "Any", **kwargs: "Any" |
174 | | - ) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]": |
175 | | - if sentry_sdk.get_client().get_integration(LitestarIntegration) is None: |
176 | | - return await receive(*args, **kwargs) |
177 | | - with sentry_sdk.start_span( |
178 | | - op=OP.MIDDLEWARE_LITESTAR_RECEIVE, |
179 | | - name=getattr(receive, "__qualname__", str(receive)), |
180 | | - origin=LitestarIntegration.origin, |
181 | | - ) as span: |
182 | | - span.set_tag("litestar.middleware_name", middleware_name) |
183 | | - return await receive(*args, **kwargs) |
184 | | - |
185 | | - receive_name = getattr(receive, "__name__", str(receive)) |
186 | | - receive_patched = receive_name == "_sentry_receive" |
187 | | - new_receive = _sentry_receive if not receive_patched else receive |
188 | | - |
189 | | - # Creating spans for the "send" callback |
190 | | - async def _sentry_send(message: "Message") -> None: |
191 | | - if sentry_sdk.get_client().get_integration(LitestarIntegration) is None: |
192 | | - return await send(message) |
193 | | - with sentry_sdk.start_span( |
194 | | - op=OP.MIDDLEWARE_LITESTAR_SEND, |
195 | | - name=getattr(send, "__qualname__", str(send)), |
196 | | - origin=LitestarIntegration.origin, |
197 | | - ) as span: |
198 | | - span.set_tag("litestar.middleware_name", middleware_name) |
199 | | - return await send(message) |
200 | | - |
201 | | - send_name = getattr(send, "__name__", str(send)) |
202 | | - send_patched = send_name == "_sentry_send" |
203 | | - new_send = _sentry_send if not send_patched else send |
204 | | - |
205 | | - return await old_call(self, scope, new_receive, new_send) |
| 166 | + if has_span_streaming_enabled(client.options): |
| 167 | + with sentry_sdk.traces.start_span( |
| 168 | + name=middleware_name, |
| 169 | + attributes={ |
| 170 | + "sentry.op": OP.MIDDLEWARE_LITESTAR, |
| 171 | + "sentry.origin": LitestarIntegration.origin, |
| 172 | + }, |
| 173 | + ) as middleware_span: |
| 174 | + middleware_span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) |
| 175 | + |
| 176 | + # Creating spans for the "receive" callback |
| 177 | + async def _sentry_receive( |
| 178 | + *args: "Any", **kwargs: "Any" |
| 179 | + ) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]": |
| 180 | + if client.get_integration(LitestarIntegration) is None: |
| 181 | + return await receive(*args, **kwargs) |
| 182 | + with sentry_sdk.traces.start_span( |
| 183 | + name=getattr(receive, "__qualname__", str(receive)), |
| 184 | + attributes={ |
| 185 | + "sentry.op": OP.MIDDLEWARE_LITESTAR_RECEIVE, |
| 186 | + "sentry.origin": LitestarIntegration.origin, |
| 187 | + }, |
| 188 | + ) as span: |
| 189 | + span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) |
| 190 | + return await receive(*args, **kwargs) |
| 191 | + |
| 192 | + receive_name = getattr(receive, "__name__", str(receive)) |
| 193 | + receive_patched = receive_name == "_sentry_receive" |
| 194 | + new_receive = _sentry_receive if not receive_patched else receive |
| 195 | + |
| 196 | + # Creating spans for the "send" callback |
| 197 | + async def _sentry_send(message: "Message") -> None: |
| 198 | + if client.get_integration(LitestarIntegration) is None: |
| 199 | + return await send(message) |
| 200 | + with sentry_sdk.traces.start_span( |
| 201 | + name=getattr(send, "__qualname__", str(send)), |
| 202 | + attributes={ |
| 203 | + "sentry.op": OP.MIDDLEWARE_LITESTAR_SEND, |
| 204 | + "sentry.origin": LitestarIntegration.origin, |
| 205 | + }, |
| 206 | + ) as span: |
| 207 | + span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) |
| 208 | + return await send(message) |
| 209 | + |
| 210 | + send_name = getattr(send, "__name__", str(send)) |
| 211 | + send_patched = send_name == "_sentry_send" |
| 212 | + new_send = _sentry_send if not send_patched else send |
| 213 | + |
| 214 | + return await old_call(self, scope, new_receive, new_send) |
| 215 | + else: |
| 216 | + with sentry_sdk.start_span( |
| 217 | + op=OP.MIDDLEWARE_LITESTAR, |
| 218 | + name=middleware_name, |
| 219 | + origin=LitestarIntegration.origin, |
| 220 | + ) as middleware_span: |
| 221 | + middleware_span.set_tag("litestar.middleware_name", middleware_name) |
| 222 | + |
| 223 | + # Creating spans for the "receive" callback |
| 224 | + async def _sentry_receive( |
| 225 | + *args: "Any", **kwargs: "Any" |
| 226 | + ) -> "Union[HTTPReceiveMessage, WebSocketReceiveMessage]": |
| 227 | + if client.get_integration(LitestarIntegration) is None: |
| 228 | + return await receive(*args, **kwargs) |
| 229 | + with sentry_sdk.start_span( |
| 230 | + op=OP.MIDDLEWARE_LITESTAR_RECEIVE, |
| 231 | + name=getattr(receive, "__qualname__", str(receive)), |
| 232 | + origin=LitestarIntegration.origin, |
| 233 | + ) as span: |
| 234 | + span.set_tag("litestar.middleware_name", middleware_name) |
| 235 | + return await receive(*args, **kwargs) |
| 236 | + |
| 237 | + receive_name = getattr(receive, "__name__", str(receive)) |
| 238 | + receive_patched = receive_name == "_sentry_receive" |
| 239 | + new_receive = _sentry_receive if not receive_patched else receive |
| 240 | + |
| 241 | + # Creating spans for the "send" callback |
| 242 | + async def _sentry_send(message: "Message") -> None: |
| 243 | + if client.get_integration(LitestarIntegration) is None: |
| 244 | + return await send(message) |
| 245 | + with sentry_sdk.start_span( |
| 246 | + op=OP.MIDDLEWARE_LITESTAR_SEND, |
| 247 | + name=getattr(send, "__qualname__", str(send)), |
| 248 | + origin=LitestarIntegration.origin, |
| 249 | + ) as span: |
| 250 | + span.set_tag("litestar.middleware_name", middleware_name) |
| 251 | + return await send(message) |
| 252 | + |
| 253 | + send_name = getattr(send, "__name__", str(send)) |
| 254 | + send_patched = send_name == "_sentry_send" |
| 255 | + new_send = _sentry_send if not send_patched else send |
| 256 | + |
| 257 | + return await old_call(self, scope, new_receive, new_send) |
206 | 258 |
|
207 | 259 | not_yet_patched = old_call.__name__ not in ["_create_span_call"] |
208 | 260 |
|
|
0 commit comments