Skip to content

Commit

Permalink
Add new Rack instrumentation middleware
Browse files Browse the repository at this point in the history
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
tombruijn committed Jul 2, 2024
1 parent 773ac31 commit f259678
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .changesets/add-new-recommended-rack-instrumentation-middleware.md
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
```
1 change: 1 addition & 0 deletions lib/appsignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def const_missing(name)
require "appsignal/marker"
require "appsignal/garbage_collection"
require "appsignal/rack/abstract_middleware"
require "appsignal/rack/instrumentation_middleware"
require "appsignal/rack/generic_instrumentation"
require "appsignal/rack/event_handler"
require "appsignal/integrations/railtie" if defined?(::Rails)
Expand Down
4 changes: 2 additions & 2 deletions lib/appsignal/rack/abstract_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def call(env)
# don't report any exceptions here, the top instrumentation middleware
# will be the one reporting the exception.
#
# Either another {GenericInstrumentation} or {EventHandler} is higher in
# the stack and will report the exception and complete the transaction.
# Either another {AbstractMiddleware} or {EventHandler} is higher in the
# stack and will report the exception and complete the transaction.
#
# @see {#instrument_app_call_with_exception_handling}
def instrument_app_call(env)
Expand Down
13 changes: 13 additions & 0 deletions lib/appsignal/rack/instrumentation_middleware.rb
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 spec/lib/appsignal/rack/instrumentation_middleware_spec.rb
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

0 comments on commit f259678

Please sign in to comment.