]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_prometheus plugin: Translate between resource attributes and target labels.
authorFlorian Forster <octo@collectd.org>
Fri, 22 Dec 2023 22:39:58 +0000 (23:39 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 28 Dec 2023 19:13:31 +0000 (20:13 +0100)
* `service.name` → `job`
* `service.instance.id` → `instance`

src/write_prometheus.c
src/write_prometheus_test.c

index 9c1040fcd4e33d5b4c40a6e5bf9d42fbfb512b13..8a0e35f7e3f2de8b6c7d30e1f5222df778effdc4 100644 (file)
@@ -77,17 +77,27 @@ static struct MHD_Daemon *httpd;
 static cdtime_t staleness_delta = PROMETHEUS_DEFAULT_STALENESS_DELTA;
 
 static int format_label_set(strbuf_t *buf, label_set_t const *labels,
-                            char const *prefix, bool first_label) {
+                            char const *prefix, bool translate,
+                            bool first_label) {
   int status = 0;
   for (size_t i = 0; i < labels->num; i++) {
     if (!first_label) {
       status = status || strbuf_print(buf, ",");
     }
 
+    char const *name = labels->ptr[i].name;
+    if (translate) {
+      if (strcmp("service.name", name) == 0) {
+        name = "job";
+      } else if (strcmp("service.instance.id", name) == 0) {
+        name = "instance";
+      }
+    }
+
     status =
         status || strbuf_print_restricted(buf, prefix, VALID_LABEL_CHARS, '_');
-    status = status || strbuf_print_restricted(buf, labels->ptr[i].name,
-                                               VALID_LABEL_CHARS, '_');
+    status =
+        status || strbuf_print_restricted(buf, name, VALID_LABEL_CHARS, '_');
     status = status || strbuf_print(buf, "=\"");
     status = status || strbuf_print_escaped(buf, labels->ptr[i].value,
                                             "\\\"\n\r\t", '\\');
@@ -114,10 +124,10 @@ static int format_metric(strbuf_t *buf, metric_t const *m) {
   bool first_label = true;
   if (resource->num != 0) {
     status = status || format_label_set(buf, resource, RESOURCE_LABEL_PREFIX,
-                                        first_label);
+                                        true, first_label);
     first_label = false;
   }
-  status = status || format_label_set(buf, &m->label, "", first_label);
+  status = status || format_label_set(buf, &m->label, "", false, first_label);
 
   return status || strbuf_print(buf, "}");
 }
@@ -186,16 +196,9 @@ void target_info(strbuf_t *buf, label_set_t resource) {
   strbuf_print(buf, "# TYPE target info\n");
   strbuf_print(buf, "# HELP target Target metadata\n");
 
-  metric_t m = {
-      .family =
-          &(metric_family_t){
-              .name = "target_info",
-          },
-      .label = resource,
-  };
-  format_metric(buf, &m);
-
-  strbuf_print(buf, " 1\n");
+  strbuf_print(buf, "target_info{");
+  format_label_set(buf, &resource, "", true, true);
+  strbuf_print(buf, "} 1\n");
 }
 
 static void format_text(strbuf_t *buf) {
index 54a67ca783212e4763042a7209b524ef47832ac4..83aab7ee8ddfae1a3b46efa5877ffcee2f42b531 100644 (file)
@@ -169,6 +169,28 @@ DEF_TEST(target_info) {
                   "# HELP target Target metadata\n"
                   "target_info{foo=\"bar\"} 1\n",
       },
+      {
+          .name = "service.name gets translated to job",
+          .resource =
+              {
+                  .ptr = &(label_pair_t){"service.name", "unittest"},
+                  .num = 1,
+              },
+          .want = "# TYPE target info\n"
+                  "# HELP target Target metadata\n"
+                  "target_info{job=\"unittest\"} 1\n",
+      },
+      {
+          .name = "service.instance.id gets translated to instance",
+          .resource =
+              {
+                  .ptr = &(label_pair_t){"service.instance.id", "42"},
+                  .num = 1,
+              },
+          .want = "# TYPE target info\n"
+                  "# HELP target Target metadata\n"
+                  "target_info{instance=\"42\"} 1\n",
+      },
   };
 
   for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {