Adding New Relic Monitoring

Photo credit: Pexels: krzysztof

As promised last time, here is the bonus post in my guide series.

A little while after getting everything running I realized I didn’t have a clear picture on how well it was working. As you may know, New Relic provides really great monitoring products to let you know how your servers and applications are performing. Fueled mostly by curiosity (and a couple times where things had not restarted correctly and I hadn’t known it), I added New Relic monitoring to the Ghost node.js application and to the CoreOS server itself.

My focus areas were on how well Ghost is working, browser performance for the client, and server performance overall. Here is how I accomplished my monitoring solution.

For Ghost performance

The main steps for install New Relic into my Ghost blog are to install the module from npm, and then require the New Relic module in my configuration file.

In my Ghost image’s Dockerfile, I added this line:

RUN npm install newrelic

after the line:

RUN npm install --production

This change makes sure I’ve got the New Relic module installed in the Ghost container.

Then I added a line to copy my own newrelic.js file (the configuration file for New Relic) into the container by adding this line:

COPY newrelic.js /usr/src/app/newrelic.js

after the line:

COPY config.js /usr/src/app/config.js

In Ghost’s config.js file, I added this at the top:

if (process.env.NEW_RELIC_KEY != undefined) {
  require('newrelic');
}

This will only load the New Relic module if the environment is set up with the license key.

The newrelic.js file is very simple.

exports.config = {
  app_name: ["Dan's Trial and Errno Blog"],
  license_key: process.env.NEW_RELIC_KEY,
  logging: {
    level: 'info'
  }
}

This mostly just sets the application name and loads the license key from the environment as we detected it exists in the Ghost config.js file.

Finally, I modified the CoreOS unit file for Ghost (ghost.service) so that the ExecStart includes the flag to docker -e "NEW_RELIC_KEY=abcd1234cafebeef" but with my actual New Relic key.

For browser performance

To add New Relic monitoring to client side interactions and download performance etc, I just pasted the standard New Relic JavaScript code into my static HTML files and Ghost template layout.

For server performance.

I’m used to running the nrsysmond utility for monitoring my server resource consumption but I was initially unsure how to deploy that into CoreOS. Fortunately this is a solved problem, and there is a Docker image specifically for this use case. The mechanism by which it is loaded was new to me, but it is actually really neat in how accomplishes the task.

Here is the systemd unit file:

[Unit]
Description=Run NewRelic System Monitoring
After=docker.service
Requires=docker.service

[Service]
Restart=always
RestartSec=30s
ExecStartPre=-/usr/bin/docker kill nrsysmond
ExecStartPre=-/usr/bin/docker rm nrsysmond
ExecStart=/usr/bin/docker run --rm --privileged=true --name nrsysmond --pid=host --net=host \
-v /sys:/sys \
-v /dev:/dev \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/log:/var/log:rw \
-e NRSYSMOND_license_key=abcd1234cafebeef \
-e NRSYSMOND_logfile=/var/log/nrsysmond.log \
newrelic/nrsysmond:latest
ExecStop=/usr/bin/docker kill nrsysmond
ExecStartPost=-/usr/bin/docker rm nrsysmond

[Install]
WantedBy=multi-user.target

Here it is pulling the nrsysmond image from the New Relic Docker registry, and then starting it up with a few paths from the host system mounted in, and privileged access to the host OS for the PID namespace and networking. Binding in sys and dev folders give New Relic access to lots of information about how the host system is running.

It also binds in the docker socket so that it can communicate with the docker process and get details about the running images. More on that… right now!

Docker!

New Relic system monitoring recently added direct support to monitor running Docker containers. This means it can give you a breakdown on what containers are running, what resources they are using, and if applicable, what New Relic APM apps are inside your containers.

For the New Relic APM with node.js, here what you see for the Ghost blog running on a server in a container from the APM area of New Relic New Relic APM with node.js in a Docker container

It knows the hostname and container ID and indicates that the application has been containerized.

For nrsysmond, you can see the resource consumption by image and the count of how many containers are running of each image. Docker image resource usage with New Relic nrsysmond And of course, graphs over time: Docker image resource usage graph with new Relic nrsysmond

It is really great that New Relic understands the concept of Docker containers and gives you visibility at that level within their monitoring solution.

That’s a wrap… for now.

So there it is, the complete guide to running Postgres, Ghost, nginx, and New Relic using Docker on CoreOS. If I make any changes to the setup, I’ll be sure to write about it. I hope you enjoyed this series, it has been a lot of fun to put together.