]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_prometheus plugin: Let "job" default to hostname_g, and "instance" to an empty...
authorFlorian Forster <octo@collectd.org>
Wed, 21 Feb 2024 15:20:30 +0000 (16:20 +0100)
committerFlorian Forster <octo@collectd.org>
Wed, 21 Feb 2024 15:20:30 +0000 (16:20 +0100)
src/write_prometheus.c

index 75d4acb1ff0a28b48db5ad7b750320b6b1003ff5..8ede62d7d5423be7b767ae444fab816d74fab9c7 100644 (file)
@@ -197,20 +197,20 @@ static int format_label_pair(strbuf_t *buf, label_pair_t l, bool *first_label) {
 
 static int format_label_set(strbuf_t *buf, label_set_t labels, char const *job,
                             char const *instance) {
-  bool first_label = true;
-  int status = 0;
-
-  if (job != NULL) {
-    status =
-        status || format_label_pair(buf, (label_pair_t){"job", (char *)job},
-                                    &first_label);
+  if (job == NULL) {
+    job = hostname_g;
   }
-  if (instance != NULL) {
-    status = status || format_label_pair(
-                           buf, (label_pair_t){"instance", (char *)instance},
-                           &first_label);
+  if (instance == NULL) {
+    instance = "";
   }
 
+  bool first_label = true;
+  int status =
+      format_label_pair(buf, (label_pair_t){"job", (char *)job},
+                        &first_label) ||
+      format_label_pair(buf, (label_pair_t){"instance", (char *)instance},
+                        &first_label);
+
   for (size_t i = 0; i < labels.num; i++) {
     status = status || format_label_pair(buf, labels.ptr[i], &first_label);
   }
@@ -434,11 +434,17 @@ static void target_info(strbuf_t *buf,
       job = strdup(v);
       label_set_update(resource, "service.name", NULL);
     }
+    if (job == NULL) {
+      job = strdup(hostname_g);
+    }
 
     if ((v = label_set_get(*resource, "service.instance.id")) != NULL) {
       instance = strdup(v);
       label_set_update(resource, "service.instance.id", NULL);
     }
+    if (instance == NULL) {
+      instance = strdup("");
+    }
 
     strbuf_print(buf, "target_info{");
     format_label_set(buf, *resource, job, instance);