]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
value_list: Improve generated metric names.
authorFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 09:19:25 +0000 (10:19 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 22:32:14 +0000 (23:32 +0100)
This changes `plugin_value_list_to_metric_family` to generate metric
names that are more in line with OpenTelemetry's naming guidelines.

src/utils/value_list/value_list.c

index 772328a436e4f9ddf71fccd1a30354ec59a04bd9..72703489d994e0480fd67810da80ec9c78dc4a76 100644 (file)
@@ -187,30 +187,6 @@ plugin_dispatch_multivalue(value_list_t const *template, /* {{{ */
   return failed;
 } /* }}} int plugin_dispatch_multivalue */
 
-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;
-}
-
 int parse_identifier(char *str, char **ret_host, char **ret_plugin,
                      char **ret_type, char **ret_data_source,
                      char *default_host) {
@@ -369,10 +345,21 @@ 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_set_t const *ds, size_t index) {
+  int status = strbuf_printf(buf, "collectd.v5.%s", vl->type);
+
+  if (ds->ds_num > 1) {
+    status = status || strbuf_printf(buf, ".%s", ds->ds[index].name);
+  }
+
+  return status;
+}
+
 EXPORT 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)) {
+  if ((vl == NULL) || (ds == NULL) || ds->ds_num <= index) {
     errno = EINVAL;
     return NULL;
   }
@@ -382,20 +369,19 @@ plugin_value_list_to_metric_family(value_list_t const *vl, data_set_t const *ds,
     return NULL;
   }
 
-  data_source_t const *dsrc = ds->ds + index;
-  strbuf_t name = STRBUF_CREATE;
-  int status = metric_family_name(&name, vl, dsrc);
+  strbuf_t buf = STRBUF_CREATE;
+  int status = metric_family_name(&buf, vl, ds, index);
   if (status != 0) {
-    STRBUF_DESTROY(name);
+    STRBUF_DESTROY(buf);
     metric_family_free(fam);
     errno = status;
     return NULL;
   }
-  fam->name = name.ptr;
-  name = (strbuf_t){0};
+  fam->name = strdup(buf.ptr);
+  STRBUF_DESTROY(buf);
 
   fam->type =
-      (dsrc->type == DS_TYPE_GAUGE) ? METRIC_TYPE_GAUGE : METRIC_TYPE_COUNTER;
+      (ds->ds[index].type == DS_TYPE_GAUGE) ? METRIC_TYPE_GAUGE : METRIC_TYPE_COUNTER;
 
   metric_t m = {
       .family = fam,
@@ -404,8 +390,14 @@ plugin_value_list_to_metric_family(value_list_t const *vl, data_set_t const *ds,
       .interval = vl->interval,
   };
 
-  status = metric_label_set(&m, "instance",
-                            (strlen(vl->host) != 0) ? vl->host : hostname_g);
+  /* If host is empty, this triggered the "use local default value" behavior. We
+   * emulate this behavior by not setting any resource attributes, which will
+   * also trigger the default behavior. */
+  if (strlen(vl->host) != 0) {
+    status = status || metric_family_resource_attribute_update(fam, "service.name", "collectd 5");
+    status = status || metric_family_resource_attribute_update(fam, "host.name", vl->host);
+  }
+
   if (strlen(vl->plugin_instance) != 0) {
     status = status || metric_label_set(&m, vl->plugin, vl->plugin_instance);
   }