From 983c13c91a8554c0e0afd806590154f34c4f6e09 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 29 Dec 2023 17:07:24 +0100 Subject: [PATCH] write_prometheus plugin: Remove invalid characers from the unit. --- src/write_prometheus.c | 3 ++- src/write_prometheus_test.c | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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++) { -- 2.47.2