diff options
-rw-r--r-- | cpu.c | 20 | ||||
-rw-r--r-- | diskstats.c | 8 | ||||
-rw-r--r-- | filesystem.c | 16 | ||||
-rw-r--r-- | hwmon.c | 8 | ||||
-rw-r--r-- | network.c | 8 | ||||
-rw-r--r-- | scrape.c | 8 | ||||
-rw-r--r-- | scrape.h | 18 | ||||
-rw-r--r-- | uname.c | 24 |
8 files changed, 59 insertions, 51 deletions
diff --git a/cpu.c b/cpu.c index c746c7b..d8f7886 100644 --- a/cpu.c +++ b/cpu.c @@ -63,19 +63,19 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) { // buffers char cpu_label[MAX_CPU_DIGITS + 1] = ""; - static const char *modes[] = { + static char *modes[] = { "user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", 0, }; - const char *stat_labels[][2] = { - { "cpu", cpu_label }, - { "mode", 0 }, // filled by code - { 0, 0 }, + struct label stat_labels[] = { + { .key = "cpu", .value = cpu_label }, + { .key = "mode", .value = 0 }, // value filled by code + LABEL_END, }; - const char *freq_labels[][2] = { - { "cpu", cpu_label }, - { 0, 0 }, + struct label freq_labels[] = { + { .key = "cpu", .value = cpu_label }, + LABEL_END, }; char buf[BUF_SIZE]; @@ -98,7 +98,7 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) { strcpy(cpu_label, at); at = sep + 1; - for (const char **mode = modes; *mode; mode++) { + for (char **mode = modes; *mode; mode++) { while (*at == ' ') at++; sep = strpbrk(at, " \n"); @@ -112,7 +112,7 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) { break; value /= ctx->clock_tick; - stat_labels[1][1] = *mode; + stat_labels[1].value = *mode; scrape_write(req, "node_cpu_seconds_total", stat_labels, value); at = sep + 1; diff --git a/diskstats.c b/diskstats.c index e622fd3..81231aa 100644 --- a/diskstats.c +++ b/diskstats.c @@ -95,9 +95,9 @@ static void diskstats_collect(scrape_req *req, void *ctx_ptr) { // buffers - const char *labels[][2] = { - { "device", 0 }, // filled by code - { 0, 0 }, + struct label labels[] = { + { .key = "device", .value = 0 }, // value filled by code + LABEL_END, }; char buf[BUF_SIZE]; @@ -123,7 +123,7 @@ static void diskstats_collect(scrape_req *req, void *ctx_ptr) { char *dev = strtok_r(0, " ", &p); if (!dev || *dev == '\0') continue; - labels[0][1] = dev; + labels[0].value = dev; // filter diff --git a/filesystem.c b/filesystem.c index edb5b42..19b2e4e 100644 --- a/filesystem.c +++ b/filesystem.c @@ -81,15 +81,15 @@ static void filesystem_collect(scrape_req *req, void *ctx_ptr) { // buffers - const char *labels[][2] = { // all filled by code - { "device", 0 }, - { "fstype", 0 }, - { "mountpoint", 0 }, - { 0, 0 }, + struct label labels[] = { // all values filled by code + { .key = "device", .value = 0 }, + { .key = "fstype", .value = 0 }, + { .key = "mountpoint", .value = 0 }, + LABEL_END, }; - const char **dev = &labels[0][1]; - const char **fstype = &labels[1][1]; - const char **mount = &labels[2][1]; + char **dev = &labels[0].value; + char **fstype = &labels[1].value; + char **mount = &labels[2].value; char buf[BUF_SIZE]; diff --git a/hwmon.c b/hwmon.c index 896122c..43e9cd0 100644 --- a/hwmon.c +++ b/hwmon.c @@ -190,10 +190,10 @@ static void hwmon_collect(scrape_req *req, void *ctx) { char chip_label[LABEL_SIZE]; char sensor_label[LABEL_SIZE]; - const char *labels[][2] = { - { "chip", chip_label }, - { "sensor", sensor_label }, - { 0, 0 }, + struct label labels[] = { + { .key = "chip", .value = chip_label }, + { .key = "sensor", .value = sensor_label }, + LABEL_END, }; char path[BUF_SIZE]; diff --git a/network.c b/network.c index 5eb2715..7b0e548 100644 --- a/network.c +++ b/network.c @@ -149,9 +149,9 @@ static void network_collect(scrape_req *req, void *ctx_ptr) { // buffers - const char *labels[][2] = { - { "device", 0 }, // filled by code - { 0, 0 }, + struct label labels[] = { + { .key = "device", .value = 0 }, // value filled by code + LABEL_END, }; char buf[BUF_SIZE]; @@ -176,7 +176,7 @@ static void network_collect(scrape_req *req, void *ctx_ptr) { if (!p) continue; *p = '\0'; - labels[0][1] = dev; + labels[0].value = dev; p++; if (ctx->include) { diff --git a/scrape.c b/scrape.c index 463efc1..f9c711b 100644 --- a/scrape.c +++ b/scrape.c @@ -150,17 +150,17 @@ void scrape_close(scrape_server *srv) { free(srv); } -void scrape_write(scrape_req *req, const char *metric, const char *(*labels)[2], double value) { +void scrape_write(scrape_req *req, const char *metric, const struct label *labels, double value) { cbuf_reset(req->buf); cbuf_puts(req->buf, metric); - if (labels && (*labels)[0]) { + if (labels && labels->key) { cbuf_putc(req->buf, '{'); - for (const char *(*l)[2] = labels; (*l)[0]; l++) { + for (const struct label *l = labels; l->key; l++) { if (l != labels) cbuf_putc(req->buf, ','); - cbuf_putf(req->buf, "%s=\"%s\"", (*l)[0], (*l)[1]); + cbuf_putf(req->buf, "%s=\"%s\"", l->key, l->value); } cbuf_putc(req->buf, '}'); } diff --git a/scrape.h b/scrape.h index 8f178c2..8f55a6c 100644 --- a/scrape.h +++ b/scrape.h @@ -38,17 +38,25 @@ void scrape_serve(scrape_server *server, scrape_handler *handler, void *handler_ /** Closes the scrape server sockets and frees any resources. */ void scrape_close(scrape_server *server); +/** Container for label keys and values. */ +struct label { + char *key; + char *value; +}; + +#define LABEL_END ((struct label){ .key = 0, .value = 0 }) + /** * Writes a metric value as a response to a scrape. * - * The \p labels parameter can be `NULL` if no extra labels need to be attached. If not null, it - * should point at the first element of an array of 2-element arrays of pointers, where the first - * element of each pair is the label name, and the second the label value. A pair of null pointers - * terminates the label array. + * The \p labels parameter can be `NULL` if no extra labels need to be + * attached. If not null, it should point at the first element of an + * array of `struct label` objects, terminated by a sentinel value + * with a null pointer as the key. * * Returns `false` if setting up the server failed, otherwise does not return. */ -void scrape_write(scrape_req *req, const char *metric, const char *(*labels)[2], double value); +void scrape_write(scrape_req *req, const char *metric, const struct label *labels, double value); /** * Writes raw data to the scrape response. diff --git a/uname.c b/uname.c index 56267c3..2ea5f92 100644 --- a/uname.c +++ b/uname.c @@ -40,7 +40,7 @@ enum { }; struct uname_context { - const char *labels[max_label + 1][2]; + struct label labels[max_label + 1]; }; static void *uname_init(int argc, char *argv[]) { @@ -53,17 +53,17 @@ static void *uname_init(int argc, char *argv[]) { } struct uname_context *ctx = must_malloc(sizeof *ctx); - ctx->labels[label_machine][0] = "machine"; - ctx->labels[label_machine][1] = must_strdup(name.machine); - ctx->labels[label_nodename][0] = "nodename"; - ctx->labels[label_nodename][1] = must_strdup(name.nodename); - ctx->labels[label_release][0] = "release"; - ctx->labels[label_release][1] = must_strdup(name.release); - ctx->labels[label_sysname][0] = "sysname"; - ctx->labels[label_sysname][1] = must_strdup(name.sysname); - ctx->labels[label_version][0] = "version"; - ctx->labels[label_version][1] = must_strdup(name.version); - ctx->labels[max_label][0] = ctx->labels[max_label][1] = 0; + ctx->labels[label_machine].key = "machine"; + ctx->labels[label_machine].value = must_strdup(name.machine); + ctx->labels[label_nodename].key = "nodename"; + ctx->labels[label_nodename].value = must_strdup(name.nodename); + ctx->labels[label_release].key = "release"; + ctx->labels[label_release].value = must_strdup(name.release); + ctx->labels[label_sysname].key = "sysname"; + ctx->labels[label_sysname].value = must_strdup(name.sysname); + ctx->labels[label_version].key = "version"; + ctx->labels[label_version].value = must_strdup(name.version); + ctx->labels[max_label].key = ctx->labels[max_label].value = 0; return ctx; } |