summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorOto Šťáva <oto.stava@nic.cz>2024-03-21 11:49:05 +0100
committerAleš Mrázek <ales.mrazek@nic.cz>2024-04-15 16:28:37 +0200
commit735a00592ca378ee4b6fdee823893f7af9d2945c (patch)
tree23cc680b91072b26202dae6a00eb74f56b5ba32f /modules
parentpoetry: prometheus-client is now optional (diff)
downloadknot-resolver-735a00592ca378ee4b6fdee823893f7af9d2945c.tar.xz
knot-resolver-735a00592ca378ee4b6fdee823893f7af9d2945c.zip
modules/stats: split stats.list() into sub-objects
Diffstat (limited to 'modules')
-rw-r--r--modules/stats/stats.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/modules/stats/stats.c b/modules/stats/stats.c
index ca3a932c..968b46cb 100644
--- a/modules/stats/stats.c
+++ b/modules/stats/stats.c
@@ -57,10 +57,18 @@ enum const_metric {
};
struct const_metric_elm {
const char *key;
+ const char *sup_key;
+ const char *sub_key;
size_t val;
};
static struct const_metric_elm const_metrics[] = {
- #define X(a,b) [metric_ ## a ## _ ## b] = { #a "." #b, 0 },
+ #define X(a,b) \
+ [metric_ ## a ## _ ## b] = { \
+ .key = #a "." #b, \
+ .sup_key = #a, \
+ .sub_key = #b, \
+ .val = 0 \
+ },
CONST_METRICS(X)
#undef X
};
@@ -376,8 +384,15 @@ static char* stats_list(void *env, struct kr_module *module, const char *args)
size_t args_len = args ? strlen(args) : 0;
for (unsigned i = 0; i < metric_const_end; ++i) {
struct const_metric_elm *elm = &const_metrics[i];
- if (!args || strncmp(elm->key, args, args_len) == 0) {
- json_append_member(root, elm->key, json_mknumber(elm->val));
+ if (!args || strcmp(elm->sup_key, args) == 0) {
+ JsonNode *sup = json_find_member(root, elm->sup_key);
+ if (!sup) {
+ sup = json_mkobject();
+ json_append_member(root, elm->sup_key, sup);
+ }
+ if (kr_fails_assert(sup))
+ break;
+ json_append_member(sup, elm->sub_key, json_mknumber(elm->val));
}
}
struct list_entry_context ctx = {