The NewRelic Ruby Agent comes with great support for Rails, Sinatra, and other frameworks and web servers out of the box. It also supports background jobs for frameworks like DelayedJob and Resque.
But what if you have your own custom background worker mechanism?
It’s fairly simple to get NewRelic working to report your custom background workers, but finding the right combination of setup calls in their docs can be a little tricky. The biggest issue is dealing with background tasks that daemonize and fork child worker processes. This is because the NewRelic agent needs to do unique instrumenting, monitoring, and reporting per process. Setting it up that way can be tricky if you’re using Bundler or another mechanism to load the
newrelic_rpm gem before the child processes are forked.
Assuming you are already familiar with the mechanics of Ruby-based daemon processes, here are the key ingredients you need to integrate the NewRelic Ruby Agent:
- Store your
newrelic.ymlconfig file somewhere and make a place for its log file to be written.
- Setup the environment variables
NEW_RELIC_LOGto take the place of
RAILS_ENVand default config and log paths you may be used to in Rails.
- Require the
newrelic_rpmgem or add it to your
Gemfileand require it via Bundler.
- Add instrumentation to your main job class with
- Add a tracer to your main job execution method, e.g.:
add_transation_tracer :execute, :category => :task, in your main job class.
- Before you daemonize and fork child processes, make sure to call
- In the child process, right after it’s been forked, make sure to call
::NewRelic::Agent.after_fork(:force_reconnection => true).
This will now make sure that the NewRelic Agent is started correctly for each child process and will report metrics on the
execute method of your job class.
While it’s not my intention to go into detail on how to build out a daemonized forking worker mechanism, below is a very simple worker script that demonstrates all of these pieces together. It assumes the use of Bundler and a directory structure like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
worker.rb script forks 4 worker daemon processes, each of which will report timing metrics to NewRelic for the jobs it runs. Note the comments correlating to the bullet points above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67