diff --git a/.changesets/report-response-status-for-rails-requests.md b/.changesets/report-response-status-for-rails-requests.md new file mode 100644 index 000000000..ecb46c9cc --- /dev/null +++ b/.changesets/report-response-status-for-rails-requests.md @@ -0,0 +1,8 @@ +--- +bump: patch +type: add +--- + +Report the response status for Rails requests as the `response_status` tag on samples, e.g. 200, 301, 500. This tag is visible on the sample detail page. + +The response status is also reported as the `response_status` metric. diff --git a/lib/appsignal/rack/event_handler.rb b/lib/appsignal/rack/event_handler.rb index f3b97a99d..5ed08d549 100644 --- a/lib/appsignal/rack/event_handler.rb +++ b/lib/appsignal/rack/event_handler.rb @@ -55,13 +55,20 @@ def on_error(request, _response, error) end end - def on_finish(request, _response) + def on_finish(request, response) self.class.safe_execution("Appsignal::Rack::EventHandler#on_finish") do transaction = request.env[APPSIGNAL_TRANSACTION] return unless transaction transaction.finish_event("process_request.rack", "", "") + transaction.set_tags(:response_status => response.status) transaction.set_http_or_background_queue_start + Appsignal.increment_counter( + :response_status, + 1, + :status => response.status, + :namespace => format_namespace(transaction.namespace) + ) # Make sure the current transaction is always closed when the request # is finished diff --git a/spec/lib/appsignal/rack/event_handler_spec.rb b/spec/lib/appsignal/rack/event_handler_spec.rb index cf125d388..beb42627c 100644 --- a/spec/lib/appsignal/rack/event_handler_spec.rb +++ b/spec/lib/appsignal/rack/event_handler_spec.rb @@ -183,6 +183,25 @@ def on_finish ) end + it "sets the response status as a tag" do + on_start + on_finish + + expect(last_transaction.to_h).to include( + "sample_data" => hash_including( + "tags" => { "response_status" => 200 } + ) + ) + end + + it "increments the response status counter for response status" do + expect(Appsignal).to receive(:increment_counter) + .with(:response_status, 1, :status => 200, :namespace => :web) + + on_start + on_finish + end + it "logs an error in case of an error" do expect(Appsignal::Transaction) .to receive(:complete_current!).and_raise(ExampleStandardError, "oh no")