]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
common: Create the metric_family_t and metric_t types.
authorFlorian Forster <octo@google.com>
Fri, 17 Jul 2020 11:12:33 +0000 (13:12 +0200)
committerFlorian Forster <octo@google.com>
Wed, 29 Jul 2020 11:40:03 +0000 (13:40 +0200)
A "metric family" is a set of metrics, all with the same name (but
different labels and/or label values). The identity is split between the
two types: the "metric family" holds the "name" while the "metric" holds
the labels. Likewise, the "metric family" holds the metric type, the
"metric" holds the metric value.

This commit contains all required changes to compile the daemon, but
pretty much everything else still fails to build. The tests have not yet
updated either.

The "cpu plugin" has been updated as a proof-of-concept implementation.

# Conflicts:
# src/daemon/plugin.h

src/utils/common/common.c
src/utils/common/common.h
src/utils/common/common_test.c

index d9f021f9857191feaab8b2722eca4eaae910ac00..4d768dbaf46544099bf03b75b6c44c476b7155d0 100644 (file)
@@ -904,15 +904,11 @@ int format_name(char *ret, int ret_len, const char *hostname,
   return 0;
 } /* int format_name */
 
-int format_values(char *ret, size_t ret_len, /* {{{ */
-                  metric_single_t const *m, bool store_rates) {
-  ret[0] = 0;
-  strbuf_t buf = STRBUF_CREATE_FIXED(ret, ret_len);
+int format_values(strbuf_t *buf, metric_t const *m, bool store_rates) {
+  strbuf_printf(buf, "%.3f", CDTIME_T_TO_DOUBLE(m->time));
 
-  strbuf_printf(&buf, "%.3f", CDTIME_T_TO_DOUBLE(m->time));
-
-  if (m->value_type == DS_TYPE_GAUGE)
-    strbuf_printf(&buf, ":" GAUGE_FORMAT, m->value.gauge);
+  if (m->family->type == METRIC_TYPE_GAUGE)
+    strbuf_printf(buf, ":" GAUGE_FORMAT, m->value.gauge);
   else if (store_rates) {
     gauge_t rates = NAN;
     int status = uc_get_rate(m, &rates);
@@ -920,43 +916,19 @@ int format_values(char *ret, size_t ret_len, /* {{{ */
       WARNING("format_values: uc_get_rate failed.");
       return status;
     }
-    strbuf_printf(&buf, ":" GAUGE_FORMAT, rates);
-  } else if (m->value_type == DS_TYPE_COUNTER)
-    strbuf_printf(&buf, ":%" PRIu64, (uint64_t)m->value.counter);
-  else if (m->value_type == DS_TYPE_DERIVE)
-    strbuf_printf(&buf, ":%" PRIi64, m->value.derive);
-  else {
-    ERROR("format_values: Unknown data source type: %i", m->value_type);
+    strbuf_printf(buf, ":" GAUGE_FORMAT, rates);
+  } else if (m->family->type == METRIC_TYPE_COUNTER) {
+    strbuf_printf(buf, ":%" PRIu64, m->value.counter);
+  } else if (m->family->type == DS_TYPE_DERIVE) {
+    strbuf_printf(buf, ":%" PRIi64, m->value.derive);
+  else {
+    ERROR("format_values: Unknown metric type: %d", m->family->type);
     return -1;
   }
 
   return 0;
 } /* }}} int format_values */
 
-int format_values_vl(char *ret, size_t ret_len, /* {{{ */
-                     const data_set_t *ds, const value_list_t *vl,
-                     bool store_rates) {
-  metrics_list_t *ml = NULL;
-  assert(0 == strcmp(ds->type, vl->type));
-  int retval = plugin_convert_values_to_metrics(vl, &ml);
-  if (retval != 0) {
-    return retval;
-  }
-  metrics_list_t *index_p = ml;
-  while (index_p != NULL) {
-    retval = format_values(ret, ret_len, &index_p->metric, store_rates);
-    if (retval != 0) {
-      destroy_metrics_list(ml);
-      return retval;
-    }
-    ret[strlen(ret)] = '\n';
-    ret_len -= strlen(ret) + 1;
-    index_p = index_p->next_p;
-  }
-  destroy_metrics_list(ml);
-  return 0;
-}
-
 int parse_identifier(char *str, char **ret_host, char **ret_plugin,
                      char **ret_type, char **ret_data_source,
                      char *default_host) {
@@ -1233,7 +1205,7 @@ int notification_init(notification_t *n, int severity, const char *message,
 } /* int notification_init */
 
 int notification_init_metric(notification_t *n, int severity,
-                             char const *message, metric_single_t const *m) {
+                             char const *message, metric_t const *m) {
   if ((n == NULL) || (message == NULL) || (m == NULL)) {
     return EINVAL;
   }
@@ -1349,9 +1321,8 @@ int rate_to_value(value_t *ret_value, gauge_t rate, /* {{{ */
     return 0;
   }
 
-  /* Counter and absolute can't handle negative rates. Reset "last time"
-   * to zero, so that the next valid rate will re-initialize the
-   * structure. */
+  /* Counter can't handle negative rates. Reset "last time" to zero, so that
+   * the next valid rate will re-initialize the structure. */
   if ((rate < 0.0) && (ds_type == DS_TYPE_COUNTER)) {
     memset(state, 0, sizeof(*state));
     return EINVAL;
index cf0b43d93c13c07ac44f1c2bbacdd165f73e19c3..1cb0df7642b9cec8baf12faecb081dcdb016982b 100644 (file)
@@ -323,10 +323,7 @@ int format_name(char *ret, int ret_len, const char *hostname,
 #define FORMAT_VL(ret, ret_len, vl)                                            \
   format_name(ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance,   \
               (vl)->type, (vl)->type_instance)
-int format_values(char *ret, size_t ret_len, metric_single_t const *m,
-                  bool store_rates);
-int format_values_vl(char *ret, size_t ret_len, const data_set_t *ds,
-                     const value_list_t *vl, bool store_rates);
+int format_values(strbuf_t *buf, metric_t const *m, bool store_rates);
 
 int parse_identifier(char *str, char **ret_host, char **ret_plugin,
                      char **ret_type, char **ret_data_source,
@@ -348,7 +345,7 @@ int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen,
 #endif
 
 int notification_init_metric(notification_t *n, int severity,
-                             const char *message, metric_single_t const *m);
+                             const char *message, metric_t const *m);
 
 int notification_init(notification_t *n, int severity, const char *message,
                       const char *host, const char *plugin,
index e3a42466614fbf2784fb362fd9b8f2fc563e7c69..f2f3888831b641890435f247a815b668509bad46 100644 (file)
@@ -377,37 +377,34 @@ DEF_TEST(value_to_rate) {
 
 DEF_TEST(format_values) {
   struct {
-    int ds_type;
+    metric_type_t type;
     value_t value;
     char const *want;
   } cases[] = {
-      {DS_TYPE_GAUGE, (value_t){.gauge = 47.11}, "1592558427.435:47.11"},
-      {DS_TYPE_GAUGE, (value_t){.gauge = NAN}, "1592558427.435:nan"},
-      {DS_TYPE_DERIVE, (value_t){.derive = 42}, "1592558427.435:42"},
-      {DS_TYPE_COUNTER, (value_t){.counter = 18446744073709551615LLU},
+      {METRIC_TYPE_GAUGE, (value_t){.gauge = 47.11}, "1592558427.435:47.11"},
+      {METRIC_TYPE_GAUGE, (value_t){.gauge = NAN}, "1592558427.435:nan"},
+      {METRIC_TYPE_COUNTER, (value_t){.counter = 18446744073709551615LLU},
        "1592558427.435:18446744073709551615"},
   };
 
   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
-    char buf[1024];
-
-    data_set_t ds = {
-        .type = "testing",
-        .ds_num = 1,
-        .ds =
-            &(data_source_t){
-                .type = cases[i].ds_type,
-            },
-    };
-    value_list_t vl = {
-        .type = "testing",
-        .values = &cases[i].value,
-        .values_len = 1,
-        .time = 1709996590700628541,
-    };
-
-    EXPECT_EQ_INT(0, format_values(buf, sizeof(buf), &ds, &vl, false));
+    metric_family_t fam =
+        {
+            .name = "testing",
+            .type = cases[i].type,
+        } metric_t m = {
+            .family = &fam,
+            .value = cases[i].value,
+            .time = MS_TO_CDTIME_T(1592558427435),
+        };
+    metric_list_add(&fam.metric, m);
+
+    strbuf_t buf = STRBUF_CREATE;
+
+    EXPECT_EQ_INT(0, format_values(&buf, &m, false));
     EXPECT_EQ_STR(cases[i].want, buf);
+
+    STRBUF_DESTROY(buf);
   }
 
   return 0;