Skip to content

Commit e23be9e

Browse files
authored
1 parent 0e7d203 commit e23be9e

2 files changed

Lines changed: 108 additions & 153 deletions

File tree

‎sentry_sdk/integrations/quart.py‎

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from sentry_sdk.scope import should_send_default_pii
1414
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
1515
from sentry_sdk.traces import StreamedSpan, get_current_span
16-
from sentry_sdk.tracing import SOURCE_FOR_STYLE as TRANSACTION_SOURCE_FOR_STYLE
17-
from sentry_sdk.tracing_utils import has_span_streaming_enabled
1816
from sentry_sdk.utils import (
1917
capture_internal_exceptions,
2018
ensure_integration_enabled,
@@ -119,15 +117,10 @@ def decorator(old_func: "Any") -> "Any":
119117
@wraps(old_func)
120118
@ensure_integration_enabled(QuartIntegration, old_func)
121119
def _sentry_func(*args: "Any", **kwargs: "Any") -> "Any":
122-
client = sentry_sdk.get_client()
123-
if has_span_streaming_enabled(client.options):
124-
span = get_current_span()
125-
if span is not None and hasattr(span, "_segment"):
126-
span._segment._update_active_thread()
127-
else:
128-
current_scope = sentry_sdk.get_current_scope()
129-
if current_scope.transaction is not None:
130-
current_scope.transaction.update_active_thread()
120+
span = get_current_span()
121+
122+
if span is not None and hasattr(span, "_segment"):
123+
span._segment._update_active_thread()
131124

132125
return old_func(*args, **kwargs)
133126

@@ -149,11 +142,7 @@ def _set_transaction_name_and_source(
149142
"endpoint": request.url_rule.endpoint,
150143
}
151144

152-
source = (
153-
SEGMENT_SOURCE_FOR_STYLE[transaction_style]
154-
if has_span_streaming_enabled(sentry_sdk.get_client().options)
155-
else TRANSACTION_SOURCE_FOR_STYLE[transaction_style]
156-
)
145+
source = SEGMENT_SOURCE_FOR_STYLE[transaction_style]
157146

158147
scope.set_transaction_name(
159148
name=name_for_style[transaction_style],
@@ -191,79 +180,50 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:
191180

192181
scope = sentry_sdk.get_isolation_scope()
193182

194-
if has_span_streaming_enabled(sentry_sdk.get_client().options):
195-
current_span = get_current_span()
196-
if type(current_span) is StreamedSpan:
197-
segment = current_span._segment
183+
current_span = get_current_span()
184+
if type(current_span) is StreamedSpan:
185+
segment = current_span._segment
198186

199-
segment.set_attribute("http.request.method", request_websocket.method)
200-
header_attributes: "dict[str, Any]" = {}
187+
segment.set_attribute("http.request.method", request_websocket.method)
188+
header_attributes: "dict[str, Any]" = {}
201189

202-
for header, header_value in _filter_headers(
203-
dict(request_websocket.headers), use_annotated_value=False
204-
).items():
205-
header_attributes[f"http.request.header.{header.lower()}"] = (
206-
header_value
207-
)
190+
for header, header_value in _filter_headers(
191+
dict(request_websocket.headers), use_annotated_value=False
192+
).items():
193+
header_attributes[f"http.request.header.{header.lower()}"] = header_value
208194

209-
segment.set_attributes(header_attributes)
195+
segment.set_attributes(header_attributes)
210196

211-
client_options = sentry_sdk.get_client().options
212-
filtered_query_string = None
213-
if has_data_collection_enabled(client_options):
214-
query_string = request_websocket.query_string.decode(
215-
"utf-8", errors="replace"
216-
)
217-
if query_string:
218-
filtered_query_string = (
219-
_apply_data_collection_filtering_to_query_string(
220-
query_string=query_string,
221-
behaviour=client_options["data_collection"][
222-
"url_query_params"
223-
],
224-
)
197+
client_options = sentry_sdk.get_client().options
198+
filtered_query_string = None
199+
if has_data_collection_enabled(client_options):
200+
query_string = request_websocket.query_string.decode(
201+
"utf-8", errors="replace"
202+
)
203+
if query_string:
204+
filtered_query_string = (
205+
_apply_data_collection_filtering_to_query_string(
206+
query_string=query_string,
207+
behaviour=client_options["data_collection"]["url_query_params"],
225208
)
226-
if filtered_query_string:
227-
segment.set_attribute(
228-
"url.query",
229-
filtered_query_string,
230-
)
231-
232-
parsed_url = parse_url(request_websocket.url)
233-
segment.set_attribute(
234-
"url.full",
235-
f"{parsed_url.url}?{filtered_query_string}"
236-
if filtered_query_string
237-
else parsed_url.url,
238209
)
210+
if filtered_query_string:
211+
segment.set_attribute(
212+
"url.query",
213+
filtered_query_string,
214+
)
239215

240-
if client_options["data_collection"]["user_info"]:
241-
user_properties = {}
242-
243-
if len(request_websocket.access_route) >= 1:
244-
segment.set_attribute(
245-
"client.address", request_websocket.access_route[0]
246-
)
247-
user_properties["ip_address"] = request_websocket.access_route[
248-
0
249-
]
250-
251-
current_user_id = _get_current_user_id_from_quart()
252-
if current_user_id:
253-
user_properties["id"] = current_user_id
254-
255-
if user_properties:
256-
existing_user_properties = scope._user or {}
257-
scope.set_user({**existing_user_properties, **user_properties})
258-
259-
elif should_send_default_pii():
260-
segment.set_attribute("url.full", request_websocket.url)
261-
segment.set_attribute(
262-
"url.query",
263-
request_websocket.query_string.decode("utf-8", errors="replace"),
264-
)
216+
parsed_url = parse_url(request_websocket.url)
217+
segment.set_attribute(
218+
"url.full",
219+
f"{parsed_url.url}?{filtered_query_string}"
220+
if filtered_query_string
221+
else parsed_url.url,
222+
)
265223

224+
if client_options["data_collection"]["user_info"]:
266225
user_properties = {}
226+
267227
if len(request_websocket.access_route) >= 1:
268228
segment.set_attribute(
269229
"client.address", request_websocket.access_route[0]
@@ -278,6 +238,28 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:
278238
existing_user_properties = scope._user or {}
279239
scope.set_user({**existing_user_properties, **user_properties})
280240

241+
elif should_send_default_pii():
242+
segment.set_attribute("url.full", request_websocket.url)
243+
segment.set_attribute(
244+
"url.query",
245+
request_websocket.query_string.decode("utf-8", errors="replace"),
246+
)
247+
248+
user_properties = {}
249+
if len(request_websocket.access_route) >= 1:
250+
segment.set_attribute(
251+
"client.address", request_websocket.access_route[0]
252+
)
253+
user_properties["ip_address"] = request_websocket.access_route[0]
254+
255+
current_user_id = _get_current_user_id_from_quart()
256+
if current_user_id:
257+
user_properties["id"] = current_user_id
258+
259+
if user_properties:
260+
existing_user_properties = scope._user or {}
261+
scope.set_user({**existing_user_properties, **user_properties})
262+
281263
evt_processor = _make_request_event_processor(app, request_websocket, integration)
282264
scope.add_event_processor(evt_processor)
283265

0 commit comments

Comments
 (0)