Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Providing License Key to Plugin Based on Hostname

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!

like image 836
shedd Avatar asked Nov 25 '25 00:11

shedd


2 Answers

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.

like image 70
rkb Avatar answered Nov 27 '25 16:11

rkb


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

like image 20
Steve Graham Avatar answered Nov 27 '25 17:11

Steve Graham