How-tos

New Relic performance monitoring for Node.js apps

Share this post:

The SDK for Node.js buildpack now contains better integration with the New Relic service in Bluemix.

When an app is bound to an instance of the New Relic service, the buildpack recognizes this based on the VCAP_SERVICES environment variable. It will then set the NEW_RELIC_APP_NAME and NEW_RELIC_LICENSE_KEY environment variables, which are recognized by the New Relic agent, for you. If you manually set these environment variables the buildpack will not override them.

Follow the steps below and you’ll be monitoring an app in no time!

Create the app

    1. Change to an empty directory and copy the following code into a file named package.json. This adds a dependency on the New Relic agent’s npm module.
      <code>
      {
      "name": "myapp",
      "version": "0.0.1",
      "private": true,
      "scripts": {
      "start": "node app.js"
      },
      "dependencies": {
      "express": "4.13.x",
      "cfenv": "1.0.x",
      <b>"newrelic": "1.30.3"</b>
      },
      "engines": {
      "node": "4.x"
      }
      }
      </code>

 

    1. Then copy the following code into a file named app.json. This is a simple Express-based app. Notice how the first thing we do is load the newrelic module.
      <code>
      <b>require('newrelic');</b>

      var express = require('express');
      var cfenv = require('cfenv');

      var app = express();
      var appEnv = cfenv.getAppEnv();

      app.get('/', function (req, res) {
      res.send('Hello World');
      })

      app.listen(appEnv.port, '0.0.0.0', function() {
      console.log("server starting on " + appEnv.url);
      });
      </code>

 

  1. Optionally use environment variables or a newrelic.js file in the root of the application to change the default agent configuration options.
    <code>
    exports.config = {
    logging: {
    level: 'trace',
    filepath: 'stdout'
    }
    }
    </code>

     

    See config.default.js for all the configuration options.

  2. Finally, push your app to Bluemix.
    <code>
    $ cf push &lt;app name&gt;
    $ cf create-service newrelic standard &lt;service instance name&gt;
    $ cf bind-service &lt;app name&gt; &lt;service instance name&gt;
    $ cf restage &lt;app name&gt;
    </code>

Monitor the app

Wait a few minutes after your app starts for the data it generates to be reported, then open the New Relic Dashboard from within the Bluemix UI. From here you will automatically be logged in to the New Relic UI.

In the New Relic UI click on your application and you will be able to see your metrics like the web transaction time.

screen-shot-2016-09-29-at-4-17-45-pm

For a good summary of what the different metrics are see A Quick Guide to Getting Started with New Relic.

More How-tos stories
May 3, 2019

Kubernetes Tutorials: 5 Ways to Get You Building Fast

Ready to start working with Kubernetes? Want to build your Kubernetes skills? The five tutorials in this post will teach you everything you need to know about how to manage your containerized apps with Kubernetes.

Continue reading

May 3, 2019

Using Portworx to Deploy and Manage an HA MySQL Cluster on IBM Cloud Kubernetes Service

This tutorial is a walkthrough of the steps involved in deploying and managing a highly available MySQL cluster on IBM Cloud Kubernetes Service.

Continue reading

May 2, 2019

Kubernetes v1.14.1 Now Available in IBM Cloud Kubernetes Service

We are excited to announce the availability of Kubernetes v1.14.1 for your clusters that are running in IBM Cloud Kubernetes Service. IBM Cloud Kubernetes Service continues to be the first public managed Kubernetes service to support the latest upstream versions from the community.

Continue reading