Links

Monday, May 19, 2014

enabling New Relic in a Cloud Foundry node application

Something you will want to enable with your apps running in a
Cloud Foundry environment such as BlueMix,
is
monitoring. You'll want a service to watch your app, show you when it's down,
how much traffic it's getting, etc.

The app name is retrieved from your package.json's name property;
and the license key is obtained from an environment variable. Note this
code is completely portable, and can be copied from one project to another
without having to change keys or names.

To set the environment variable for your CloudFoundry app, use the command

cf set-env <app-name> NEW_RELIC_LICENSE_KEY 983498129....

To run the code in the initialize() function, use the following code in
your application startup, as close to the beginning as possible:

This code is different than what you what New Relic suggests you use at the
beginning of your application code;
instead of doing the require("newrelic") directly in your code, it will be
run via the require("./newrelic").initialize() code.

If you don't have the relevant environment variable set, then New Relic monitoring
will not be enabled for your app.

Another option to keeping your license key un-hard-coded and dynamic is to
use a Cloud Foundry service. For instance, you can create a
user-provided service instance using the following command:

cf cups NewRelic -p '{"key":"983498129...."}'

You can then bind that service to your app:

cf bind-service <app-name> NewRelic

Your application code can then get the key from the
VCAP_SERVICES environment variable.

I would actually use services rather than environment variables in most cases, as
services can be bound to multiple apps at the same time, whereas you need to set
the environment variables for each app.

In this case, I chose to use an environment variable, as you really should be
doing the New Relic initialization as early as possible, and there is less
code involved in getting the value of an environment variable than parsing the
VCAP_SERVICES values.

You may want to add some other enhancements, such as appending a -dev or -local
suffix to the app name if you determine you're running locally instead of within
Cloud Foundry.

What this means is that you can most likely get by with a much easier
set up if you want to live the "environment variable configuration" life.
There may still be some value in a more structured approach, like what I've
documented here, if you'd like to be a little more explicit.

Also note that I specified using an environment variable of
NEW_RELIC_LICENSE_KEY, which is the exact same name
as the environment variable that the New Relic module uses itself!
(Great minds think alike?)
As such, it would probably be a good idea, if you want to do explicit
configuration as described here, to avoid using NEW_RELIC_* as
the name of your environment variables, as you may get some unexpected
interaction. In fact, my read of the precedence rules are that the environment
variables override the newrelic.js config file settings, so the setting
in the newrelic.js is ignored in favor of the environment variable, at
least in this example.