How to add performance monitoring to node.js and io.js applications
We are starting to use Node.js here at Sematext and since eating one’s own dogfood is healthy, we wanted to be able to monitor our Node.js apps with SPM (we are in performance monitoring and big data biz). So the first thing we do in such a case is adding monitoring capability for technology we use in-house (like we did for Java, Solr, Elasticsearch, Kafka, HBase, NGINX). For example we monitor Kibana4 servers (based on node.js), which we have in production for our “ 1-click ELK stack ”.
You may have seen our post about SPM for Node.js — but I thought I’d share a bit about how we monitor Node.js to help others facing the same devops needs when introducing new Node.js apps or the challenge to operate large deployments with a mix of technologies in the application stack:
- npm i spm-agent-nodejs
It’s open source on Github: sematext/spm-agent-nodejs
3) set the Environment variable for the application token
4) add one line to the beginning of your source code when using node.js, for io.js got a better option/see below …
var spmAgent = require (‘spm-agent-nodejs’)
5) Run you app and after 60 seconds you should see first metrics in SPM
At this point what do I get? I can see pre-defined metric charts like these, with about 5 minutes of work :)
I saved already time — no need to define metric queries/widgets/dashboards .
Now i could set up alerts on Latency or Garbage Collection, or I could have anomaly detection tell me when the number of Workers in a dynamic queue changes drastically. I typically set ‘Algolerts’ (basically machine learning based anomaly detection) to get notified (e.g. via PagerDuty ) when a service suddenly slows down — it produces less noise than regular threshold alerts. In addition I recommend to add Heartbeat alerts for each monitored service to be notified on any server outage or network problem. In our case, where a node.js app runs tasks on Elasticsearch it makes sense to create custom dashboard to see Elasticsearch and Node.js metrics together (see 2nd screenshot above) — of course this applicable for other applications in the stack like Nginx, Redis or HaProxy — or combine it with Docker container metrics .
In fact you can use the application token for multiple servers — to see how your cluster behaves using the “ birds eye view ” (a kind of top + df to show the health of all your servers )
But have a look to the differnce to the procedure when using io.js …
io.js supports preloading modules
When we use io.js preload command-line option, we can add instrumentaion without adding the require statement for ‘spm-agent-nodejs’ to the source code:
That’s why Step 4) c ould be done even better with the io.js (>1.6) :
iojs -r “./spm-agent-nodejs” yourApp.js
This is just a little feature but it shows how the io.js community is listening to the needs of users and is able to release such things quickly.
If you want to try io.js, here is how to install it: npm i n -g n io 2.3.1
The ‘node’ executable is now linked to ‘iojs’ — to switch back to node 0.12 simply use n 0.12
I hope this helps and if you’d like to see some Node.js/io.js metrics that are currently not being captured by SPM — please let me know or simply open an issue here: https://github.com/sematext/spm-agent-nodejs/