sentry_backtrace/
trim.rs

1use sentry_core::protocol::{Frame, Stacktrace};
2
3use crate::utils::function_starts_with;
4
5const WELL_KNOWN_NOT_IN_APP: &[&str] = &[
6    // standard library and sentry crates
7    "std::",
8    "core::",
9    "alloc::",
10    "backtrace::",
11    "sentry::",
12    "sentry_core::",
13    "sentry_types::",
14    "sentry_backtrace::",
15    // these are not modules but things like __rust_maybe_catch_panic
16    "__rust_",
17    "___rust_",
18    "rust_begin_unwind",
19    "_start",
20    // these are well-known library frames
21    "anyhow::",
22    "log::",
23    "tokio::",
24    "tracing_core::",
25    "futures_core::",
26    "futures_util::",
27];
28
29const WELL_KNOWN_BORDER_FRAMES: &[&str] = &[
30    "std::panicking::begin_panic",
31    "core::panicking::panic",
32    // well-known library frames
33    "anyhow::",
34    "<sentry_log::Logger as log::Log>::log",
35    "tracing_core::",
36];
37
38/// A helper function to trim a stacktrace.
39pub fn trim_stacktrace<F>(stacktrace: &mut Stacktrace, f: F)
40where
41    F: Fn(&Frame, &Stacktrace) -> bool,
42{
43    let known_cutoff = stacktrace
44        .frames
45        .iter()
46        .rev()
47        .position(|frame| match frame.function {
48            Some(ref func) => is_well_known_border_frame(func) || f(frame, stacktrace),
49            None => false,
50        });
51
52    if let Some(cutoff) = known_cutoff {
53        let trunc = stacktrace.frames.len() - cutoff - 1;
54        stacktrace.frames.truncate(trunc);
55    }
56}
57
58/// Checks if a function is from a module that shall be considered not in-app by default
59pub fn is_well_known_not_in_app(func: &str) -> bool {
60    WELL_KNOWN_NOT_IN_APP
61        .iter()
62        .any(|m| function_starts_with(func, m))
63}
64
65/// Checks if a function is a well-known border frame
66fn is_well_known_border_frame(func: &str) -> bool {
67    WELL_KNOWN_BORDER_FRAMES
68        .iter()
69        .any(|m| function_starts_with(func, m))
70}