-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Rack instrumentation middleware
Let's no longer the GenericInstrumentation middleware with its default action name set to `unknown`. This new InstrumentationMiddleware will not report any action name, only report an instrumentation event. The action name can be set by the app using the `Appsignal.set_action` helper from now on in this middleware. This will require us to update the Rack docs page to replace GenericInstrumentation with InstrumentationMiddleware.
- Loading branch information
Showing
5 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
.changesets/add-new-recommended-rack-instrumentation-middleware.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,18 @@ | ||
--- | ||
bump: minor | ||
type: add | ||
--- | ||
|
||
Add our new recommended Rack instrumentation middleware. If an app is using the `Appsignal::Rack::GenericInstrumentation` middleware, please update it to use `Appsignal::Rack::InstrumentationMiddleware` instead. | ||
|
||
This new middleware will not report all requests under the "unknown" action if no action name is set. To set an action name, call the `Appsignal.set_action` helper from the app. | ||
|
||
```ruby | ||
# config.ru | ||
|
||
# Setup AppSignal | ||
|
||
use Appsignal::Rack::InstrumentationMiddleware | ||
|
||
# Run app | ||
``` |
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
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Rack | ||
# @api public | ||
class InstrumentationMiddleware < AbstractMiddleware | ||
def initialize(app, options = {}) | ||
options[:instrument_span_name] ||= "process_request_middleware.rack" | ||
super | ||
end | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
spec/lib/appsignal/rack/instrumentation_middleware_spec.rb
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,38 @@ | ||
describe Appsignal::Rack::InstrumentationMiddleware do | ||
let(:app) { DummyApp.new } | ||
let(:env) { Rack::MockRequest.env_for("/some/path") } | ||
let(:middleware) { described_class.new(app, {}) } | ||
|
||
before { start_agent } | ||
around { |example| keep_transactions { example.run } } | ||
|
||
def make_request(env) | ||
middleware.call(env) | ||
end | ||
|
||
context "without an exception" do | ||
it "reports a process_request_middleware.rack event" do | ||
make_request(env) | ||
|
||
expect(last_transaction).to include_event("name" => "process_request_middleware.rack") | ||
end | ||
end | ||
|
||
context "with custom action name" do | ||
let(:app) { DummyApp.new { |_env| Appsignal.set_action("MyAction") } } | ||
|
||
it "reports the custom action name" do | ||
make_request(env) | ||
|
||
expect(last_transaction).to have_action("MyAction") | ||
end | ||
end | ||
|
||
context "without action name metadata" do | ||
it "reports no action name" do | ||
make_request(env) | ||
|
||
expect(last_transaction).to_not have_action | ||
end | ||
end | ||
end |