Getting metrics from your Express app without effort with Docker and Grafana
In this article we will have a look on how to get metrics from an Express based application with minimal effort and fun things like Docker and Grafana.
I chose once again to go with Docker to not pollute my system, to not loose more hair during installation (my hair potential is quite low…) and to have fun, again. On the metrics side, I choose to use the statsd/grafana stack:
- statsd: A network daemon that runs on the Node.js platform and listens for statistics, like counters and timers, sent over UDP or TCP and sends aggregates to one or more pluggable backend services (e.g., Graphite).
- grafana: An open source, feature rich metrics dashboard and graph editor for Graphite, InfluxDB & OpenTSDB.
What I want to achieve here, is to get some statistics about my Express application API usage, get some counters about errors, response time, etc… Based on these requirements, an easy solution is to plug an Express middleware somewhere which will send data to statsd. Here is where express-statsd comes in (please note the originality of the name, and how hard it is to find it while googling ‘node express statsd’).
Let’s go for some small express application with the monitoring middleware:
Now that the application is ready to be monitored, let’s run all this stuff. Let’s do it with docker-compose so that all is launched and bound without effort (yes I am lazy, and tools like docker are also for lazy guys).
The docker-compose descriptor above creates the required link between the express container and the statsd container. By doing so, we just can use the statsd container name in our express application as hostname.
This says to docker to pull all images, build the node app container and starts all the containers in the right order with the right links as defined in the docker-compose file. The app is now ready to be ‘monitored’.
As defined in the docker-compose file, Grafana is available on http://HOST:3001. Just connect to it using the admin/admin credentials and let’s create a new dashboard to display metrics:
Note that in the video, we connect to the Graphite data source. This is the simplest way to feed Grafana with data coming from statsd. Graphite is bundled in the hopsoft/graphite-statsd Docker image. The URL for the datasource is http://192.168.59.103:3002/ where:
- 192.168.59.103 is the IP address of my boot2docker VM which is visible from the containers.
- 3002 is the Graphite port as defined in the docker-compose descriptor.
The source code with the express app and all the Docker stuff is available on https://github.com/chamerling/lazyguy-monitoring.
Please note that this is a really simplistic way to get metrics. I really think that you should not use it like this in production (monitoring all your routes like I did here is quite ugly). But getting stats on your API usage may allow you to enhance the way you build your backend and frontend code.