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

Defer agent initialization in Rails until after :load_config_initializers #1658

Conversation

kaylareopelle
Copy link
Contributor

@kaylareopelle kaylareopelle commented Nov 29, 2022

For Rails applications, the agent initialized before all other initializers with the before: :load_config_initializers hook.

Though this enables initializers to reference the add_method_tracer API, there are actions within the agent's initialization that load Rails framework libraries (ActiveJob, ActiveRecord, ActionView). This caused initializers referencing these libraries to be skipped entirely.

Since we only need the add_method_tracer API to be available before the initializers are run, the modules that include this API will be loaded before config initializers. All other agent-initialization activity will be loaded after config initializers.

Furthermore, ActionView was the only Rails framework library that did not have an ActiveSupport.on_load block around the instrumentation executes block. This has been added.

Closes #662

@kaylareopelle kaylareopelle changed the title Instrument ActionView on_load Defer agent initialization in Rails until after :load_config_initializers Dec 7, 2022
@kaylareopelle kaylareopelle marked this pull request as ready for review December 7, 2022 00:18
@kaylareopelle kaylareopelle force-pushed the bugfix/rails-frameworks-forced-to-load-during-agent-initialization branch 4 times, most recently from 249ae16 to a4e149b Compare December 7, 2022 19:08
fallwith
fallwith previously approved these changes Dec 7, 2022
Previously, ActionView was forced to load by the agent before ActiveSupport
loaded its code, causing any application-defined initializers related
to ActionView to be ignored.
Subscribing after ActiveSupport loads ActionView resolves the problem.
The central argument around loading the agent before other
initializers is to allow initializers to reference the add_method_tracer
API. Including the modules before other initializers are run
allows this to happen.

If the second initializer doesn't specify when it should be run, it
will run immediately after our first initializer. By specifying after:
:load_config_initializers, the agent will not execute init_plugin until
after initializers defined in config/initializers are run. This stops
the agent from forcing ActiveRecord to load before customer-defined
initializers.
The previous instructions were not compatible with Zsh.
These instructions work with Zsh and bash.
@kaylareopelle kaylareopelle force-pushed the bugfix/rails-frameworks-forced-to-load-during-agent-initialization branch from a4e149b to bbd78a1 Compare December 7, 2022 19:54
Starting the agent after :load_config_initializers prevents the dynamic
initializer for roadie-rails from running, making the gem moot. The customer
who used the bugfix also had an error pop up with one of their
initializers. Though this change solves the problem with configs getting
applied, it may interact unpredictably with existing initializers.
We won't turn on this behavior by default until a major version release.
CHANGELOG.md Outdated Show resolved Hide resolved
lib/new_relic/agent/configuration/default_source.rb Outdated Show resolved Hide resolved
@kaylareopelle kaylareopelle force-pushed the bugfix/rails-frameworks-forced-to-load-during-agent-initialization branch from cb02458 to 7ccc3c5 Compare December 8, 2022 17:59
fallwith
fallwith previously approved these changes Dec 8, 2022
fallwith
fallwith previously approved these changes Dec 8, 2022
@github-actions
Copy link

github-actions bot commented Dec 8, 2022

SimpleCov Report

Coverage Threshold
Line 93.2% 93%
Branch 84.51% 84%

@kaylareopelle kaylareopelle merged commit c3337d8 into dev Dec 8, 2022
@kaylareopelle kaylareopelle deleted the bugfix/rails-frameworks-forced-to-load-during-agent-initialization branch December 8, 2022 18:50
@kaylareopelle kaylareopelle restored the bugfix/rails-frameworks-forced-to-load-during-agent-initialization branch December 8, 2022 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RPM forces Rails frameworks to load during initialization
2 participants