Skip to content

Commit b7e2d71

Browse files
ref(replay): add resource.xhr log message (#97805)
- closes https://linear.app/getsentry/issue/REPLAY-589/add-log-line-for-xhrs-in-summary - see also getsentry/seer#3141 - also updates the `resource.fetch` log to be more similar to the other log messages.
1 parent 10ba47f commit b7e2d71

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/sentry/replays/lib/summarize.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ def as_log_message(event: dict[str, Any]) -> str | None:
293293

294294
# Parse URL path
295295
parsed_url = urlparse(description)
296-
path = f"{parsed_url.path}?{parsed_url.query}"
296+
path_part = parsed_url.path.lstrip("/")
297+
path = f"{parsed_url.netloc}/{path_part}"
298+
if parsed_url.query:
299+
path += f"?{parsed_url.query}"
297300

298301
# Check if the tuple is valid and response size exists
299302
sizes_tuple = parse_network_content_lengths(event)
@@ -306,17 +309,45 @@ def as_log_message(event: dict[str, Any]) -> str | None:
306309
return None
307310

308311
if response_size is None:
309-
return f'Application initiated request: "{method} {path} HTTP/2.0" with status code {status_code}; took {duration} milliseconds at {timestamp}'
312+
return (
313+
f'Fetch request "{method} {path}" failed with {status_code} at {timestamp}'
314+
)
310315
else:
311-
return f'Application initiated request: "{method} {path} HTTP/2.0" with status code {status_code} and response size {response_size}; took {duration} milliseconds at {timestamp}'
316+
return f'Fetch request "{method} {path}" failed with {status_code} ({response_size} bytes) at {timestamp}'
312317
case EventType.LCP:
313318
duration = event["data"]["payload"]["data"]["size"]
314319
rating = event["data"]["payload"]["data"]["rating"]
315320
return f"Application largest contentful paint: {duration} ms and has a {rating} rating at {timestamp}"
316321
case EventType.HYDRATION_ERROR:
317322
return f"There was a hydration error on the page at {timestamp}"
318323
case EventType.RESOURCE_XHR:
319-
return None
324+
payload = event["data"]["payload"]
325+
method = payload["data"]["method"]
326+
status_code = payload["data"]["statusCode"]
327+
description = payload["description"]
328+
duration = payload["endTimestamp"] - payload["startTimestamp"]
329+
330+
# Parse URL path
331+
parsed_url = urlparse(description)
332+
path_part = parsed_url.path.lstrip("/")
333+
path = f"{parsed_url.netloc}/{path_part}"
334+
if parsed_url.query:
335+
path += f"?{parsed_url.query}"
336+
337+
# Check if the tuple is valid and response size exists
338+
sizes_tuple = parse_network_content_lengths(event)
339+
response_size = None
340+
if sizes_tuple and sizes_tuple[1] is not None:
341+
response_size = str(sizes_tuple[1])
342+
343+
# Skip successful requests
344+
if status_code and str(status_code).startswith("2"):
345+
return None
346+
347+
if response_size is None:
348+
return f'XHR request "{method} {path}" failed with {status_code} at {timestamp}'
349+
else:
350+
return f'XHR request "{method} {path}" failed with {status_code} ({response_size} bytes) at {timestamp}'
320351
case EventType.MUTATIONS:
321352
return None
322353
case EventType.UNKNOWN:

tests/sentry/replays/lib/test_summarize.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,27 @@ def test_as_log_message() -> None:
241241
}
242242

243243
result = as_log_message(event)
244-
assert result is None
244+
assert result is None # we filter out 200 status codes
245245

246246
event = {
247247
"type": 5,
248248
"timestamp": 0.0,
249-
"data": {"tag": "performanceSpan", "payload": {"op": "resource.xhr"}},
249+
"data": {
250+
"tag": "performanceSpan",
251+
"payload": {
252+
"op": "resource.xhr",
253+
"description": "https://www.z.com/path?q=true",
254+
"endTimestamp": 0.0,
255+
"startTimestamp": 0.0,
256+
"data": {
257+
"method": "GET",
258+
"statusCode": 404,
259+
"response": {"size": 0},
260+
},
261+
},
262+
},
250263
}
251-
assert as_log_message(event) is None
264+
assert as_log_message(event) is not None
252265

253266
event = {
254267
"type": 5,

0 commit comments

Comments
 (0)