Code

plugin, store: Add service/metric "hostname" attributes centrally.
authorSebastian Harl <sh@tokkee.org>
Wed, 16 Sep 2015 19:33:05 +0000 (21:33 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 16 Sep 2015 19:33:05 +0000 (21:33 +0200)
That is, let the respective sdb_plugin_store* functions do that instead of
requiring each store implementation to do so.

src/core/plugin.c
src/core/store.c
t/unit/core/store_expr_test.c
t/unit/core/store_json_test.c

index 235b8c7721ba97d08dc1b165c6ac0812ed7c6292..54f3f7f09f7f39f20c17fb0856db21df4fbd3e7d 100644 (file)
@@ -1508,9 +1508,10 @@ int
 sdb_plugin_store_service(const char *hostname, const char *name,
                sdb_time_t last_update)
 {
-       char *cname;
-
        sdb_llist_iter_t *iter;
+       sdb_data_t d;
+
+       char *cname;
        int status = 0;
 
        if ((! hostname) || (! name))
@@ -1539,6 +1540,16 @@ sdb_plugin_store_service(const char *hostname, const char *name,
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+
+       if (! status) {
+               /* record the hostname as an attribute */
+               d.type = SDB_TYPE_STRING;
+               d.data.string = cname;
+               if (sdb_plugin_store_service_attribute(cname, name,
+                                       "hostname", &d, last_update))
+                       status = -1;
+       }
+
        free(cname);
        return status;
 } /* sdb_plugin_store_service */
@@ -1547,9 +1558,10 @@ int
 sdb_plugin_store_metric(const char *hostname, const char *name,
                sdb_metric_store_t *store, sdb_time_t last_update)
 {
-       char *cname;
-
        sdb_llist_iter_t *iter;
+       sdb_data_t d;
+
+       char *cname;
        int status = 0;
 
        if ((! hostname) || (! name))
@@ -1581,6 +1593,16 @@ sdb_plugin_store_metric(const char *hostname, const char *name,
                        status = s;
        }
        sdb_llist_iter_destroy(iter);
+
+       if (! status) {
+               /* record the hostname as an attribute */
+               d.type = SDB_TYPE_STRING;
+               d.data.string = cname;
+               if (sdb_plugin_store_metric_attribute(cname, name,
+                                       "hostname", &d, last_update))
+                       status = -1;
+       }
+
        free(cname);
        return status;
 } /* sdb_plugin_store_metric */
index be8f9fa259406af1e31a2783560da81ca21776de..30b4d1c367cdc845714c98550501da3c1037f540 100644 (file)
@@ -654,7 +654,6 @@ store_service(const char *hostname, const char *name,
 
        sdb_host_t *host;
        sdb_avltree_t *services;
-       sdb_data_t d;
 
        int status = 0;
 
@@ -676,15 +675,6 @@ store_service(const char *hostname, const char *name,
 
        sdb_object_deref(SDB_OBJ(host));
        pthread_rwlock_unlock(&st->host_lock);
-
-       if (status)
-               return status;
-
-       /* record the hostname as an attribute */
-       d.type = SDB_TYPE_STRING;
-       d.data.string = SDB_OBJ(host)->name;
-       if (store_service_attr(hostname, name, "hostname", &d, last_update, user_data))
-               status = -1;
        return status;
 } /* store_service */
 
@@ -742,7 +732,6 @@ 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;
 
@@ -784,12 +773,6 @@ store_metric(const char *hostname, const char *name,
                if (store_metric_store(metric, store))
                        status = -1;
        pthread_rwlock_unlock(&st->host_lock);
-
-       /* record the hostname as an attribute */
-       d.type = SDB_TYPE_STRING;
-       d.data.string = SDB_OBJ(host)->name;
-       if (store_metric_attr(hostname, name, "hostname", &d, last_update, user_data))
-               status = -1;
        return status;
 } /* store_metric */
 
index d11452ae72dc072e0c5fac7330fd4b29b18e4969..3f21a82f9f21952dd684a6361d13deefac662aae 100644 (file)
@@ -29,7 +29,6 @@
 #      include "config.h"
 #endif
 
-#include "core/plugin.h"
 #include "core/store.h"
 #include "core/store-private.h"
 #include "parser/parser.h"
@@ -322,23 +321,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 },
-               }, 2,
+                       { 0 },
+               }, 1,
        },
        {
                { 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 },
-               }, 2,
+                       { 0 },
+               }, 1,
        },
        {
                { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true,
@@ -391,23 +390,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 },
-               }, 3,
+                       { 0 },
+               }, 2,
        },
        {
                { 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 },
-               }, 3,
+                       { 0 },
+               }, 2,
        },
        {
                { SDB_OBJECT_INIT, TYPED_EXPR, -1, &namer, NULL, ATTRS }, true,
index d887c439f0bddbb4c21572402d27e9d490245d24..f8ebac913633fe025df01344c2e2ecab521874cc 100644 (file)
@@ -29,7 +29,6 @@
 #      include "config.h"
 #endif
 
-#include "core/plugin.h"
 #include "core/store.h"
 #include "testutils.h"
 
@@ -173,9 +172,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k3\", \"value\": 42, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -183,12 +179,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]},"
                        "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
@@ -196,29 +187,16 @@ struct {
                                        "{\"name\": \"m1\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "], "
                                "\"services\": ["
                                        "{\"name\": \"s1\", "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]},"
+                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                        "{\"name\": \"s2\", "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []},"
@@ -254,9 +232,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -278,12 +253,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
@@ -306,19 +276,11 @@ struct {
                                "\"services\": ["
                                        "{\"name\": \"s1\", "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]},"
+                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                        "{\"name\": \"s2\", "
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []},"
@@ -353,9 +315,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k1\", \"value\": 123, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -377,9 +336,6 @@ struct {
                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                "\"update_interval\": \"0s\", \"backends\": [], "
                                                "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []},"
                                                        "{\"name\": \"k3\", \"value\": 42, "
                                                                "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
                                                                "\"update_interval\": \"0s\", \"backends\": []}"
@@ -387,12 +343,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]},"
                        "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
                                "\"update_interval\": \"0s\", \"backends\": [], "
@@ -400,12 +351,7 @@ struct {
                                        "{\"name\": \"m1\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h2\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { NULL, 0, SDB_DATA_INIT },
@@ -442,12 +388,7 @@ struct {
                                        "{\"name\": \"m2\", "
                                                "\"timeseries\": false, "
                                                "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                               "\"update_interval\": \"0s\", \"backends\": [], "
-                                               "\"attributes\": ["
-                                                       "{\"name\": \"hostname\", \"value\": \"h1\", "
-                                                               "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
-                                                               "\"update_interval\": \"0s\", \"backends\": []}"
-                                               "]}"
+                                               "\"update_interval\": \"0s\", \"backends\": []}"
                                "]}"
                "]" },
        { { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,