Sebastian Julius
Sebastian Julius

Reputation: 155

Newrelic via Heroku doesn't receive data from Ruby instance running in Unicorn although preload_app=true

I'm running a ruby app with grape (no rails) on heroku and am using the new relic addon. The app is forked with unicorn. As described here, I integrated this option to my configs. Here's my setup:

unicorn.rb:
worker_processes 3
preload_app true
timeout 30

config.ru:
if ENV['RACK_ENV'] == 'production'
  require 'newrelic_rpm'
end

The environment on heroku is set to production, so the file should be included properly. Furthermore, the heroku log files indicate that the new relic agent starts up successfully:

2013-04-08T10:47:47+00:00 heroku[deployhooks]: Notified New Relic about the deploy
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Reading configuration from config/newrelic.yml
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Environment: production
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Dispatcher: unicorn
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Application: mobile-v1-ruby
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Connecting workers after forking.
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Installing Net instrumentation
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Installing Unicorn instrumentation
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Detected Unicorn, please see additional documentation: https://newrelic.com/docs/troubleshooting/im-using-unicorn-and-i-dont-see-any-data
2013-04-08T10:47:49+00:00 app[web.1]: ** [NewRelic][04/08/13 10:47:49 +0000 eea5ecfd-86be-4b88-8b2a-6b7564aa9943 (2)] INFO : Finished instrumentation
2013-04-08T10:47:49+00:00 app[web.1]: Hey there - I started in environment: production
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.798057 #2]  INFO -- : listening on addr=0.0.0.0:58224 fd=9
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.798299 #2]  INFO -- : worker=0 spawning...
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.809140 #2]  INFO -- : worker=1 spawning...
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.812632 #5]  INFO -- : worker=0 spawned pid=5
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.816144 #2]  INFO -- : worker=2 spawning...
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.819594 #5]  INFO -- : worker=0 ready
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.821252 #9]  INFO -- : worker=1 spawned pid=9
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.823869 #2]  INFO -- : master process ready
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.826441 #9]  INFO -- : worker=1 ready
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.831072 #13]  INFO -- : worker=2 spawned pid=13
2013-04-08T10:47:49+00:00 app[web.1]: I, [2013-04-08T10:47:49.836053 #13]  INFO -- : worker=2 ready
2013-04-08T10:47:50+00:00 heroku[web.1]: State changed from starting to up

To me everything seems to work good. But on new relic, I only receive the deployment notifications. Does anybody have an idea?

Thank you very much.

Upvotes: 2

Views: 725

Answers (2)

Sebastian Julius
Sebastian Julius

Reputation: 155

I was able to solve it now. Thanks for all your help, guys! It's working with developer mode, grape requests showing up and database requests to mongodb via mongoid.

Guess what: One of the worst problems was the string comparison in my conditional loadings:

if ENV['RACK_ENV'] == "production"

The quals with == doesn't seem to work. I changed that to the .eql?() method plus some reloading stuff with newrelic instrumentation and now everything works magically :D Awesome!

if ENV['RACK_ENV'].eql?("production")

Here's my working setup:

Gemfile

gem "newrelic-grape"
gem "newrelic_moped"
gem "newrelic_rpm"

config.ru

run MyApp.new

if ENV["NEW_RELIC_ENABLE"].eql?("true")

    if ENV["RACK_ENV"].eql?("development")
      puts "Loading NewRelic in developer mode ..."
      require "new_relic/rack/developer_mode"
      use NewRelic::Rack::DeveloperMode
    end

    if !ENV["RACK_ENV"].eql?("test")
        NewRelic::Agent.manual_start
        DependencyDetection.detect!
    end

end

This loads new relic in developer mode if the RACK_ENV is defined properly. Otherwise, it starts regulary except if in test mode.

The biggest problem here was the missing command NewRelic::Agent.manual_start and the DependencyDetection.detect!

The api.rb class includes now the Rack instrumentation if new relic was enabled.

api.rb

if !ENV['RACK_ENV'].eql?("test") && ENV['NEW_RELIC_ENABLE'].eql?("true")
    include NewRelic::Agent::Instrumentation::Rack
end

Upvotes: 1

Michael Granger
Michael Granger

Reputation: 1368

New Relic doesn't (currently) have auto-detection for Grape, so it's likely you'll have to install the NR rack middleware yourself. It should just be a matter of an include in your Grape::API class:

class My::API < Grape::API
  include NewRelic::Agent::Instrumentation::Rack

  # ...

end

For more in-depth details, the API docs for the Rack instrumentation is a good place to start.

Upvotes: 0

Related Questions