From: Michael Tremer Date: Fri, 29 May 2026 11:10:37 +0000 (+0000) Subject: metrics: Add convenience function to extract metrics from JSON X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7867448873794d7392355c465cfd2b1bcef875b;p=telemetry.git metrics: Add convenience function to extract metrics from JSON Signed-off-by: Michael Tremer --- diff --git a/src/daemon/metrics.c b/src/daemon/metrics.c index 4b05833..36d7548 100644 --- a/src/daemon/metrics.c +++ b/src/daemon/metrics.c @@ -381,6 +381,21 @@ int td_metrics_set(td_metrics* self, const td_metric_value* values) { return 0; } +int td_metrics_push_uint64_from_json(td_metrics* self, const char* field, + sd_json_variant* json, const char* key) { + sd_json_variant* value = NULL; + + // Fetch the value + value = sd_json_variant_by_key(json, key); + if (!value) { + DEBUG(self->ctx, "Could not find key '%s' in JSON object\n", key); + return -EBADMSG; + } + + // Push the value + return td_metrics_push_uint64(self, field, sd_json_variant_unsigned(value)); +} + int __td_metrics_serialize(td_metrics* self, char* buffer, size_t length) { td_metric* metric = NULL; ssize_t bytes_written = 0; diff --git a/src/daemon/metrics.h b/src/daemon/metrics.h index 9a0b1e3..06e9483 100644 --- a/src/daemon/metrics.h +++ b/src/daemon/metrics.h @@ -61,6 +61,8 @@ typedef struct td_metrics_parser { #define VALUE_UINT64(field, value) VALUE(field, TD_METRIC_UINT64, value) #define VALUE_FLOAT(field, value) VALUE(field, TD_METRIC_FLOAT, value) +#include + #include "ctx.h" #include "source.h" @@ -83,6 +85,9 @@ int td_metrics_push_float(td_metrics* self, const char* field, double value); int td_metrics_set(td_metrics* self, const td_metric_value* values); +int td_metrics_push_uint64_from_json(td_metrics* self, const char* field, + sd_json_variant* json, const char* key); + #define td_metrics_serialize(metrics, buffer) \ __td_metrics_serialize(metrics, buffer, sizeof(buffer))