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

Fix Source Code Extraction to Stack Traces (regression via rails) #2472

Closed
chodges15 opened this issue Nov 22, 2024 · 6 comments · Fixed by #2475
Closed

Fix Source Code Extraction to Stack Traces (regression via rails) #2472

chodges15 opened this issue Nov 22, 2024 · 6 comments · Fixed by #2475
Assignees

Comments

@chodges15
Copy link

Describe the idea
Currently, the Ruby Sentry SDK provides stack traces that include the file name, line number, and method name where an error occurred. However, it does not include the actual lines of source code. This feature is crucial for debugging, especially when dealing with third-party code, as it provides immediate context around the error without requiring developers to manually locate and open the files.

The feature request is to add functionality to the Ruby Sentry SDK to extract and include source code snippets in stack traces. This can be achieved by implementing a mechanism similar to the CodeExtractor class in the Bugsnag SDK, which reads the relevant files and attaches the surrounding lines of code to the stack trace.

Why do you think it's beneficial to most of the users

  1. Improved Debugging: Including source code snippets in stack traces allows developers to quickly understand the context of an error, making it easier to diagnose and fix issues.

  2. Third-Party Code: When errors occur in third-party libraries, developers may not be familiar with the code. Providing the surrounding lines of code helps them understand what might be going wrong without needing to dive into the third-party codebase.

  3. Efficiency: This feature saves time by reducing the need to manually locate and open files to see the relevant lines of code. It streamlines the debugging process and helps developers focus on solving the problem.

@ndmanvar
Copy link

minor clarification:
I believe we show context_lines/source_code for in-app frames; it seems like we aren't doing so for vendor-libraries (i.e, non-in-app-frames)
Image

Also, we may shown context_lines for all frames at some point...
See following event from v5.7.0 which shows that context-lines/source_code for all frames

@sl0thentr0py
Copy link
Member

we already support this since forever

@pre_context, @context_line, @post_context = \
linecache.get_file_context(abs_path, lineno, context_lines)

but it is true that some vendor frames are missing context lines in the final product, I will investigate why this is happening.

@sl0thentr0py
Copy link
Member

alright, not technically a regression on our end but at some point rails added this default backtrace gem path cleaner
https://github.com/rails/rails/blob/a8709e6ea26eca73a652af4fdd0a9f7db5352af4/activesupport/lib/active_support/backtrace_cleaner.rb#L118-L125

that causes our source code reading to fail since instead of paths like

/Users/neel/.asdf/installs/ruby/3.2.6/lib/ruby/gems/3.2.0/gems/actionpack-7.1.2/lib/abstract_controller/base.rb

we get paths like

activesupport (7.1.2) lib/active_support/callbacks.rb

I will think of the best way to fix this since technically this affects all grouping now if I suddenly remove that gem filter..

@sl0thentr0py
Copy link
Member

sl0thentr0py commented Nov 25, 2024

in the meantime, if they want to fix this, they can turn off the backtrace_cleaner with

Sentry.init do |config|
  # ...
  config.backtrace_cleanup_callback = ->(x) { x }
end

@sl0thentr0py sl0thentr0py changed the title Add Source Code Extraction to Stack Traces Fix Source Code Extraction to Stack Traces (regression via rails) Nov 25, 2024
@chodges15
Copy link
Author

Ah, this all makes sense, we'd figured this functionality was tablestakes for a Sentry SDK, glad you already support it and thanks for the clarification on the backtrace path cleaner regression. We'll give this a shot and report back.

@sl0thentr0py
Copy link
Member

fix is up at #2474, will probably take a week or so for review/release.

st0012 added a commit that referenced this issue Nov 27, 2024
In Rails 7.2, Rails's backtrace cleaner, which sentry-rails' backtrace
cleaner inherits from, starts shortening gem's paths in backtraces. This
will present sentry-ruby's `Sentry::Backtrace` from handling them correctly,
which will result in issues like #2472.

This commit avoids the issue by clearing the default filters that
Sentry's backtrace cleaner inherits.
st0012 added a commit that referenced this issue Nov 27, 2024
In Rails 7.2, Rails's backtrace cleaner, which sentry-rails' backtrace
cleaner inherits from, starts shortening gem's paths in backtraces. This
will prevent sentry-ruby's `Sentry::Backtrace` from handling them correctly,
which will result in issues like #2472.

This commit avoids the issue by clearing the default filters that
Sentry's backtrace cleaner inherits.
st0012 added a commit that referenced this issue Nov 30, 2024
In Rails 7.2, Rails's backtrace cleaner, which sentry-rails' backtrace
cleaner inherits from, starts shortening gem's paths in backtraces. This
will prevent sentry-ruby's `Sentry::Backtrace` from handling them correctly,
which will result in issues like #2472.

This commit avoids the issue by clearing the default filters that
Sentry's backtrace cleaner inherits.
sl0thentr0py pushed a commit that referenced this issue Dec 2, 2024
* Clear Rails' default backtrace filters from Sentry's backtrace cleaner

In Rails 7.2, Rails's backtrace cleaner, which sentry-rails' backtrace
cleaner inherits from, starts shortening gem's paths in backtraces. This
will prevent sentry-ruby's `Sentry::Backtrace` from handling them correctly,
which will result in issues like #2472.

This commit avoids the issue by clearing the default filters that
Sentry's backtrace cleaner inherits.

* Remove premature path-removal filter

This filter removes the project root part from the stacktrace, which prevents
sentry-ruby from retaining the correct absolute path of it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment