Skip to content

Commit

Permalink
Support nested webmachine apps
Browse files Browse the repository at this point in the history
Apply the same logic as we have in the AbstractMiddleware for webmachine
apps. When a parent transaction is active, use that and don't create a
new one. More importantly, don't close the active transaction.

The webmachine instrumentation can't use the AbstractMiddleware, or any
middleware, as this discouraged by the webmachine project. In practice I
don't see this happening, but if someone happens to add an AppSignal
EventHandler to the middleware stack in `config.ru`, we now support it.

Part of #329
  • Loading branch information
tombruijn committed Jul 2, 2024
1 parent 1d3aaeb commit 243d20a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changesets/support-nested-webmachine-apps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: add
---

Support nested webmachine apps. If webmachine apps are nested in other AppSignal instrumentation it will now report the webmachine instrumentation as part of the parent transaction, reporting more runtime of the request.
24 changes: 15 additions & 9 deletions lib/appsignal/integrations/webmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ module Integrations
# @api private
module WebmachineIntegration
def run
transaction = Appsignal::Transaction.create(
SecureRandom.uuid,
Appsignal::Transaction::HTTP_REQUEST,
request,
:params_method => :query
)

transaction.set_action_if_nil("#{resource.class.name}##{request.method}")
has_parent_transaction = Appsignal::Transaction.current?
transaction =
if has_parent_transaction
Appsignal::Transaction.current
else
Appsignal::Transaction.create(
SecureRandom.uuid,
Appsignal::Transaction::HTTP_REQUEST,
request
)
end

Appsignal.instrument("process_action.webmachine") do
super
end
ensure
transaction.set_action_if_nil("#{resource.class.name}##{request.method}")
transaction.set_params_if_nil(request.query)

Appsignal::Transaction.complete_current!
Appsignal::Transaction.complete_current! unless has_parent_transaction
end

private
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/appsignal/integrations/webmachine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ def to_html
expect(last_transaction).to be_completed
expect(current_transaction?).to be_falsy
end

context "with parent transaction" do
let(:transaction) { http_request_transaction }
before { set_current_transaction(transaction) }

it "sets the action" do
fsm.run
expect(last_transaction).to have_action("MyResource#GET")
end

it "sets the params" do
fsm.run
last_transaction._sample
expect(last_transaction).to include_params("param1" => "value1", "param2" => "value2")
end

it "does not close the transaction" do
expect(last_transaction).to_not be_completed
end
end
end

describe "#handle_exceptions" do
Expand Down

0 comments on commit 243d20a

Please sign in to comment.