DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
statsd.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file statsd.h
6 * @brief StatsD metrics client - push counters/gauges/timings/sets to a StatsD collector.
7 *
8 * StatsD is the de-facto push metrics protocol: one line per metric, `name:value|type`, over
9 * UDP (fire-and-forget). It is spoken by Graphite/StatsD, Telegraf, Datadog, InfluxDB, and
10 * most observability stacks. This is the push counterpart to the pull-based Prometheus
11 * `/metrics` endpoint: instead of a scraper polling the device, the device shoves metrics out
12 * as things happen - handy behind NAT/firewalls where nothing can reach in to scrape.
13 *
14 * Types: counter (`c`), gauge (`g`, absolute or a signed `+`/`-` delta), timing (`ms`), and
15 * set (`s`). Optional sample rate (`|@0.1`) and DogStatsD tags (`|#env:prod,host:a`).
16 *
17 * The line builder (statsd_format) is pure and host-tested; the emit helpers format the value
18 * and send via the transport UDP service (det_udp_sendto), so they are host-testable through
19 * its capture seam too. Zero heap; gated by DETWS_ENABLE_STATSD.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef DETERMINISTICESPASYNCWEBSERVER_STATSD_H
26#define DETERMINISTICESPASYNCWEBSERVER_STATSD_H
27
28#include <stddef.h>
29#include <stdint.h>
30
31/** @brief StatsD metric type codes (the token after the `|`). */
32enum class StatsdType : uint8_t
33{
34 STATSD_COUNTER = 'c',
35 STATSD_GAUGE = 'g',
36 STATSD_TIMING = 'm', ///< emitted as "ms"
37 STATSD_SET = 's',
38};
39
40/**
41 * @brief Format one StatsD line: `name:value|type[|@rate][|#tags]`. Pure - no I/O.
42 *
43 * @param out output buffer (NUL-terminated on success).
44 * @param cap capacity of @p out.
45 * @param name metric name (e.g. "api.requests").
46 * @param value the value already rendered as text (e.g. "1", "-3", "42.5").
47 * @param type a ::StatsdType code ('c','g','m','s').
48 * @param rate sample rate in (0,1]; >= 1 emits no `|@` annotation.
49 * @param tags DogStatsD tag string "k:v,k2:v2" (no leading `#`), or nullptr for none.
50 * @return line length (excluding the NUL), or 0 on a bad argument or overflow.
51 */
52size_t statsd_format(char *out, size_t cap, const char *name, const char *value, StatsdType type, float rate,
53 const char *tags);
54
55/**
56 * @brief Point the client at a collector and set optional global tags (added to every metric).
57 * @param host collector hostname/IP.
58 * @param port UDP port (0 selects DETWS_STATSD_PORT).
59 * @param global_tags DogStatsD tags applied to every metric, or nullptr.
60 */
61void statsd_begin(const char *host, uint16_t port, const char *global_tags);
62
63/** @brief Increment a counter by @p delta (may be negative). */
64void statsd_count(const char *name, int64_t delta);
65
66/** @brief Increment a counter, annotated with sample @p rate (0,1] so the server scales up. */
67void statsd_count_sampled(const char *name, int64_t delta, float rate);
68
69/** @brief Set a gauge to an absolute @p value. */
70void statsd_gauge(const char *name, int64_t value);
71
72/** @brief Adjust a gauge by a signed @p delta (sent as a `+`/`-` gauge update). */
73void statsd_gauge_delta(const char *name, int64_t delta);
74
75/** @brief Record a timing/duration of @p ms milliseconds. */
76void statsd_timing(const char *name, uint32_t ms);
77
78/** @brief Add a unique @p member to a set (counts distinct values server-side). */
79void statsd_set(const char *name, const char *member);
80
81#endif // DETERMINISTICESPASYNCWEBSERVER_STATSD_H
void statsd_gauge_delta(const char *name, int64_t delta)
Adjust a gauge by a signed delta (sent as a +/- gauge update).
void statsd_begin(const char *host, uint16_t port, const char *global_tags)
Point the client at a collector and set optional global tags (added to every metric).
void statsd_count(const char *name, int64_t delta)
Increment a counter by delta (may be negative).
void statsd_timing(const char *name, uint32_t ms)
Record a timing/duration of ms milliseconds.
StatsdType
StatsD metric type codes (the token after the |).
Definition statsd.h:33
@ STATSD_TIMING
emitted as "ms"
void statsd_set(const char *name, const char *member)
Add a unique member to a set (counts distinct values server-side).
size_t statsd_format(char *out, size_t cap, const char *name, const char *value, StatsdType type, float rate, const char *tags)
Format one StatsD line: name:value|type[|@rate][|#tags]. Pure - no I/O.
void statsd_gauge(const char *name, int64_t value)
Set a gauge to an absolute value.
void statsd_count_sampled(const char *name, int64_t delta, float rate)
Increment a counter, annotated with sample rate (0,1] so the server scales up.