Question

I'm using New Relic for Rails, but only need New Relic to monitor my one production environment. However, I have a staging server that also runs in Production mode. New Relic detects both as operating prod instances of my application (as it should), but I don't want to have to pay for monitoring two instances when I only want to keep an eye on the one.

New Relic accepts an environment variable for the license key:

license_key: <%= ENV['NEWRELIC_ACCOUNT_KEY'] %>

Is it possible to detect the hostname in the environment files (i.e. config/environments/production.rb) so that I can define this environment variable dynamically?

Both environments are deployed from the same Git repository, same source code base, so I can't really change it in the code for each deploy - it would be best to be able to define the environment variable dynamically somehow.

Any thoughts would be appreciated! Thanks!

Was it helpful?

Solution

The New Relic Ruby agent will parse the newrelic.yml configuration file as ERB before parsing it as YAML. Therefore, you can do lots of neat tricks, such as:

production:
  license_key: <%= Socket.gethostname == 'mission-critical.example.com' ? 'PAID_ACCOUNT_LICENSE_KEY' : 'FREE_LITE_ACCOUNT_LICENSE_KEY' %>

This parsing all happens at app boot time.

If your RAILS_ENV were different from 'production' in that staging server that runs "in Production mode", then you could just use the environment sections as Steve Graham suggests.

OTHER TIPS

Have you created a separate environment for staging? i.e. development, test, production, and staging? See here for instructions…

Having done that, one can specify behaviour for that environment in config/newrelic.yml

common: &default_settings
  license_key: 'PASTE_YOUR_KEY_HERE'
  …

development:
  enabled: false
  …

test:
  enabled: false
  …

production:
  enabled: true
  …

staging: 
  enabled: false
  …

etc, etc

The host name is only available in a request object, so no, it's not available when rails builds the environment.

I would just set up your staging deploy script to copy production.rb to staging.rb and start up the app in "staging". Another technique would be to not keep the newrelic.yml in your scm and instead copy it, or link it, from somewhere else to the current deploy directory.

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