summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorOto Šťáva <oto.stava@nic.cz>2024-04-19 16:18:51 +0200
committerAleš Mrázek <ales.mrazek@nic.cz>2024-04-22 12:43:52 +0200
commit50de6fb35eda7cb6612b75b3938e91fbe6099a9c (patch)
treefc7ca2a20b576b9a201d6dcb79c6fb9cc2f4b420 /modules
parentNEWS: cache prefetching improvements (diff)
downloadknot-resolver-50de6fb35eda7cb6612b75b3938e91fbe6099a9c.tar.xz
knot-resolver-50de6fb35eda7cb6612b75b3938e91fbe6099a9c.zip
modules/stats: make custom stats hierarchical
Forgotten feature from !1527
Diffstat (limited to 'modules')
-rw-r--r--modules/stats/stats.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/modules/stats/stats.c b/modules/stats/stats.c
index 968b46cb..a8a29de2 100644
--- a/modules/stats/stats.c
+++ b/modules/stats/stats.c
@@ -367,8 +367,31 @@ static int list_entry(const char *key, uint32_t key_len, trie_val_t *val, void *
if (!key_matches_prefix(key, key_len, ctx->key_prefix, ctx->key_prefix_len))
return 0;
size_t number = (size_t) *val;
- auto_free char *key_nt = strndup(key, key_len);
- json_append_member(ctx->root, key_nt, json_mknumber(number));
+
+ uint32_t dot_index = 0;
+ for (uint32_t i = 0; i < key_len; i++) {
+ if (!key[i])
+ break;
+ if (key[i] == '.') {
+ dot_index = i;
+ }
+ }
+
+ if (dot_index) {
+ auto_free char *sup_key_nt = strndup(key, dot_index);
+ auto_free char *sub_key_nt = strndup(key + dot_index + 1, key_len - dot_index - 1);
+ JsonNode *sup = json_find_member(ctx->root, sup_key_nt);
+ if (!sup) {
+ sup = json_mkobject();
+ json_append_member(ctx->root, sup_key_nt, sup);
+ }
+ if (kr_fails_assert(sup))
+ return 0;
+ json_append_member(sup, sub_key_nt, json_mknumber(number));
+ } else {
+ auto_free char *key_nt = strndup(key, key_len);
+ json_append_member(ctx->root, key_nt, json_mknumber(number));
+ }
return 0;
}