Configuring StatsD
Tags: statsd, monitoring
Published: 2019-06-22 14:46:34

---
title: Configuring StatsD
published: true
description: Configuring StatsD
tags: statsd, monitoring
---

## Goal

Learn how to emit metrics with statsD.

---

## What is StatsD
[Statsd's](https://github.com/statsd/statsd) description on its git repo is clear enough so let's just use it:

> 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).

---

## When to use and when not to use StatsD

You should not expect metrics generated with statsD to be 100% precise and extract business metrics from it (e.g: how many clients bought a product, or how many users registered for your newsletter) since data is sent over `UDP` (more on that below). Use statsD metrics to identify abnormalities in your services (e.g.: too many error statuses; methods taking too long to complete; etc). 

> Packages sent over User Datagram Protocol (UDP) do not have guaranteed delivery. More on that this [wikipedia page](https://en.wikipedia.org/wiki/User_Datagram_Protocol).

---

## Our current scenario and goal with statsd

Suppose we have an application which is accessed for not so many users and has an average of 10 login operations per minute. We will have a CloudWatch Alarm to notify us in case there is more than 30 unsuccessful login operations within a minute so we can find out what is going on. The application will emit events with statsD to CloudWatch using CloudWatch Agent.

---

## Run StatsD locally

First make sure you have docker installed and running. Then run the following command:
```bash
docker run --rm\
 --name graphite\
 -p 80:80\
 -p 81:81\
 -p 2003-2004:2003-2004\
 -p 2023-2024:2023-2024\
 -p 8125:8125/udp\
 -p 8126:8126\
 hopsoft/graphite-statsd
```

You can already navigate to http://localhost:81 and see an empty graph from [Graphite](https://graphiteapp.org).

## Our application

[My application for this demo](https://github.com/augustovictor/loud-watch-agent-statsd-collectd) (Instructions on how to run that can be found in the `README.MD` of the repository.

Here is the part of the code that matters. For simplicity I kept everything in the same file:

{% gist https://gist.github.com/augustovictor/72c947c3b41288c42ec2976fd7a07643 %}

## Generate some metrics

Make some successful requests to our api:
```bash
for i in {1..30};
do
	curl -X POST -H 'Content-type: application/json' -i localhost:8080/v1/users/login -d '{"username": "firstuser", "password": "123"}'
done;
```

Now some requests with wrong credentials:
```bash
for i in {1..8};
do
	curl -X POST -H 'Content-type: application/json' -i localhost:8080/v1/users/login -d '{"username": "firstuser", "password": "wrong_passwd"}'
done;
```

Check graphite graph for the requests by navigating http://localhost:81 and open the directories in the left hand side: `Metrics` > `stats_counts` > `my` > `custom` > `app` > `users` > `login` and click the `success` and `error` metrics.

Change the range of time of the displayed data in the empty Graphite Composite panel by clicking the clock icon and set its `Recent Time Range` to `10 min`.

![](https://thepracticaldev.s3.amazonaws.com/i/vmfptn7ykb0tpkppuqgs.png)

## Check your metrics

![](https://thepracticaldev.s3.amazonaws.com/i/1oz8w08xxt4oa9d5ie98.png)