Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit c9c291d

Browse files
authored
feat(content): How to: Debug StatsD locally (#18)
1 parent e4a3750 commit c9c291d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "How to: Debug StatsD locally"
3+
slug: "/writing/how-to-debug-statsd-locally"
4+
date: 2023-03-22
5+
tags:
6+
- "how-to"
7+
- "statsd"
8+
---
9+
10+
It's useful to know what StatsD metrics you're sending locally before it
11+
hits your production metrics sink. Getting a metric name, namespace, or data
12+
type incorrect in production is annoying - it can mess up metrics for a
13+
certain time window even after it's fixed. So, I always try to test any
14+
metric-related changes locally before it hits production.
15+
16+
Assuming this example that sends a few metrics every 2 seconds:
17+
18+
```python
19+
from time import sleep
20+
import statsd
21+
22+
c = statsd.StatsClient("localhost", 8125)
23+
24+
while True:
25+
sleep(2)
26+
c.timing("stats.timed", 1)
27+
c.incr("foo")
28+
```
29+
30+
You can echo the metrics it's sending by using `netcat` to listen for
31+
the UDP packets:
32+
33+
```sh
34+
$ nc -ulv -p 8125
35+
Received packet from 127.0.0.1:58916 -> 127.0.0.1:8125 (local)
36+
stats.timed:1.000000|msfoo:1|c
37+
```
38+
39+
`$ nc -ulv` starts `nc` in UDP listening mode:
40+
* `-u` = UDP
41+
* `-l` = Listen
42+
* `-v` = Verbose Logs
43+
* `-p` = Port of your StatsD sender
44+
45+
You can also take this a step further and bake it into your CI process.
46+
Run an `nc` listener and point your app's StatsD host/port at the `nc`
47+
process, and perform assertions against the output of `nc`.

0 commit comments

Comments
 (0)