]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
format_json: Add the `unit` field to the OpenTelemetry output.
authorFlorian Forster <octo@collectd.org>
Wed, 3 Jan 2024 14:59:53 +0000 (15:59 +0100)
committerFlorian Forster <octo@collectd.org>
Wed, 3 Jan 2024 15:39:36 +0000 (16:39 +0100)
src/utils/format_json/format_json_test.c
src/utils/format_json/open_telemetry.c

index 5479bdb9a0dcec03ae177ca587820bf045bb8c7a..528be87c0bfdb3688675beeece4e48b8889a1bbe 100644 (file)
@@ -293,11 +293,13 @@ DEF_TEST(open_telemetry) {
       &(metric_family_t){
           .name = "unit.tests",
           .help = "Example gauge metric",
+          .unit = "1",
           .type = METRIC_TYPE_GAUGE,
       },
       &(metric_family_t){
           .name = "unit.test.count",
           .help = "Example counter metric",
+          .unit = "{test}",
           .type = METRIC_TYPE_COUNTER,
       },
   };
@@ -321,12 +323,14 @@ DEF_TEST(open_telemetry) {
       "{\"resourceMetrics\":[{\"resource\":{\"attributes\":[{\"key\":\"service."
       "name\",\"value\":{\"stringValue\":\"unit "
       "test\"}}]},\"scopeMetrics\":[{\"scope\":{\"name\":\"collectd\","
-      "\"version\":\"" PACKAGE_VERSION "\"},\"metrics\":[{\"name\":\"unit."
-      "test.count\",\"description\":\"Example counter "
+      "\"version\":\"" PACKAGE_VERSION
+      "\"},\"metrics\":[{\"name\":\"unit.test.count\",\"unit\":\"{test}\","
+      "\"description\":\"Example counter "
       "metric\",\"sum\":{\"dataPoints\":[{\"attributes\":[{\"key\":\"metric."
       "label\",\"value\":{\"stringValue\":\"bar\"}}],\"timeUnixNano\":0,"
       "\"asInt\":31337}],\"aggregationTemporality\":\"2\",\"isMonotonic\":true}"
-      "},{\"name\":\"unit.tests\",\"description\":\"Example gauge "
+      "},{\"name\":\"unit.tests\",\"unit\":\"1\",\"description\":\"Example "
+      "gauge "
       "metric\",\"gauge\":{\"dataPoints\":[{\"attributes\":[{\"key\":\"metric."
       "label\",\"value\":{\"stringValue\":\"test "
       "label\"}}],\"timeUnixNano\":0,\"asDouble\":42.0}]}}]}]}]}",
index 099b495c08de2a59e69d1c72f3aac18fe79d8a1c..195e48b52234c727c66ba0d846bff071f15d194b 100644 (file)
@@ -150,9 +150,10 @@ static int metric(yajl_gen g, metric_family_t const *fam) {
   CHECK(json_add_string(g, "name"));
   CHECK(json_add_string(g, fam->name));
 
-  // TODO(octo): populate the "unit" field.
-  // CHECK(json_add_string(g, "unit"));
-  // CHECK(json_add_string(g, "1"));
+  if (fam->unit != NULL) {
+    CHECK(json_add_string(g, "unit"));
+    CHECK(json_add_string(g, fam->unit));
+  }
 
   if (fam->help != NULL) {
     CHECK(json_add_string(g, "description"));