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

Rack instrumentation: support and test with v3+ #1600

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions lib/new_relic/agent/instrumentation/rack/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ def to_app_with_newrelic_deferred_dependency_detection
if ::NewRelic::Agent::Instrumentation::RackHelpers.middleware_instrumentation_enabled?
::NewRelic::Agent.logger.info("Installing #{builder_class} middleware instrumentation")

def run_with_newrelic(app, *args)
run_with_tracing(app) { |wrapped_app| run_without_newrelic(wrapped_app, *args) }
def run_with_newrelic(app = nil, *args, &block)
app_or_block = app || block
run_with_tracing(app_or_block) do |wrapped|
# Rack::Builder#run for Rack v3+ supports a block, and does not
# support args. Whether a block or an app is provided, that
# callable object will be wrapped into a MiddlewareProxy
# instance. That proxy instance must then be passed to
# run_without_newrelic as the app argument.
block ? run_without_newrelic(wrapped, &nil) : run_without_newrelic(wrapped, *args)
end
end

alias_method(:run_without_newrelic, :run)
Expand Down
11 changes: 9 additions & 2 deletions lib/new_relic/agent/instrumentation/rack/prepend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ def to_app
with_deferred_dependency_detection { super }
end

def run(app, *args)
run_with_tracing(app) { |wrapped_app| super(wrapped_app, *args) }
def run(app = nil, *args, &block)
app_or_block = app || block
run_with_tracing(app_or_block) do |wrapped|
# Rack::Builder#run for Rack v3+ supports a block, and does not
# support args. Whether a block or an app is provided, that callable
# object will be wrapped into a MiddlewareProxy instance. That
# proxy instance must then be passed to super as the app argument.
block ? super(wrapped, &nil) : super(wrapped, *args)
end
end

def use(middleware_class, *args, &blk)
Expand Down
6 changes: 4 additions & 2 deletions test/multiverse/suites/rack/Envfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ PUMA_VERSIONS = [
]

def gem_list(puma_version = nil)
rack_version = RUBY_VERSION >= '2.3.0' ? '~> 2.2.3' : '~> 2.1.4'
# try to test with the latest Rack version by not specifying a version
# constraint at all, but provide one for older Rubies not supporting Rack v3+
rack_version = ", '~> 2.1.4'" if RUBY_VERSION < '2.3.0'
<<-RB
gem 'puma'#{puma_version}
gem 'rack', '#{rack_version}'
gem 'rack'#{rack_version}
gem 'rack-test'
#{ruby3_gem_webrick}
RB
Expand Down
15 changes: 15 additions & 0 deletions test/multiverse/suites/rack/rack_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ def instance.middleware_instrumentation_enabled?; false; end
end
end

# Rack::Builder#run supports blocks as of v3.0.0
def test_run_with_tracing_with_the_original_run_method_is_given_a_block
skip 'Requires Rack v3+' unless defined?(Rack::RELEASE) && Rack::RELEASE >= '3.0.0'

builder = Rack::Builder.new
payload = [200, {}, ['Hello, Mr. Toad!']]
::NewRelic::Agent::Instrumentation::RackHelpers.stub :middleware_instrumentation_enabled?, true do
builder.run { |_env| payload }
end
run = builder.instance_variable_get(:@run)

assert_kind_of NewRelic::Agent::Instrumentation::MiddlewareProxy, run
assert_equal payload, run.call({})
end

def test_use_with_tracing
skip 'Requires MiniTest v5+' unless MiniTest::Unit::VERSION > '5.0'

Expand Down