Skip to content

Commit

Permalink
Report request response status for Rails apps (#1090)
Browse files Browse the repository at this point in the history
We can now report the response status as a `response_status` tag and
metric. Previously, due to the position of the Rails middleware, this
was unreliable and wouldn't always report the real status code if other
middleware in the stack that are run after ours returned another status.

This is part of issue #183, but only implements it now for Rails apps
and apps using the `Appsignal::Rack::EventHandler` manually. We'll need
to update other instrumentations for Rack, Sinatra, Padrino, Grape, etc.
to use this new EventHandler, so they can also benefit from this new
response status tag and metric.
  • Loading branch information
tombruijn authored Jun 17, 2024
1 parent 9a5baf4 commit 5459a02
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changesets/report-response-status-for-rails-requests.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 8 additions & 1 deletion lib/appsignal/rack/event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/appsignal/rack/event_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5459a02

Please sign in to comment.