Question

We have a Rails web app stack which includes some code that runs EventMachine in a separate process (Heroku instance). They share common code and classes and it hasn't made sense to separate them out.

I'd like to report the data to New Relic under separate app names, so that the instance stats (CPU, memory), are not all getting averaged together, as the EventMachine process has very different usage characteristics than the web processes.

Ideally, I'd like the EventMachine process to pick up a different configuration stanza in the newrelic.yml file.

I've tried calling NewRelic::Control.instance.init_plugin(:agent_enabled => true, :env => 'em-process') to re-initialize with a different environment. That didn't work.

I've also tried to shutdown and restart the agent: ::NewRelic::Agent.shutdown,
::NewRelic::Agent.manual_start(env: 'connector') But I saw this in the log: This agent connected from parent process 10878--not sending shutdown -- that didn't work either.

How can this be done?

Was it helpful?

Solution

TLDR:

A cleaner approach is add the following line to your environment.rb file before the call to YourApp::Application.initialize!:

NewRelic::Control.instance.env = 'em-process' if ($0 =~ /em-process/)

Details:

The trick is to ensure that NewRelic::Control.instance.env is set before the newrelic_rpm.start_plugin Railtie initializer runs (which starts the agent and reads that env value). The reason this can't go in an initializer file is that files in config/initializers get loaded after all Railtie initializers run.

With this approach you don't need to disable the auto-require on the newrelic_rpm gem in your Gemfile nor do you need to create a new initializer file, so it's a little cleaner.

Also in our case this approach was better because we don't want the New Relic agent running in rails consoles or migrations, so we have agent_enabled: false in our newrelic.yml file and we explicitly set the environment variable NEWRELIC_ENABLE=true when we want to enable the agent (only when our processes run on Heroku). When we went with the config/initializers/new_relic.rb approach it caused the agent to start up in rails consoles and migrations locally and on Heroku, which we didn't want. We tried to change the manual_start call to:

NewRelic::Agent.manual_start(args) if ENV['NEWRELIC_ENABLE']

But then we got exceptions like undefined method 'add_method_tracer' when NEWRELIC_ENABLE wasn't set, because it's the call to NewRelic::Agent.manual_start that makes those instrumentation methods available.

The upshot is that the newrelic_rpm gem wasn't designed to behave well when setting require: false in the Gemfile.

OTHER TIPS

I got a very helpful answer from Ben Weintraub at New Relic.

The best option is to not auto-require and auto-start the agent through bundler, by setting require: false in the Gemfile:

gem 'newrelic_rpm', '~> 3.6.5.130', require: false

And then adding an initializer file, e.g. config/initializers/new_relic.rb:

require 'newrelic_rpm'

args = {}
args[:env] = 'em-process' if ($0 =~ /em-process/)  # start in em-process environment if script name includes the word 'em-process'
NewRelic::Agent.manual_start(args)

This assumes the name of the startup script for the EventMachine process includes the string em-process.

Thanks Ben!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top