Skip to content

Commit 6f94662

Browse files
authored
ref(trimming): Keep frames from both ends of the trace (#3905)
Currently we trim frames from only one end of the stacktrace, this works decently well except for recursion errors where you would also be interested in what triggered the recursion initially. Adjust the trimming to keep some frames from the beginning of the trace, the majority is still taken from the end.
1 parent 5f11c71 commit 6f94662

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Bug Fixes**:
6+
7+
- Keep frames from both ends of the stacktrace when trimming frames. ([#3905](https://github.com/getsentry/relay/pull/3905))
8+
39
## 24.8.0
410

511
**Bug Fixes**:

relay-event-normalization/src/trimming.rs

+65-6
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Processor for TrimmingProcessor {
283283
}
284284

285285
processor::apply(&mut stacktrace.frames, |frames, meta| {
286-
enforce_frame_hard_limit(frames, meta, 250);
286+
enforce_frame_hard_limit(frames, meta, 200, 50);
287287
Ok(())
288288
})?;
289289

@@ -359,12 +359,26 @@ fn trim_string(value: &mut String, meta: &mut Meta, max_chars: usize, max_chars_
359359
});
360360
}
361361

362-
fn enforce_frame_hard_limit(frames: &mut Array<Frame>, meta: &mut Meta, limit: usize) {
363-
// Trim down the frame list to a hard limit. Prioritize the last frames.
362+
/// Trim down the frame list to a hard limit.
363+
///
364+
/// The total limit is `recent_frames` + `old_frames`.
365+
/// `recent_frames` is the number of frames to keep from the beginning of the list,
366+
/// the most recent stack frames, `old_frames` is the last at the end of the list of frames,
367+
/// the oldest frames up the stack.
368+
///
369+
/// It makes sense to keep some of the old frames in recursion cases to see what actually caused
370+
/// the recursion.
371+
fn enforce_frame_hard_limit(
372+
frames: &mut Array<Frame>,
373+
meta: &mut Meta,
374+
recent_frames: usize,
375+
old_frames: usize,
376+
) {
364377
let original_length = frames.len();
378+
let limit = recent_frames + old_frames;
365379
if original_length > limit {
366380
meta.set_original_length(Some(original_length));
367-
let _ = frames.drain(0..original_length - limit);
381+
let _ = frames.drain(old_frames..original_length - recent_frames);
368382
}
369383
}
370384

@@ -823,7 +837,13 @@ mod tests {
823837
]);
824838

825839
processor::apply(&mut frames, |f, m| {
826-
enforce_frame_hard_limit(f, m, 3);
840+
enforce_frame_hard_limit(f, m, 3, 0);
841+
Ok(())
842+
})
843+
.unwrap();
844+
845+
processor::apply(&mut frames, |f, m| {
846+
enforce_frame_hard_limit(f, m, 1, 2);
827847
Ok(())
828848
})
829849
.unwrap();
@@ -850,7 +870,7 @@ mod tests {
850870
]);
851871

852872
processor::apply(&mut frames, |f, m| {
853-
enforce_frame_hard_limit(f, m, 3);
873+
enforce_frame_hard_limit(f, m, 3, 0);
854874
Ok(())
855875
})
856876
.unwrap();
@@ -871,6 +891,45 @@ mod tests {
871891
);
872892
}
873893

894+
#[test]
895+
fn test_frame_hard_limit_recent_old() {
896+
fn create_frame(filename: &str) -> Annotated<Frame> {
897+
Annotated::new(Frame {
898+
filename: Annotated::new(filename.into()),
899+
..Default::default()
900+
})
901+
}
902+
903+
let mut frames = Annotated::new(vec![
904+
create_frame("foo1.py"),
905+
create_frame("foo2.py"),
906+
create_frame("foo3.py"),
907+
create_frame("foo4.py"),
908+
create_frame("foo5.py"),
909+
]);
910+
911+
processor::apply(&mut frames, |f, m| {
912+
enforce_frame_hard_limit(f, m, 2, 1);
913+
Ok(())
914+
})
915+
.unwrap();
916+
917+
let mut expected_meta = Meta::default();
918+
expected_meta.set_original_length(Some(5));
919+
920+
assert_eq!(
921+
frames,
922+
Annotated(
923+
Some(vec![
924+
create_frame("foo1.py"),
925+
create_frame("foo4.py"),
926+
create_frame("foo5.py"),
927+
]),
928+
expected_meta
929+
)
930+
);
931+
}
932+
874933
#[test]
875934
fn test_slim_frame_data_under_max() {
876935
let mut frames = vec![Annotated::new(Frame {

0 commit comments

Comments
 (0)