Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(backtrace): add entries and extra logic for in-app detection #756

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sentry-backtrace/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use backtrace::Backtrace;
use sentry_core::ClientOptions;

use crate::trim::{is_sys_function, trim_stacktrace};
use crate::trim::{is_well_known_not_in_app, trim_stacktrace};
use crate::utils::{
demangle_symbol, filename, function_starts_with, parse_crate_name, strip_symbol,
};
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn process_event_stacktrace(stacktrace: &mut Stacktrace, options: &ClientOpt
continue;
}

if is_sys_function(func_name) {
if is_well_known_not_in_app(func_name) {
frame.in_app = Some(false);
}
}
Expand Down
18 changes: 11 additions & 7 deletions sentry-backtrace/src/trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use sentry_core::protocol::{Frame, Stacktrace};

use crate::utils::function_starts_with;

const WELL_KNOWN_SYS_MODULES: &[&str] = &[
const WELL_KNOWN_NOT_IN_APP: &[&str] = &[
// standard library and sentry crates
"std::",
"core::",
"alloc::",
Expand All @@ -13,11 +14,14 @@ const WELL_KNOWN_SYS_MODULES: &[&str] = &[
// these are not modules but things like __rust_maybe_catch_panic
"__rust_",
"___rust_",
"rust_begin_unwind",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always the top frame for panics and ends up in the high level issue description, I would rather have the first in-app frame there
Unless it's intended that we have rust_begin_unwind there?

// these are well-known library frames
"anyhow::",
"log::",
"tokio::",
"tracing_core::",
"futures_core::",
"futures_util::",
Comment on lines +23 to +24
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not add too specific stuff but this seems common enough to be here

];

const WELL_KNOWN_BORDER_FRAMES: &[&str] = &[
Expand All @@ -39,7 +43,7 @@ where
.iter()
.rev()
.position(|frame| match frame.function {
Some(ref func) => is_well_known(func) || f(frame, stacktrace),
Some(ref func) => is_well_known_border_frame(func) || f(frame, stacktrace),
None => false,
});

Expand All @@ -49,15 +53,15 @@ where
}
}

/// Checks if a function is considered to be not in-app
pub fn is_sys_function(func: &str) -> bool {
WELL_KNOWN_SYS_MODULES
/// Checks if a function is from a module that shall be considered not in-app by default
pub fn is_well_known_not_in_app(func: &str) -> bool {
WELL_KNOWN_NOT_IN_APP
.iter()
.any(|m| function_starts_with(func, m))
}

/// Checks if a function is a well-known system function
fn is_well_known(func: &str) -> bool {
/// Checks if a function is a well-known border frame
fn is_well_known_border_frame(func: &str) -> bool {
WELL_KNOWN_BORDER_FRAMES
.iter()
.any(|m| function_starts_with(func, m))
Expand Down
10 changes: 8 additions & 2 deletions sentry-backtrace/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ pub fn demangle_symbol(s: &str) -> String {
///
/// In trait implementations, the original type name is wrapped in "_< ... >" and colons are
/// replaced with dots. This function accounts for differences while checking.
/// The `<F as ` pattern is a special case that can often be observed in frames involving futures-rs traits.
pub fn function_starts_with(mut func_name: &str, mut pattern: &str) -> bool {
if pattern.starts_with('<') {
while pattern.starts_with('<') {
pattern = &pattern[1..];

if func_name.starts_with('<') {
if func_name.starts_with("<F as ") {
func_name = &func_name[6..];
} else if func_name.starts_with('<') {
func_name = &func_name[1..];
} else if func_name.starts_with("_<") {
func_name = &func_name[2..];
Expand All @@ -111,7 +114,10 @@ pub fn function_starts_with(mut func_name: &str, mut pattern: &str) -> bool {
}
}
} else {
func_name = func_name.trim_start_matches('<').trim_start_matches("_<");
func_name = func_name
.trim_start_matches("<F as ")
Copy link
Member Author

@lcian lcian Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific case that can be commonly observed in raw frames related to futures_core, futures_util, axum, hyper, and others
It's specific for those cases as in general we will see the form <X as Y> for trait impls but I don't see any downside with adding this.

.trim_start_matches('<')
.trim_start_matches("_<");
}

if !func_name.is_char_boundary(pattern.len()) {
Expand Down