Skip to content

Commit

Permalink
Add params, path and method to errors reported via Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater-thatch committed Mar 12, 2024
1 parent b75109f commit fe3e1ff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/appsignal/integrations/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ def report(error, handled:, severity:, context: {}, source: nil)
return unless handled

Appsignal.send_error(error) do |transaction|
namespace, action_name, tags, custom_data = context_for(context.dup)
namespace, action_name, path, method, params, tags, custom_data =
context_for(context.dup)
transaction.set_namespace(namespace) if namespace
transaction.set_action(action_name) if action_name
transaction.set_metadata("path", path)
transaction.set_metadata("method", method)
transaction.params = params
transaction.set_sample_data("custom_data", custom_data) if custom_data

tags[:severity] = severity
Expand All @@ -81,9 +85,15 @@ def context_for(context)
# Fetch the namespace and action name based on the Rails execution
# context.
controller = context.delete(:controller)
path = nil
method = nil
params = nil
if controller
namespace = Appsignal::Transaction::HTTP_REQUEST
action_name = "#{controller.class.name}##{controller.action_name}"
path = controller.request.path
method = controller.request.method
params = controller.request.filtered_parameters
end
# ActiveJob transaction naming relies on the current AppSignal
# transaction namespace and action name copying done after this.
Expand Down Expand Up @@ -115,7 +125,7 @@ def context_for(context)
end
tags.merge!(context)

[namespace, action_name, tags, custom_data]
[namespace, action_name, path, method, params, tags, custom_data]
end
end
end
Expand Down
29 changes: 27 additions & 2 deletions spec/lib/appsignal/integrations/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,28 @@ def subscribe(subscriber)
end

context "when no transaction is active" do
class ExampleRailsRequestMock
def path
"path"
end

def method
"GET"
end

def filtered_parameters
{ :user_id => 123, :password => "[FILTERED]" }
end
end

class ExampleRailsControllerMock
def action_name
"index"
end

def request
@request ||= ExampleRailsRequestMock.new
end
end

class ExampleRailsJobMock
Expand All @@ -275,7 +293,7 @@ def arguments
clear_current_transaction!
end

it "fetches the action from the controller in the context" do
it "fetches the action, path and method from the controller in the context" do
# The controller key is set by Rails when raised in a controller
given_context = { :controller => ExampleRailsControllerMock.new }
with_rails_error_reporter do
Expand All @@ -285,7 +303,14 @@ def arguments
transaction = last_transaction
transaction_hash = transaction.to_h
expect(transaction_hash).to include(
"action" => "ExampleRailsControllerMock#index"
"action" => "ExampleRailsControllerMock#index",
"metadata" => hash_including(
"path" => "path",
"method" => "GET"
),
"sample_data" => hash_including(
"params" => { "user_id" => 123, "password" => "[FILTERED]" }
)
)
end

Expand Down

0 comments on commit fe3e1ff

Please sign in to comment.