Skip to content

Commit

Permalink
Report request response status for Rails apps
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 committed Jun 3, 2024
1 parent 52fc3b5 commit 211ab2f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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.
8 changes: 8 additions & 0 deletions lib/appsignal/rack/event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ def on_finish(request, _response)

transaction.finish_event("process_request.rack", "", "")
transaction.set_action_if_nil("#{request.request_method} #{request.path}")
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
Appsignal::Transaction.complete_current!
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 @@ -160,6 +160,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 211ab2f

Please sign in to comment.