Skip to content

Commit

Permalink
Report Rails runner errors (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombruijn authored Jul 24, 2024
1 parent 46f23f1 commit 4d6add1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changesets/report-errors-from-rails-runners.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: minor
type: add
---

Report errors from Rails runners. When a Rails runner reports an unhandled error, it will now report the error in the "runner" namespace.
9 changes: 7 additions & 2 deletions lib/appsignal/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ def self.initialize_error_reporter
class RailsErrorReporterSubscriber
class << self
def report(error, handled:, severity:, context: {}, source: nil)
is_rails_runner = source == "application.runner.railties"
# Ignore not handled errors. They are reraised by the error reporter
# and are caught and recorded by our Rails middleware.
return unless handled
return if !handled && !is_rails_runner

Appsignal.send_error(error) do |transaction|
namespace, action_name, path, method, params, tags, custom_data =
context_for(context.dup)
transaction.set_namespace(namespace) if namespace
if namespace
transaction.set_namespace(namespace)
elsif is_rails_runner
transaction.set_namespace("runner")
end
transaction.set_action(action_name) if action_name
transaction.set_metadata("path", path)
transaction.set_metadata("method", method)
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/appsignal/integrations/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ def subscribe(subscriber)

expect(created_transactions).to be_empty
end

if DependencyHelper.rails7_1_present?
it "reports the error if the source is the Rails runner" do
expect do
with_rails_error_reporter do
expect do
Rails.error.record(:source => "application.runner.railties") do
raise ExampleStandardError, "error message"
end
end.to raise_error(ExampleStandardError, "error message")
end
end.to change { created_transactions.count }.by(1)

transaction = last_transaction
expect(transaction).to have_namespace("runner")
expect(transaction).to_not have_action
expect(transaction).to have_error("ExampleStandardError", "error message")
expect(transaction).to include_tags("source" => "application.runner.railties")
end
end
end

context "when error is handled (not reraised)" do
Expand Down
4 changes: 4 additions & 0 deletions spec/support/helpers/dependency_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def rails7_present?
rails_present? && rails_version >= Gem::Version.new("7.0.0")
end

def rails7_1_present?
rails_present? && rails_version >= Gem::Version.new("7.1.0")
end

def active_job_wraps_args?
rails7_present? || (ruby_3_1_or_newer? && rails6_1_present? && !rails6_1_5_present?)
end
Expand Down

0 comments on commit 4d6add1

Please sign in to comment.