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

Enable defer_rails_initialization config by environment variable only #1790

Closed
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
1 change: 1 addition & 0 deletions .github/workflows/performance_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- main
schedule:
- cron: '0 9 * * 0'
workflow_dispatch:

jobs:
check_branch_changes:
Expand Down
10 changes: 0 additions & 10 deletions lib/new_relic/agent/configuration/default_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1865,16 +1865,6 @@ def self.enforce_fallback(allowed_values: nil, fallback: nil)
:allowed_from_server => false,
:description => 'Specify a custom host name for [display in the New Relic UI](/docs/apm/new-relic-apm/maintenance/add-rename-remove-hosts#display_name).'
},
# Rails
:'defer_rails_initialization' => {
:default => false,
:public => true,
:type => Boolean,
:allowed_from_server => false,
:description => 'This configuration option is currently unreachable. Please do not use. ' \
'If `true`, when the agent is in an application using Ruby on Rails, it will start after ' \
'config/initializers have run.'
},
# Rake
:'rake.tasks' => {
:default => [],
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/control/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def init_plugin(options = {})
# An artifact of earlier implementation, we put both #add_method_tracer and #trace_execution
# methods in the module methods.
# Rails applications load the next two lines before any other initializers are run
unless defined?(Rails::VERSION) && NewRelic::Agent.config[:defer_rails_initialization]
unless defined?(Rails::VERSION)
Module.send(:include, NewRelic::Agent::MethodTracer::ClassMethods)
Module.send(:include, NewRelic::Agent::MethodTracer)
end
Expand Down
18 changes: 6 additions & 12 deletions lib/newrelic_rpm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@
if defined?(Rails::VERSION)
module NewRelic
class Railtie < Rails::Railtie
if NewRelic::Agent.config[:defer_rails_initialization]
initializer "newrelic_rpm.include_method_tracers", before: :load_config_initializers do |app|
Module.send(:include, NewRelic::Agent::MethodTracer::ClassMethods)
Module.send(:include, NewRelic::Agent::MethodTracer)
end
initializer "newrelic_rpm.include_method_tracers", before: :load_config_initializers do |app|
Module.send(:include, NewRelic::Agent::MethodTracer::ClassMethods)
Module.send(:include, NewRelic::Agent::MethodTracer)
end

initializer "newrelic_rpm.start_plugin", after: :load_config_initializers do |app|
NewRelic::Control.instance.init_plugin(config: app.config)
end
else
initializer "newrelic_rpm.start_plugin", before: :load_config_initializers do |app|
NewRelic::Control.instance.init_plugin(config: app.config)
end
initializer "newrelic_rpm.start_plugin", after: :load_config_initializers do |app|
NewRelic::Control.instance.init_plugin(config: app.config)
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions newrelic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ common: &default_settings
# or port_path_or_id parameters to transaction or slow SQL traces.
# datastore_tracer.instance_reporting.enabled: true

# If true, when the agent is in an application using Ruby on Rails, it will start after
# config/initializers have run.
# defer_rails_initialization: false

# If true, disables Action Cable instrumentation.
# disable_action_cable_instrumentation: false

Expand Down
3 changes: 2 additions & 1 deletion test/environments/rails60/config/initializers/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ def sniff

add_method_tracer :sniff
end

puts Rails.application.config.active_record.timestamped_migrations
Rails.application.config.active_record.timestamped_migrations = false
puts Rails.application.config.active_record.timestamped_migrations
82 changes: 22 additions & 60 deletions test/new_relic/newrelic_rpm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,22 @@
require_relative '../test_helper'

class NewRelicRpmTest < Minitest::Test
RAILS_32_SKIP_MESSAGE = 'MySQL error for Rails 3.2 when we add an initializer to config/initializers'
# This test examines the behavior of an initializer when the agent is started in a Rails context
# The initializer used for these tests is defined in test/environments/*/config/inititalizers/test.rb

# We have documentation recommending customers call add_method_tracer
# in their initializers, let's make sure this works
def test_add_method_tracer_in_initializer_gets_traced_when_agent_initialized_before_config_initializers
skip unless defined?(Rails::VERSION)
skip 'MySQL error for Rails 3.2' if Rails::VERSION::MAJOR == 3

with_config(defer_rails_initialization: false) do
assert Bloodhound.newrelic_method_exists?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.newrelic_method_exists?'
assert Bloodhound.method_traced?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.method_traced?'
end
end

def test_add_method_tracer_in_initializer_gets_traced_when_agent_initialized_after_config_initializers
skip unless defined?(Rails::VERSION)
skip 'MySQL error for Rails 3.2 if we define an initializer in config/initializers' if Rails::VERSION::MAJOR == 3

with_config(defer_rails_initialization: true) do
assert Bloodhound.newrelic_method_exists?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.newrelic_method_exists?'
assert Bloodhound.method_traced?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.method_traced?'
end
skip RAILS_32_SKIP_MESSAGE if Rails::VERSION::MAJOR == 3

assert Bloodhound.newrelic_method_exists?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.newrelic_method_exists?'
assert Bloodhound.method_traced?('sniff'),
'Bloodhound#sniff not found by' \
'NewRelic::Agent::MethodTracer::ClassMethods::AddMethodTracer.method_traced?'
end

# All supported Rails versions have the default value for
Expand All @@ -44,42 +29,19 @@ def test_add_method_tracer_in_initializer_gets_traced_when_agent_initialized_aft

def test_active_record_initializer_config_change_saved_when_agent_initialized_after_config_initializers
skip unless defined?(Rails::VERSION)
# TODO: This test passes in a Rails console in a playground app and in the customer's environment
# but fails in this unit test context
skip if Gem::Version.new(NewRelic::VERSION::STRING) >= Gem::Version.new('8.13.0')
skip 'MySQL error for Rails 3.2' if Rails::VERSION::MAJOR == 3

with_config(defer_rails_initialization: true) do
# Verify the configuration value was set to the initializer value
refute Rails.application.config.active_record.timestamped_migrations,
"Rails.application.config.active_record.timestamped_migrations equals true, expected false"

# Verify the configuration value was applied to the ActiveRecord class variable
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('7.1')
refute ActiveRecord.timestamped_migrations, "ActiveRecord.timestamped_migrations equals true, expected false"
else
refute ActiveRecord::Base.timestamped_migrations,
"ActiveRecord::Base.timestamped_migrations equals true, expected false"
end
end
end

def test_active_record_initializer_config_change_not_saved_when_agent_initialized_before_config_initializers
skip unless defined?(Rails::VERSION)
skip 'MySQL error for Rails 3.2' if Rails::VERSION::MAJOR == 3

with_config(defer_rails_initialization: false) do
# Verify the configuration value was set to the initializer value
refute Rails.application.config.active_record.timestamped_migrations,
"Rails.application.config.active_record.timestamped_migrations equals true, expected false"

# Rails.application.config value should not be applied (this is the state of the original bug)
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('7.1')
assert ActiveRecord.timestamped_migrations, "ActiveRecord.timestamped_migrations equals false, expected true"
else
assert ActiveRecord::Base.timestamped_migrations,
"ActiveRecord::Base.timestamped_migrations equals false, expected true"
end
skip RAILS_32_SKIP_MESSAGE if Rails::VERSION::MAJOR == 3
# skip "Test passes in a Rails console on a playground app and in the customer's environment, but fails here"

# Verify the configuration value was set to the initializer value
refute Rails.application.config.active_record.timestamped_migrations,
"Rails.application.config.active_record.timestamped_migrations equals true, expected false"

# Verify the configuration value was applied to the ActiveRecord class variable
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('7.1')
refute ActiveRecord.timestamped_migrations, "ActiveRecord.timestamped_migrations equals true, expected false"
else
refute ActiveRecord::Base.timestamped_migrations,
"ActiveRecord::Base.timestamped_migrations equals true, expected false"
end
end
end