From: Florian Forster Date: Wed, 3 Jan 2024 14:59:53 +0000 (+0100) Subject: format_json: Add the `unit` field to the OpenTelemetry output. X-Git-Tag: 6.0.0-rc0~18^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0a92ff35a897e25ab4964bc5579b5c590b57d530;p=thirdparty%2Fcollectd.git format_json: Add the `unit` field to the OpenTelemetry output. --- diff --git a/src/utils/format_json/format_json_test.c b/src/utils/format_json/format_json_test.c index 5479bdb9a..528be87c0 100644 --- a/src/utils/format_json/format_json_test.c +++ b/src/utils/format_json/format_json_test.c @@ -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}]}}]}]}]}", diff --git a/src/utils/format_json/open_telemetry.c b/src/utils/format_json/open_telemetry.c index 099b495c0..195e48b52 100644 --- a/src/utils/format_json/open_telemetry.c +++ b/src/utils/format_json/open_telemetry.c @@ -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"));