]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
daemon, common: Move plugin_value_list_to_metric_family().
authorFlorian Forster <octo@google.com>
Thu, 30 Jul 2020 16:35:00 +0000 (18:35 +0200)
committerFlorian Forster <octo@google.com>
Mon, 10 Aug 2020 06:48:04 +0000 (08:48 +0200)
src/daemon/plugin.c
src/daemon/plugin.h
src/utils/common/common.c
src/utils/common/common.h

index 066bcce637aaf1e638da6afd7fac2a23000ce12d..92a4c7a98964d0aaa8cb03e6f9b9b4396126f0e4 100644 (file)
@@ -1526,89 +1526,6 @@ static int compare_read_func_group(llentry_t *e, void *ud) /* {{{ */
   return strcmp(rf->rf_group, (const char *)group);
 } /* }}} int compare_read_func_group */
 
-static int metric_family_name(strbuf_t *buf, value_list_t const *vl,
-                              data_source_t const *dsrc) {
-  int status = strbuf_print(buf, "collectd");
-
-  if (strcmp(vl->plugin, vl->type) != 0) {
-    status = status || strbuf_print(buf, "_");
-    status = status || strbuf_print(buf, vl->plugin);
-  }
-
-  status = status || strbuf_print(buf, "_");
-  status = status || strbuf_print(buf, vl->type);
-
-  if (strcmp("value", dsrc->name) != 0) {
-    status = status || strbuf_print(buf, "_");
-    status = status || strbuf_print(buf, dsrc->name);
-  }
-
-  if ((dsrc->type == DS_TYPE_COUNTER) || (dsrc->type == DS_TYPE_DERIVE)) {
-    status = status || strbuf_print(buf, "_total");
-  }
-
-  return status;
-}
-
-metric_family_t *plugin_value_list_to_metric_family(value_list_t const *vl,
-                                                    data_set_t const *ds,
-                                                    size_t index) {
-  if ((vl == NULL) || (ds == NULL)) {
-    errno = EINVAL;
-    return NULL;
-  }
-
-  metric_family_t *fam = calloc(1, sizeof(*fam));
-  if (fam == NULL) {
-    return NULL;
-  }
-
-  data_source_t const *dsrc = ds->ds + index;
-  strbuf_t name = STRBUF_CREATE;
-  int status = metric_family_name(&name, vl, dsrc);
-  if (status != 0) {
-    STRBUF_DESTROY(name);
-    metric_family_free(fam);
-    errno = status;
-    return NULL;
-  }
-  fam->name = name.ptr;
-  name = (strbuf_t){0};
-
-  fam->type =
-      (dsrc->type == DS_TYPE_GAUGE) ? METRIC_TYPE_GAUGE : METRIC_TYPE_COUNTER;
-
-  metric_t m = {
-      .family = fam,
-      .value = vl->values[index],
-      .time = vl->time,
-      .interval = vl->interval,
-  };
-
-  status = metric_label_set(&m, "instance",
-                            (strlen(vl->host) != 0) ? vl->host : hostname_g);
-  if (strlen(vl->plugin_instance) != 0) {
-    status = status || metric_label_set(&m, vl->plugin, vl->plugin_instance);
-  }
-  if (strlen(vl->type_instance) != 0) {
-    char const *name = "type";
-    if (strlen(vl->plugin_instance) == 0) {
-      name = vl->plugin;
-    }
-    status = status || metric_label_set(&m, name, vl->type_instance);
-  }
-
-  status = status || metric_family_metric_append(fam, m);
-  if (status != 0) {
-    metric_reset(&m);
-    metric_family_free(fam);
-    errno = status;
-    return NULL;
-  }
-
-  return fam;
-}
-
 EXPORT int plugin_unregister_read_group(const char *group) /* {{{ */
 {
   llentry_t *le;
index 96f75d691e922c85bd51c0215469d5827cc66430..faec3970577ee3e35c36675452f99ce6b2c21195 100644 (file)
@@ -327,13 +327,6 @@ int plugin_unregister_notification(const char *name);
  */
 void plugin_log_available_writers(void);
 
-/* plugin_value_list_to_metric_family converts a value in a value_list_t to a
- * metric_family_t. In case of error, errno is set and NULL is returned. The
- * returned pointer must be freed using metric_family_free(). */
-metric_family_t *plugin_value_list_to_metric_family(value_list_t const *vl,
-                                                    data_set_t const *ds,
-                                                    size_t index);
-
 /*
  * NAME
  *  plugin_dispatch_values
index a4597d8536ca07b32411320b83fda6694811a710..60c404570f4045fe8a497898c7e0ee1a7026ab9e 100644 (file)
@@ -1087,6 +1087,89 @@ metric_t *parse_legacy_identifier(char const *s) {
   return fam->metric.ptr;
 }
 
+static int metric_family_name(strbuf_t *buf, value_list_t const *vl,
+                              data_source_t const *dsrc) {
+  int status = strbuf_print(buf, "collectd");
+
+  if (strcmp(vl->plugin, vl->type) != 0) {
+    status = status || strbuf_print(buf, "_");
+    status = status || strbuf_print(buf, vl->plugin);
+  }
+
+  status = status || strbuf_print(buf, "_");
+  status = status || strbuf_print(buf, vl->type);
+
+  if (strcmp("value", dsrc->name) != 0) {
+    status = status || strbuf_print(buf, "_");
+    status = status || strbuf_print(buf, dsrc->name);
+  }
+
+  if ((dsrc->type == DS_TYPE_COUNTER) || (dsrc->type == DS_TYPE_DERIVE)) {
+    status = status || strbuf_print(buf, "_total");
+  }
+
+  return status;
+}
+
+metric_family_t *plugin_value_list_to_metric_family(value_list_t const *vl,
+                                                    data_set_t const *ds,
+                                                    size_t index) {
+  if ((vl == NULL) || (ds == NULL)) {
+    errno = EINVAL;
+    return NULL;
+  }
+
+  metric_family_t *fam = calloc(1, sizeof(*fam));
+  if (fam == NULL) {
+    return NULL;
+  }
+
+  data_source_t const *dsrc = ds->ds + index;
+  strbuf_t name = STRBUF_CREATE;
+  int status = metric_family_name(&name, vl, dsrc);
+  if (status != 0) {
+    STRBUF_DESTROY(name);
+    metric_family_free(fam);
+    errno = status;
+    return NULL;
+  }
+  fam->name = name.ptr;
+  name = (strbuf_t){0};
+
+  fam->type =
+      (dsrc->type == DS_TYPE_GAUGE) ? METRIC_TYPE_GAUGE : METRIC_TYPE_COUNTER;
+
+  metric_t m = {
+      .family = fam,
+      .value = vl->values[index],
+      .time = vl->time,
+      .interval = vl->interval,
+  };
+
+  status = metric_label_set(&m, "instance",
+                            (strlen(vl->host) != 0) ? vl->host : hostname_g);
+  if (strlen(vl->plugin_instance) != 0) {
+    status = status || metric_label_set(&m, vl->plugin, vl->plugin_instance);
+  }
+  if (strlen(vl->type_instance) != 0) {
+    char const *name = "type";
+    if (strlen(vl->plugin_instance) == 0) {
+      name = vl->plugin;
+    }
+    status = status || metric_label_set(&m, name, vl->type_instance);
+  }
+
+  status = status || metric_family_metric_append(fam, m);
+  if (status != 0) {
+    metric_reset(&m);
+    metric_family_free(fam);
+    errno = status;
+    return NULL;
+  }
+
+  return fam;
+}
+
 int parse_value(const char *value_orig, value_t *ret_value, int ds_type) {
   char *value;
   char *endptr = NULL;
index d7fd83e0508c265f80c00eb6f61a55b6279afa28..b7c9c08cacb7074c303938fb39c4ec949be6fe0f 100644 (file)
@@ -346,6 +346,13 @@ int parse_identifier_vl(const char *str, value_list_t *vl,
  * "host/plugin/type" and converts it to a metric_t. */
 metric_t *parse_legacy_identifier(char const *s);
 
+/* plugin_value_list_to_metric_family converts a value in a value_list_t to a
+ * metric_family_t. In case of error, errno is set and NULL is returned. The
+ * returned pointer must be freed using metric_family_free(). */
+metric_family_t *plugin_value_list_to_metric_family(value_list_t const *vl,
+                                                    data_set_t const *ds,
+                                                    size_t index);
+
 int parse_value(const char *value, value_t *ret_value, int ds_type);
 int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds);