Skip to content

Commit

Permalink
Check Rails.backtrace_cleaner method exists (#734)
Browse files Browse the repository at this point in the history
Check if the Rails.backtrace_cleaner method exists before calling it.
We've got a report from a user that the method call fails with a
NoMethodError in a Sinatra app using the activesupport and actionmailer
gems version 5.2.0.

```
NoMethodError - undefined method `backtrace_cleaner' for Rails:Module:
/gems/appsignal-3.0.6/lib/appsignal/transaction.rb:530:in `cleaned_backtrace'
```

I was unable to reproduce in the test setups, but I can see a scenario
where this could fail. To make sure we don't cause this kind of error in
such a scenario, check if the Rails module listens to the
`backtrace_cleaner` method before calling it.

appsignal/test-setups@d484b36
  • Loading branch information
tombruijn authored Jun 23, 2021
1 parent 1ab5012 commit 44dd4bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changesets/check-rails-backtrace_cleaner-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bump: "patch"
---

Check Rails.backtrace_cleaner method before calling the method. This prevents a NoMethodError from being raised in some edge cases.
2 changes: 1 addition & 1 deletion lib/appsignal/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def sanitized_tags
end

def cleaned_backtrace(backtrace)
if defined?(::Rails) && backtrace
if defined?(::Rails) && Rails.respond_to?(:backtrace_cleaner) && backtrace
::Rails.backtrace_cleaner.clean(backtrace, nil)
else
backtrace
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,13 @@ def session_exists?(_env)
expect(subject).to eq ["line 1", "line 2"]
end

context "with Rails module but without backtrace_cleaner method" do
it "returns the backtrace uncleaned" do
stub_const("Rails", Module.new)
expect(subject).to eq ["line 1", "line 2"]
end
end

if rails_present?
context "with rails" do
it "cleans the backtrace with the Rails backtrace cleaner" do
Expand Down

0 comments on commit 44dd4bd

Please sign in to comment.