Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics-exporter-prometheus: properly sanitize metric names #290

Merged
merged 4 commits into from
Apr 4, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions metrics-exporter-prometheus/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@ pub fn write_metric_line<T, T2>(
/// [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;
gnuvince marked this conversation as resolved.
Show resolved Hide resolved
}

/// Sanitizes a label key to be valid under the Prometheus [data model].
Expand Down Expand Up @@ -226,7 +235,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);
Expand Down