Nodejs monitoring in production server is very important and critical part in overall Nodejs application development. Unlike staging or development scenario, if anything goes wrong in production server it directly affects the end-user and most importantly your client. In this tutorial we will look over how to properly monitor our Nodejs server after deploying it to production using PM2 node module and keymetrics as analysis platform.
What is PM2
PM2 is a process manager for Nodejs application. It comes up with its own load balancer. It make sure that your Nodejs application is fully available to end-user by reloading them in case of exception.
Installing PM2 is easy and you can do this by using following command.
PM2 should be installed as a global node module so that’s what -g will do. You may need to pass sudo access to it in case you are using Mac or Linux-based system.
Integrating PM2 with Nodejs
Before doing the integration let’s create one basic Node.js app using Express.
Use npm init to create a package.json.
"test": "echo "Error: no test specified" && exit 1"
Let’s install Express module. Run following command.
Let’s create a basic server and integrate pm2 with it.
var app = express();
var router = express.Router();
In order to run this app, we generally using npm or node
You can run following command to check the status of the app every time.
Visit localhost:300 to view the app.
Now if your app crashes due to any issue, PM2 will restart it automatically. In order to stop the app you can run following command.
In order to stop all process at once, run following command.
To stop the PM2 itself, run following command.
To monitor the process live, run following command.
This feed will update live as your application keeps running and used.
Integrating PM2 with Keymetrics
Keymetrics is a web platform to monitor your application using PM2. We just had a look above about monitor command in PM2. How about having that live information in your browser ? Pretty amazing to me. Go checkout keymetrics and sign up for new account.
Once you sign up, create a new Bucket.
After this, Keymetrics will provide you the URL by which you can link your app to Keymetrics.
Just copy the command and paste it in your terminal.
This is it, within a second you can see the live update of your application in your browser.
Keymetrics provides various features and admin option which you can use to perform Nodejs monitoring. Let’s have a look over some of them.
CPU and Memory usage
In the main dashboard you can see and visualize the amount of memory and CPU your app is consuming. This data is also live and you can see the comparative graph as well.
This is one of the coolest part which i personally like more. In case your application crashes due to some exception, PM2 will report it to keymetrics and keymetrics will immediately send email to your account and will show the exception detail in dashboard.
In order to replicate it, let’s put some exception in our code. For some reason, throwing an exception is not working so I am going to put one syntax error. In our route code place extra comma like this.
Now restart the app using following command.
Hit the route using by visiting localhost:3000 and you should receive an email immediately like this.
Also you can see the exception in dashboard.
Other than Syntax error, it also catches exception which are dynamic such as call stack size exceeded which we discovered in different application.
Monitoring custom events
Other than exception, you can also register for custom events such as “10000 user registered” or anything suitable to your app. You need to install add-on for called pmx in order to emit custom event. Install it using following command.
Then require it in your code and emit the events like this.
pmx.emit("Event name","event detail,supports JSON too");
Wrapping it up
PM2 and Keymetrics together is great tool for Nodejs monitoring in production server. Other than a fact that it keeps your application alive, it also keeps checking for exception and memory, CPU usage. You can also monitor logs in your browser and can restart your application as well.