Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rack instrumentation middleware #1139

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: deprecate
---

Deprecate the `Appsignal::Rack::GenericInstrumentation` middleware. Use the `Appsignal::Rack::InstrumentationMiddleware` middleware instead. See changelog entry about the `InstrumentationMiddleware`.
3 changes: 2 additions & 1 deletion lib/appsignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ def const_missing(name)
require "appsignal/probes"
require "appsignal/marker"
require "appsignal/garbage_collection"
require "appsignal/rack"
require "appsignal/rack/abstract_middleware"
require "appsignal/rack/generic_instrumentation"
require "appsignal/rack/instrumentation_middleware"
require "appsignal/rack/event_handler"
require "appsignal/integrations/railtie" if defined?(::Rails)
require "appsignal/transaction"
Expand Down
26 changes: 26 additions & 0 deletions lib/appsignal/rack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Appsignal
# @api private
module Rack
# Alias constants that have moved with a warning message that points to the
# place to update the reference.
def self.const_missing(name)
case name
when :GenericInstrumentation
require "appsignal/rack/generic_instrumentation"

callers = caller
Appsignal::Utils::StdoutAndLoggerMessage.warning \
"The constant Appsignal::Rack::GenericInstrumentation has been deprecated. " \
"Please update the constant name to " \
"Appsignal::Rack::InstrumentationMiddleware in the following file to " \
"remove this message.\n#{callers.first}"
tombruijn marked this conversation as resolved.
Show resolved Hide resolved
# Return the alias so it can't ever get stuck in a recursive loop
Appsignal::Rack::GenericInstrumentationAlias
else
super
end
end
end
end
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
7 changes: 4 additions & 3 deletions lib/appsignal/rack/generic_instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

require "rack"

module Appsignal
# @api private
module Rack
# @api private
class GenericInstrumentation < AbstractMiddleware
def initialize(app, options = {})
options[:instrument_span_name] ||= "process_action.generic"
Expand All @@ -16,5 +14,8 @@ def add_transaction_metadata_after(transaction, request)
transaction.set_action_if_nil("unknown")
end
end

# @api private
class GenericInstrumentationAlias < GenericInstrumentation; end
end
end
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
2 changes: 0 additions & 2 deletions lib/appsignal/rack/rails_instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "rack"

module Appsignal
module Rack
# @api private
Expand Down
2 changes: 0 additions & 2 deletions lib/appsignal/rack/sinatra_instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "rack"

module Appsignal
module Rack
# Stub old middleware. Prevents Sinatra middleware being loaded twice.
Expand Down
87 changes: 70 additions & 17 deletions spec/lib/appsignal/rack/generic_instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,81 @@
describe Appsignal::Rack::GenericInstrumentation do
let(:app) { double(:call => true) }
let(:env) { Rack::MockRequest.env_for("/some/path") }
let(:middleware) { Appsignal::Rack::GenericInstrumentation.new(app, {}) }
describe "Appsignal::Rack::GenericInstrumentation" do
describe "Appsignal::Rack::GenericInstrumentation constant" do
let(:err_stream) { std_stream }
let(:stderr) { err_stream.read }
before do
if Appsignal::Rack.const_defined?(:GenericInstrumentation)
hide_const "Appsignal::Rack::GenericInstrumentation"
end
end

it "returns the Rack::GenericInstrumentation constant" do
silence do
expect(Appsignal::Rack::GenericInstrumentation)
.to be(Appsignal::Rack::GenericInstrumentationAlias)
end
end

before(:context) { start_agent }
around { |example| keep_transactions { example.run } }
it "prints a deprecation warning to STDERR" do
capture_std_streams(std_stream, err_stream) do
Appsignal::Rack::GenericInstrumentation
end

def make_request(env)
middleware.call(env)
end
expect(stderr).to include(
"appsignal WARNING: The constant Appsignal::Rack::GenericInstrumentation " \
"has been deprecated."
)
end

context "without an exception" do
it "reports a process_action.generic event" do
make_request(env)
it "logs a warning" do
logs =
capture_logs do
silence do
Appsignal::Rack::GenericInstrumentation
end
end

expect(last_transaction).to include_event("name" => "process_action.generic")
expect(logs).to contains_log(
:warn,
"The constant Appsignal::Rack::GenericInstrumentation has been deprecated."
)
end
end

context "without action name metadata" do
it "reports 'unknown' as the action name" do
make_request(env)
describe "middleware" do
let(:app) { double(:call => true) }
let(:env) { Rack::MockRequest.env_for("/some/path") }
let(:middleware) { Appsignal::Rack::GenericInstrumentation.new(app, {}) }

before(:context) { 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_action.generic event" do
make_request(env)

expect(last_transaction).to include_event("name" => "process_action.generic")
end
end

context "with action name env" do
it "reports the appsignal.action env as the action name" do
env["appsignal.action"] = "MyAction"
make_request(env)

expect(last_transaction).to have_action("MyAction")
end
end

context "without action name metadata" do
it "reports 'unknown' as the action name" do
make_request(env)

expect(last_transaction).to have_action("unknown")
expect(last_transaction).to have_action("unknown")
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/appsignal/rack/grape_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:err_stream) { std_stream }
let(:stderr) { err_stream.read }

it "returns the Probes constant calling the Minutely constant" do
it "returns the Rack::GrapeMiddleware constant calling the Grape::Middleware constant" do
silence { expect(Appsignal::Grape::Middleware).to be(Appsignal::Rack::GrapeMiddleware) }
end

Expand Down
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
Loading