Heroku logo

Heroku Cedar + Rails 3.1 Asset Pipeline + New Relic

We use New Relic RPM to monitor our apps. We also love the new Rails 3.1 asset pipeline, and have been slowly migrating our Heroku apps to the new(ish) Cedar stack.
Unfortunately, when I combined all three for the first time today, I got a nasty surprise:
-----> Preparing app for Rails asset pipeline
 Running: rake assets:precompile
 rake aborted!
 could not connect to server: Connection refused
 Is the server running on host "" and accepting
 TCP/IP connections on port 5432?
 Tasks: TOP => environment
 (See full trace by running task with --trace)
 Precompiling assets failed, enabling runtime asset compilation
 Injecting rails31_enable_runtime_asset_compilation
 Please see this article for troubleshooting help...
Sad robin! Worse, in my case, runtime asset compilation failed as well, yielding a broken app -- not that I would want it doing runtime compilation in production even if it had worked.
The troubleshooting article linked in the log output yields the clue that the environment is disabled and stubbed out during asset precompilation. New Relic RPM attempts to initialize during precompilation, and fails when it initializes ActiveRecord against the invalid stub database supplied.
Amazingly, I didn't find a fix for this when I Googled around! So here's my solution. I wrapped the newrelic.yml in some ERB to disable New Relic when the environment it depends on is missing:
<%unless ENV["NEW_RELIC_LICENSE_KEY"].nil? %>
 capture_source: true
 enabled: true
 ignore_errors: ActionController::RoutingError
 apdex_t: 0.5
 ssl: false
 monitor_mode: true
 license_key: <%= ENV["NEW_RELIC_LICENSE_KEY"] %>
 developer_mode: false
 app_name: <%= ENV["NEW_RELIC_APP_NAME"] %>
 record_sql: obfuscated
 enabled: true
 stack_trace_threshold: 0.5
 transaction_threshold: apdex_f
 capture_params: false
 log_level: info
In addition to fixing the problem during precompilation, this makes my local system happier when I need to check something under -e production.
Any better solutions? Leave me a tweet.
Recent insights