Skip to content

Commit 0f0666c

Browse files
authored
Fix sentry-rails' backtrace cleaner issues (#2475)
* 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.
1 parent f225138 commit 0f0666c

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
- Fix `RescuedExceptionInterceptor` to handle an empty configuration [#2428](https://github.com/getsentry/sentry-ruby/pull/2428)
2020
- Add mutex sync to `SessionFlusher` aggregates [#2469](https://github.com/getsentry/sentry-ruby/pull/2469)
2121
- Fixes [#2468](https://github.com/getsentry/sentry-ruby/issues/2468)
22+
- Fix sentry-rails' backtrace cleaner issues ([#2475](https://github.com/getsentry/sentry-ruby/pull/2475))
23+
- Fixes [#2472](https://github.com/getsentry/sentry-ruby/issues/2472)
2224

2325
## 5.21.0
2426

sentry-rails/lib/sentry/rails/backtrace_cleaner.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner
1111

1212
def initialize
1313
super
14-
# we don't want any default silencers because they're too aggressive
14+
# We don't want any default silencers because they're too aggressive
1515
remove_silencers!
16+
# We don't want any default filters because Rails 7.2 starts shortening the paths. See #2472
17+
remove_filters!
1618

17-
@root = "#{Sentry.configuration.project_root}/"
18-
add_filter do |line|
19-
line.start_with?(@root) ? line.from(@root.size) : line
20-
end
2119
add_filter do |line|
2220
if line =~ RENDER_TEMPLATE_PATTERN
2321
line.sub(RENDER_TEMPLATE_PATTERN, "")

sentry-rails/spec/sentry/rails/event_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
it 'marks in_app correctly' do
3333
frames = hash[:exception][:values][0][:stacktrace][:frames]
3434
expect(frames[0][:filename]).to eq("test/some/other/path")
35+
expect(frames[0][:abs_path]).to eq("test/some/other/path")
3536
expect(frames[0][:in_app]).to eq(true)
3637
expect(frames[1][:filename]).to eq("/app/some/other/path")
3738
expect(frames[1][:in_app]).to eq(false)
@@ -41,7 +42,7 @@
4142
expect(frames[3][:in_app]).to eq(true)
4243
expect(frames[4][:filename]).to eq("vendor/bundle/some_gem.rb")
4344
expect(frames[4][:in_app]).to eq(false)
44-
expect(frames[5][:filename]).to eq("vendor/bundle/cache/other_gem.rb")
45+
expect(frames[5][:filename]).to eq("dummy/test_rails_app/vendor/bundle/cache/other_gem.rb")
4546
expect(frames[5][:in_app]).to eq(false)
4647
end
4748

@@ -50,6 +51,7 @@
5051
$LOAD_PATH << "#{Rails.root}/app/models"
5152
frames = hash[:exception][:values][0][:stacktrace][:frames]
5253
expect(frames[3][:filename]).to eq("app/models/user.rb")
54+
expect(frames[3][:abs_path]).to eq("#{Rails.root}/app/models/user.rb")
5355
$LOAD_PATH.delete("#{Rails.root}/app/models")
5456
end
5557
end
@@ -58,15 +60,17 @@
5860
it 'normalizes the filename using the load path' do
5961
$LOAD_PATH.push "vendor/bundle"
6062
frames = hash[:exception][:values][0][:stacktrace][:frames]
61-
expect(frames[5][:filename]).to eq("cache/other_gem.rb")
63+
expect(frames[5][:filename]).to eq("dummy/test_rails_app/vendor/bundle/cache/other_gem.rb")
64+
expect(frames[5][:abs_path]).to eq("#{Rails.root}/vendor/bundle/cache/other_gem.rb")
6265
$LOAD_PATH.pop
6366
end
6467
end
6568

6669
context "when a non-in_app path under project_root isn't on the load path" do
6770
it 'normalizes the filename using project_root' do
6871
frames = hash[:exception][:values][0][:stacktrace][:frames]
69-
expect(frames[5][:filename]).to eq("vendor/bundle/cache/other_gem.rb")
72+
expect(frames[5][:filename]).to eq("dummy/test_rails_app/vendor/bundle/cache/other_gem.rb")
73+
expect(frames[5][:abs_path]).to eq("#{Rails.root}/vendor/bundle/cache/other_gem.rb")
7074
end
7175
end
7276
end

sentry-rails/spec/sentry/rails_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ def capture_in_separate_process(exit_code:)
242242
expect(traces.dig(-1, "function")).to be_nil
243243
end
244244

245+
it "makes sure BacktraceCleaner gem cleanup doesn't affect context lines population" do
246+
get "/view_exception"
247+
248+
traces = event.dig("exception", "values", 0, "stacktrace", "frames")
249+
gem_frame = traces.find { |t| t["abs_path"].match(/actionview/) }
250+
expect(gem_frame["pre_context"]).not_to be_empty
251+
expect(gem_frame["post_context"]).not_to be_empty
252+
expect(gem_frame["context_line"]).not_to be_empty
253+
end
254+
245255
it "doesn't filters exception backtrace if backtrace_cleanup_callback is overridden" do
246256
make_basic_app do |config|
247257
config.backtrace_cleanup_callback = lambda { |backtrace| backtrace }

0 commit comments

Comments
 (0)