From cc2c2890f5272a129b6d29f4870c61181f15c152 Mon Sep 17 00:00:00 2001 From: Vincent Foley Date: Fri, 18 Mar 2022 09:42:30 -0400 Subject: [PATCH 1/3] Add failing test from #289 --- metrics-exporter-prometheus/src/formatting.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/metrics-exporter-prometheus/src/formatting.rs b/metrics-exporter-prometheus/src/formatting.rs index f95f6d3e..3e90a304 100644 --- a/metrics-exporter-prometheus/src/formatting.rs +++ b/metrics-exporter-prometheus/src/formatting.rs @@ -226,7 +226,13 @@ mod tests { #[test] fn test_sanitize_metric_name_known_cases() { - let cases = &[("*", "_"), ("\"", "_"), ("foo_bar", "foo_bar"), ("1foobar", "_foobar")]; + let cases = &[ + ("*", "_"), + ("\"", "_"), + ("foo_bar", "foo_bar"), + ("foo1_bar", "foo1_bar"), + ("1foobar", "_foobar"), + ]; for (input, expected) in cases { let result = sanitize_metric_name(input); From b3776ff701373de3d0a107eba97d85558b35ca60 Mon Sep 17 00:00:00 2001 From: Vincent Foley Date: Fri, 18 Mar 2022 14:29:02 -0400 Subject: [PATCH 2/3] Fix metrics name sanitization --- metrics-exporter-prometheus/src/formatting.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/metrics-exporter-prometheus/src/formatting.rs b/metrics-exporter-prometheus/src/formatting.rs index 3e90a304..89d8e5bf 100644 --- a/metrics-exporter-prometheus/src/formatting.rs +++ b/metrics-exporter-prometheus/src/formatting.rs @@ -110,8 +110,17 @@ pub fn write_metric_line( /// [data model]: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels pub fn sanitize_metric_name(name: &str) -> String { // The first character must be [a-zA-Z_:], and all subsequent characters must be [a-zA-Z0-9_:]. - name.replacen(invalid_metric_name_start_character, "_", 1) - .replace(invalid_metric_name_character, "_") + let mut out = String::with_capacity(name.len()); + let mut is_invalid: fn(char) -> bool = invalid_metric_name_start_character; + for c in name.chars() { + if is_invalid(c) { + out.push('_'); + } else { + out.push(c); + } + is_invalid = invalid_metric_name_character; + } + return out; } /// Sanitizes a label key to be valid under the Prometheus [data model]. From 6798fba4f0326d6a4d40fc9232f0dfe170854f1e Mon Sep 17 00:00:00 2001 From: Vincent Foley Date: Sat, 19 Mar 2022 13:06:59 -0400 Subject: [PATCH 3/3] Update metrics-exporter-prometheus/src/formatting.rs Co-authored-by: Toby Lawrence --- metrics-exporter-prometheus/src/formatting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics-exporter-prometheus/src/formatting.rs b/metrics-exporter-prometheus/src/formatting.rs index 89d8e5bf..b0fd4b78 100644 --- a/metrics-exporter-prometheus/src/formatting.rs +++ b/metrics-exporter-prometheus/src/formatting.rs @@ -120,7 +120,7 @@ pub fn sanitize_metric_name(name: &str) -> String { } is_invalid = invalid_metric_name_character; } - return out; + out } /// Sanitizes a label key to be valid under the Prometheus [data model].