From 32a9483322ca1462e9be6889f053d6ea3ed1e9df Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 15 May 2015 00:43:12 +0200 Subject: [PATCH] store: Store a service/metric's parent hostname as an attribute. --- src/core/store.c | 14 +++++++ t/unit/core/store_expr_test.c | 16 ++++---- t/unit/core/store_json_test.c | 77 ++++++++++++++++++++++++++++++----- t/unit/frontend/query_test.c | 30 ++++++++++---- 4 files changed, 111 insertions(+), 26 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index cb3189d..ea30da9 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -640,6 +640,7 @@ sdb_store_service(const char *hostname, const char *name, { sdb_host_t *host; sdb_avltree_t *services; + sdb_data_t d; int status = 0; @@ -667,6 +668,12 @@ sdb_store_service(const char *hostname, const char *name, if (sdb_plugin_store_service(hostname, name, last_update)) status = -1; + + /* record the hostname as an attribute */ + d.type = SDB_TYPE_STRING; + d.data.string = SDB_OBJ(host)->name; + if (sdb_store_service_attr(hostname, name, "hostname", &d, last_update)) + status = -1; return status; } /* sdb_store_service */ @@ -721,6 +728,7 @@ sdb_store_metric(const char *hostname, const char *name, sdb_store_obj_t *obj = NULL; sdb_host_t *host; sdb_metric_t *metric; + sdb_data_t d; sdb_avltree_t *metrics; @@ -765,6 +773,12 @@ sdb_store_metric(const char *hostname, const char *name, if (sdb_plugin_store_metric(hostname, name, store, last_update)) status = -1; + + /* record the hostname as an attribute */ + d.type = SDB_TYPE_STRING; + d.data.string = SDB_OBJ(host)->name; + if (sdb_store_metric_attr(hostname, name, "hostname", &d, last_update)) + status = -1; return status; } /* sdb_store_metric */ diff --git a/t/unit/core/store_expr_test.c b/t/unit/core/store_expr_test.c index 5de5fdd..48c128c 100644 --- a/t/unit/core/store_expr_test.c +++ b/t/unit/core/store_expr_test.c @@ -309,23 +309,23 @@ struct { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, "a", SDB_SERVICE, "s1", NULL, { + { SDB_TYPE_STRING, { .string = "hostname" } }, { SDB_TYPE_STRING, { .string = "k1" } }, { 0 }, { 0 }, { 0 }, - { 0 }, - }, 1, + }, 2, }, { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, "a", SDB_SERVICE, "s1", "age >= 0s", { + { SDB_TYPE_STRING, { .string = "hostname" } }, { SDB_TYPE_STRING, { .string = "k1" } }, { 0 }, { 0 }, { 0 }, - { 0 }, - }, 1, + }, 2, }, { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, @@ -378,23 +378,23 @@ struct { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, "b", SDB_METRIC, "m2", NULL, { + { SDB_TYPE_STRING, { .string = "hostname" } }, { SDB_TYPE_STRING, { .string = "k1" } }, { SDB_TYPE_STRING, { .string = "k2" } }, { 0 }, { 0 }, - { 0 }, - }, 2, + }, 3, }, { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, "b", SDB_METRIC, "m2", "age >= 0s", { + { SDB_TYPE_STRING, { .string = "hostname" } }, { SDB_TYPE_STRING, { .string = "k1" } }, { SDB_TYPE_STRING, { .string = "k2" } }, { 0 }, { 0 }, - { 0 }, - }, 2, + }, 3, }, { { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true, diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index ff6a18e..704ad06 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -124,8 +124,7 @@ verify_json_output(sdb_strbuf_t *buf, const char *expected) struct { struct { - sdb_store_matcher_t *(*m)(sdb_store_expr_t *, - sdb_store_expr_t *); + sdb_store_matcher_t *(*m)(sdb_store_expr_t *, sdb_store_expr_t *); int field; sdb_data_t value; } filter; @@ -155,6 +154,9 @@ struct { "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k3\", \"value\": 42, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" @@ -162,7 +164,12 @@ struct { "{\"name\": \"m2\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "]}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " @@ -170,16 +177,29 @@ struct { "{\"name\": \"m1\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "], " "\"services\": [" "{\"name\": \"s1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}," + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}," "{\"name\": \"s2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k1\", \"value\": 123, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}," @@ -215,6 +235,9 @@ struct { "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k1\", \"value\": 123, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" @@ -236,7 +259,12 @@ struct { "{\"name\": \"m2\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "]}" "]" }, { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE, @@ -259,11 +287,19 @@ struct { "\"services\": [" "{\"name\": \"s1\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}," + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}," "{\"name\": \"s2\", " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k1\", \"value\": 123, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}," @@ -298,6 +334,9 @@ struct { "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k1\", \"value\": 123, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" @@ -319,6 +358,9 @@ struct { "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}," "{\"name\": \"k3\", \"value\": 42, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": []}" @@ -326,7 +368,12 @@ struct { "{\"name\": \"m2\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "]}," "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " "\"update_interval\": \"0s\", \"backends\": [], " @@ -334,7 +381,12 @@ struct { "{\"name\": \"m1\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h2\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "]}" "]" }, { { NULL, 0, SDB_DATA_INIT }, @@ -371,7 +423,12 @@ struct { "{\"name\": \"m2\", " "\"timeseries\": false, " "\"last_update\": \"1970-01-01 00:00:00 +0000\", " - "\"update_interval\": \"0s\", \"backends\": []}" + "\"update_interval\": \"0s\", \"backends\": [], " + "\"attributes\": [" + "{\"name\": \"hostname\", \"value\": \"h1\", " + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " + "\"update_interval\": \"0s\", \"backends\": []}" + "]}" "]}" "]" }, { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE, diff --git a/t/unit/frontend/query_test.c b/t/unit/frontend/query_test.c index 0f25493..73f3042 100644 --- a/t/unit/frontend/query_test.c +++ b/t/unit/frontend/query_test.c @@ -91,19 +91,30 @@ populate(void) "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": [], " \ "\"attributes\": [" \ + "{\"name\": \"hostname\", \"value\": \"h1\", " \ + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ + "\"update_interval\": \"0s\", \"backends\": []}," \ "{\"name\": \"k3\", \"value\": 42, " \ "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": []}]}," \ "{\"name\": \"m2\", \"timeseries\": false, " \ "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ - "\"update_interval\": \"0s\", \"backends\": []}]}" + "\"update_interval\": \"0s\", \"backends\": [], " \ + "\"attributes\": [" \ + "{\"name\": \"hostname\", \"value\": \"h1\", " \ + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ + "\"update_interval\": \"0s\", \"backends\": []}]}]}" #define SERVICE_H2_S1 \ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": [], " \ "\"services\": [" \ "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \ - "\"update_interval\": \"0s\", \"backends\": []}]}" + "\"update_interval\": \"0s\", \"backends\": [], " \ + "\"attributes\": [" \ + "{\"name\": \"hostname\", \"value\": \"h2\", " \ + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ + "\"update_interval\": \"0s\", \"backends\": []}]}]}" #define METRIC_H1_M1 \ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \ @@ -113,6 +124,9 @@ populate(void) "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": [], " \ "\"attributes\": [" \ + "{\"name\": \"hostname\", \"value\": \"h1\", " \ + "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ + "\"update_interval\": \"0s\", \"backends\": []}," \ "{\"name\": \"k3\", \"value\": 42, " \ "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \ "\"update_interval\": \"0s\", \"backends\": []}]}]}" @@ -239,11 +253,11 @@ static struct { /* hosts */ { SDB_HOST, "h1", NULL, NULL, - 0, SDB_CONNECTION_DATA, 851, HOST_H1, + 0, SDB_CONNECTION_DATA, 1110, HOST_H1, }, { SDB_HOST, "h1", NULL, "age >= 0s", /* always matches */ - 0, SDB_CONNECTION_DATA, 851, HOST_H1, + 0, SDB_CONNECTION_DATA, 1110, HOST_H1, }, { SDB_HOST, "h1", NULL, "age < 0s", /* never matches */ @@ -260,11 +274,11 @@ static struct { /* services */ { SDB_SERVICE, "h2", "s1", NULL, - 0, SDB_CONNECTION_DATA, 218, SERVICE_H2_S1, + 0, SDB_CONNECTION_DATA, 356, SERVICE_H2_S1, }, { SDB_SERVICE, "h2", "s1", "age >= 0s", /* always matches */ - 0, SDB_CONNECTION_DATA, 218, SERVICE_H2_S1, + 0, SDB_CONNECTION_DATA, 356, SERVICE_H2_S1, }, { SDB_SERVICE, "h2", "s1", "age < 0s", /* never matches */ @@ -289,11 +303,11 @@ static struct { /* metrics */ { SDB_METRIC, "h1", "m1", NULL, - 0, SDB_CONNECTION_DATA, 368, METRIC_H1_M1, + 0, SDB_CONNECTION_DATA, 489, METRIC_H1_M1, }, { SDB_METRIC, "h1", "m1", "age >= 0s", /* always matches */ - 0, SDB_CONNECTION_DATA, 368, METRIC_H1_M1, + 0, SDB_CONNECTION_DATA, 489, METRIC_H1_M1, }, { SDB_METRIC, "h1", "m1", "age < 0s", /* never matches */ -- 2.30.2