-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1113 from appsignal/hanami-refactor
Update Hanami to use Rack middleware
- Loading branch information
Showing
5 changed files
with
154 additions
and
108 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changesets/report-response_status-tag-and-metric-for-hanami-requests.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
bump: patch | ||
type: add | ||
--- | ||
|
||
Improve instrumentation of Hanami requests by making sure the transaction is always closed. | ||
It will also report a `response_status` tag and metric for Hanami requests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Rack | ||
# @api private | ||
class HanamiMiddleware < AbstractMiddleware | ||
def initialize(app, options = {}) | ||
options[:request_class] ||= ::Hanami::Action::Request | ||
options[:params_method] ||= :params | ||
options[:instrument_span_name] ||= "process_action.hanami" | ||
super | ||
end | ||
|
||
private | ||
|
||
def params_for(request) | ||
super&.to_h | ||
end | ||
|
||
def request_for(env) | ||
params = ::Hanami::Action.params_class.new(env) | ||
@request_class.new( | ||
:env => env, | ||
:params => params, | ||
:sessions_enabled => true | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "appsignal/rack/hanami_middleware" | ||
|
||
if DependencyHelper.hanami2_present? | ||
describe Appsignal::Rack::HanamiMiddleware do | ||
let(:app) { double(:call => true) } | ||
let(:router_params) { { "param1" => "value1", "param2" => "value2" } } | ||
let(:env) do | ||
Rack::MockRequest.env_for( | ||
"/some/path", | ||
"router.params" => router_params | ||
) | ||
end | ||
let(:middleware) { Appsignal::Rack::HanamiMiddleware.new(app, {}) } | ||
|
||
before(:context) { start_agent } | ||
around { |example| keep_transactions { example.run } } | ||
|
||
def make_request(env) | ||
middleware.call(env) | ||
end | ||
|
||
context "with params" do | ||
it "sets request parameters on the transaction" do | ||
make_request(env) | ||
|
||
expect(last_transaction.to_h).to include( | ||
"sample_data" => hash_including( | ||
"params" => { "param1" => "value1", "param2" => "value2" } | ||
) | ||
) | ||
end | ||
end | ||
|
||
it "reports a process_action.hanami event" do | ||
make_request(env) | ||
|
||
expect(last_transaction.to_h).to include( | ||
"events" => [ | ||
hash_including( | ||
"body" => "", | ||
"body_format" => Appsignal::EventFormatter::DEFAULT, | ||
"count" => 1, | ||
"name" => "process_action.hanami", | ||
"title" => "" | ||
) | ||
] | ||
) | ||
end | ||
end | ||
end |