|
ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
|
This example makes your ESP32 report measurements - like "free memory is 210 KB" or "I just handled a request" - to a metrics collector, so you can watch graphs of your device over time. It is written for a beginner; no prior monitoring experience needed.
A metric is just a named number you record over and over: a temperature, a count of requests, how long something took. Collect them over time and you can draw a graph and spot trends ("memory is slowly leaking", "traffic spikes at noon").
StatsD is a tiny, wildly popular format for shipping those numbers. Each metric is one short line of text sent over the network:
The |g, |c, |ms on the end says what kind of number it is. That is the whole protocol. Loads of tools understand it: Telegraf, Graphite/StatsD, Datadog, and more.
There are two ways to get metrics off a device: the device can wait to be asked (that is the Prometheus /metrics example, #21), or the device can push them out itself. StatsD is the push way - great when your device is behind a home router and nothing on the internet can reach in to ask it.
nc -u -l 8125 - it prints every StatsD line the ESP32 sends. Perfect for a first look.[[inputs.statsd]] block feeding InfluxDB + Grafana, or the classic StatsD + Graphite.SSID, PASSWORD, and STATSD_HOST (the collector's IP). Leave STATSD_PORT at 8125 (the StatsD standard).pushed metrics every 10 seconds.nc -u -l 8125 shows lines like esp32.heap.free:210000|g|#device:esp32-demo).The |#device:esp32-demo on the end is a tag - an optional label (here set once in pc_statsd_begin) that lets your dashboard group metrics by device. Plain StatsD collectors ignore it; Datadog/Telegraf use it.
The API is one call per metric - drop these wherever something interesting happens:
STATSD_HOST and that the collector is listening on UDP 8125. Also, many home routers isolate wireless clients from each other; if the collector is on WiFi too, put it on a wired port or disable that isolation.|g) is the value right now; a counter (|c) is a running total the collector sums per interval - pick the right type for what you mean.The feature lives in the library, so the flag must reach the whole build:
(The Arduino IDE reads the flag from build_opt.h beside the sketch automatically.)
pc_statsd_begin(host, port, tags) stores the target and optional global tags in fixed BSS. Each pc_statsd_* call renders the value by hand (no printf float/64-bit formatting, which needs extra support on some targets), builds the name:value|type[|@rate][|#tags] line with the pure pc_statsd_format() builder, and sends it with the transport UDP service (pc_udp_sendto). Zero heap; the line format is unit-tested on a PC against the StatsD spec (see test/test_statsd).