From: Florian Forster Date: Fri, 29 Dec 2023 16:07:24 +0000 (+0100) Subject: write_prometheus plugin: Remove invalid characers from the unit. X-Git-Tag: 6.0.0-rc0~20^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F4220%2Fhead;p=thirdparty%2Fcollectd.git write_prometheus plugin: Remove invalid characers from the unit. --- diff --git a/src/write_prometheus.c b/src/write_prometheus.c index f4f28eb02..fd663c4d0 100644 --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@ -263,7 +263,8 @@ void format_metric_family_name(strbuf_t *buf, metric_family_t const *fam) { if (unit != NULL) { strbuf_printf(buf, "_%s", unit->prometheus); } else if (fam->unit != NULL && fam->unit[0] != '{') { - strbuf_printf(buf, "_%s", fam->unit); + strbuf_print(buf, "_"); + strbuf_print_restricted(buf, fam->unit, VALID_NAME_CHARS, '_'); } if (fam->type == METRIC_TYPE_COUNTER) { diff --git a/src/write_prometheus_test.c b/src/write_prometheus_test.c index 61d1de5cf..386d1eabe 100644 --- a/src/write_prometheus_test.c +++ b/src/write_prometheus_test.c @@ -108,14 +108,15 @@ DEF_TEST(format_metric_family_name) { .unit = "%", .want = "storage_filesystem_utilization_percent", }, - /* Not yet supported: { .name = "astro.light.speed", .type = METRIC_TYPE_GAUGE, .unit = "m/s", + .want = "astro_light_speed_m_s", + /* Not yet supported. Should be: .want = "astro_light_speed_meters_per_second", + */ }, - */ }; for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {