Search code examples
ruby-on-railsruby-on-rails-pluginsnewrelic

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!


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.