• Log in

Instrument your application with our APM agent

10 min

lab

This procedure is part of a lab that teaches you how to monitor your application with New Relic.

Each procedure in the lab builds upon the last, so make sure you've completed the last procedure, Set up your lab environment, before starting this one.

In this procedure, you instrument your app with New Relic to start monitoring its behaviors and performance.

Specifically, you use our guided install to collect application performance monitoring (APM) data and send it to New Relic so you can analyze it later.

Install Javascript APM agent

Step 1 of 10

Navigate to New Relic, and sign in with your account.

Step 2 of 10

On the right side of the upper navigation bar, click Add more data.

Add more data

Step 3 of 10

Click Guided install.

Arrow pointing to the guided install button

This walks you through the installation process.

Step 4 of 10

Select APM (Application Monitoring), and click Node JS.

Arrow pointing to the Node JS agent

Step 5 of 10

Click Begin installation.

Arrow pointing to Begin installation button

Step 6 of 10

Select Package manager.

Arrow pointing to Begin installation button

This guides you through instrumenting your application with our Node JS agent using npm.

Step 7 of 10

Select your account, name your application "food-me", and copy the Install the agent command to your clipboard.

Boxes to select account and name your application

Step 8 of 10

Open a terminal window and execute the Install the agent command in the root directory of your application.

bash
$
npm install newrelic --save
added 58 packages, and audited 124 packages in 7s
6 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Step 9 of 10

Download your custom config file from New Relic.

Arrow pointing to Download agent configuration file

This downloads the agent configuration file for your application. Put this file in the root directory of your app.

Step 10 of 10

In your app code, update server/index.js to add New Relic to your application.

require('newrelic');
var express = require('express');
var fs = require('fs');
var open = require('open');
var RestaurantRecord = require('./model').Restaurant;
var MemoryStorage = require('./storage').Memory;
var API_URL = '/api/restaurant';
var API_URL_ID = API_URL + '/:id';
var API_URL_ORDER = '/api/order';
var removeMenuItems = function(restaurant) {
var clone = {};
Object.getOwnPropertyNames(restaurant).forEach(function(key) {
if (key !== 'menuItems') {
clone[key] = restaurant[key];
}
});
return clone;
};
exports.start = function(PORT, STATIC_DIR, DATA_FILE, TEST_DIR) {
var app = express();
var storage = new MemoryStorage();
// serve static files for demo client
app.use(express.static(STATIC_DIR));
// API
app.get(API_URL, function(req, res, next) {
res.status(200).send(storage.getAll().map(removeMenuItems))
});
app.post(API_URL, function(req, res, next) {
var restaurant = new RestaurantRecord(req.body);
var errors = [];
if (restaurant.validate(errors)) {
storage.add(restaurant);
return res.status(201).send(restaurant);
}
return res.status(400).send({error: errors});
});
app.post(API_URL_ORDER, function(req, res, next) {
console.log(req.body)
return res.status(201).send({ orderId: Date.now()});
});
app.get(API_URL_ID, function(req, res, next) {
var restaurant = storage.getById(req.params.id);
if (restaurant) {
return res.status(200).send(restaurant);
}
return res.status(400).send({error: 'No restaurant with id "' + req.params.id + '"!'});
});
app.put(API_URL_ID, function(req, res, next) {
var restaurant = storage.getById(req.params.id);
var errors = [];
if (restaurant) {
restaurant.update(req.body);
return res.status(200).send(restaurant);
}
restaurant = new RestaurantRecord(req.body);
if (restaurant.validate(errors)) {
storage.add(restaurant);
return res.status(201).send(restaurant);
}
return res.status(400).send({error: errors});
});
app.delete(API_URL_ID, function(req, res, next) {
if (storage.deleteById(req.params.id)) {
return res.status(204).send(null);
}
return res.status(400).send({error: 'No restaurant with id "' + req.params.id + '"!'});
});
// only for running e2e tests
app.use('/test/', express.static(TEST_DIR));
// start the server
// read the data from json and start the server
fs.readFile(DATA_FILE, function(err, data) {
JSON.parse(data).forEach(function(restaurant) {
storage.add(new RestaurantRecord(restaurant));
});
app.listen(PORT, function() {
open('http://localhost:' + PORT + '/');
// console.log('Go to http://localhost:' + PORT + '/');
});
});
// Windows and Node.js before 0.8.9 would crash
// https://github.com/joyent/node/issues/1553
try {
process.on('SIGINT', function() {
// save the storage back to the json file
fs.writeFile(DATA_FILE, JSON.stringify(storage.getAll()), function() {
process.exit(0);
});
});
} catch (e) {}
};
server/index.js

That's it! Your application is now instrumented with our APM agent.

Restart your application

Now that you've instrumented your application, it's time to restart your local server.

bash
$
node ./server/start.js
Go to http://localhost:3000/

Restart your load generator, as well.

bash
$
python simulator.py

Important

Make sure you're running these commands in the correct terminal windows. If you no longer have those windows, follow the steps in the setup procedure.

View your data

Your app is now sending data to New Relic. Check it out!

Step 1 of 2

Navigate to New Relic, and select APM from top navigation.

Arrow pointing to APM

Step 2 of 2

Select food-me.

Arrow pointing to food-me app

Here, you see your Web transaction time, Apdex score, Throughput, and other performance data from your app.

APM view of food-me

You've instrumented your application to send APM data to New Relic using our Node JS agent. You also saw your data in the platform. Now, it's time to instrument your application with our browser agent.

In the terminal window that's running your application, press <CTRL-C> to shut down your application. With your app shut down, you'll be able to update your code to introduce monitoring tools.

lab

This procedure is part of a lab that teaches you how to monitor your application with New Relic. Now that you've instrumented your application with our APM agent, instrument your application with our browser agent.

Copyright © 2022 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.