Skip to content

Commit

Permalink
Rails v7.1 driven 'rails' suite updates
Browse files Browse the repository at this point in the history
Update the multiverse 'rails' suite for Rails v7.1 compatibility
  • Loading branch information
fallwith committed Oct 10, 2023
1 parent 4c4cfd0 commit 824ef52
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/new_relic/control/frameworks/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module Frameworks
# Rails specific configuration, instrumentation, environment values,
# etc.
class Rails < NewRelic::Control::Frameworks::Ruby
INSTALLED_SINGLETON = NewRelic::Agent.config
INSTALLED = :@browser_monitoring_installed

def env
@env ||= (ENV['NEW_RELIC_ENV'] || RAILS_ENV.dup)
end
Expand Down Expand Up @@ -97,9 +100,9 @@ def install_agent_hooks(config)

def install_browser_monitoring(config)
@install_lock.synchronize do
return if defined?(@browser_monitoring_installed) && @browser_monitoring_installed
return if browser_agent_already_installed?

@browser_monitoring_installed = true
mark_browser_agent_as_installed
return if config.nil? || !config.respond_to?(:middleware) || !Agent.config[:'browser_monitoring.auto_instrument']

begin
Expand All @@ -112,6 +115,15 @@ def install_browser_monitoring(config)
end
end

def browser_agent_already_installed?
INSTALLED_SINGLETON.instance_variable_defined?(INSTALLED) &&
INSTALLED_SINGLETON.instance_variable_get(INSTALLED)
end

def mark_browser_agent_as_installed
INSTALLED_SINGLETON.instance_variable_set(INSTALLED, true)
end

def rails_version
@rails_version ||= Gem::Version.new(::Rails::VERSION::STRING)
end
Expand Down
8 changes: 8 additions & 0 deletions test/multiverse/suites/rails/action_cable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def initialize
@logger = Logger.new(StringIO.new)
end

# In Rails itself, `#config` is delegated via a stub.
# See https://github.com/rails/rails/commit/8fff6d609cec2d20972235d3c2cf7d004e2d6983
# But seeing as that stub is not distributed in the ActionCable gem, we
# use this workaround.
def config
Rails.application.config
end

def transmit(data)
@transmissions << data
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require './app'

if defined?(ActionController::Live)

class UndeadController < ApplicationController
RESPONSE_BODY = '<html><head></head><body>Brains!</body></html>'

Expand Down Expand Up @@ -44,6 +43,7 @@ def test_excludes_rum_instrumentation_when_streaming_with_action_controller_live
def test_excludes_rum_instrumentation_when_streaming_with_action_stream_true
get('/undead/brain_stream', env: {'HTTP_VERSION' => 'HTTP/1.1'})

assert_predicate(response, :ok?, 'Expected ActionController streaming response to be OK')
assert_includes(response.body, UndeadController::RESPONSE_BODY)
assert_not_includes(response.body, JS_LOADER)
end
Expand Down
12 changes: 11 additions & 1 deletion test/multiverse/suites/rails/activejob_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ def test_code_information_recorded_with_new_transaction
namespace: 'MyJob'}
segment = MiniTest::Mock.new
segment.expect(:code_information=, nil, [expected])
segment.expect(:finish, [])
segment.expect(:code_information=,
nil,
[{transaction_name: 'OtherTransaction/ActiveJob::Inline/MyJob/execute'}])
(NewRelic::Agent::Instrumentation::ActiveJobSubscriber::PAYLOAD_KEYS.size + 1).times do
segment.expect(:params, {}, [])
end
3.times do
segment.expect(:finish, [])
end
segment.expect(:record_scoped_metric=, nil, [false])
segment.expect(:notice_error, nil, [])
NewRelic::Agent::Tracer.stub(:start_segment, segment) do
MyJob.perform_later
end
Expand Down
1 change: 0 additions & 1 deletion test/multiverse/suites/rails/rails3_app/app_rails3_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# Tests should feel free to define their own Controllers locally, but if they
# need anything special at the Application level, put it here
if !defined?(MyApp)

ENV['NEW_RELIC_DISPATCHER'] = 'test'

class NamedMiddleware
Expand Down
4 changes: 2 additions & 2 deletions test/multiverse/suites/rails/view_instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class ViewInstrumentationTest < ActionDispatch::IntegrationTest
end
end

(ViewsController.action_methods - %w[raise_render collection_render haml_render]).each do |method|
(ViewsController.action_methods - %w[raise_render collection_render haml_render proc_render]).each do |method|
define_method("test_sanity_#{method}") do
get "/views/#{method}"

assert_equal 200, status
assert_equal 200, status, "Expected 200, got #{status} for /views/#{method}"
end

def test_should_allow_uncaught_exception_to_propagate
Expand Down

0 comments on commit 824ef52

Please sign in to comment.