|
| 1 | +use derive_more::Display; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use crate::prometheus::PrometheusSerializable; |
| 5 | + |
| 6 | +#[derive(Debug, Display, Clone, Eq, PartialEq, Default, Deserialize, Serialize, Hash, Ord, PartialOrd)] |
| 7 | +pub struct LabelName(String); |
| 8 | + |
| 9 | +impl LabelName { |
| 10 | + /// Creates a new `LabelName` instance. |
| 11 | + /// |
| 12 | + /// # Panics |
| 13 | + /// |
| 14 | + /// Panics if the provided name is empty. |
| 15 | + #[must_use] |
| 16 | + pub fn new(name: &str) -> Self { |
| 17 | + assert!( |
| 18 | + !name.is_empty(), |
| 19 | + "Label name cannot be empty. It must have at least one character." |
| 20 | + ); |
| 21 | + |
| 22 | + Self(name.to_owned()) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl PrometheusSerializable for LabelName { |
| 27 | + /// In Prometheus: |
| 28 | + /// |
| 29 | + /// - Labels may contain ASCII letters, numbers, as well as underscores. |
| 30 | + /// They must match the regex [a-zA-Z_][a-zA-Z0-9_]*. |
| 31 | + /// - Label names beginning with __ (two "_") are reserved for internal |
| 32 | + /// use. |
| 33 | + /// - Label values may contain any Unicode characters. |
| 34 | + /// - Labels with an empty label value are considered equivalent to |
| 35 | + /// labels that do not exist. |
| 36 | + /// |
| 37 | + /// The label name is changed: |
| 38 | + /// |
| 39 | + /// - If a label name starts with, or contains, an invalid character: |
| 40 | + /// replace character with underscore. |
| 41 | + /// - If th label name starts with two underscores: |
| 42 | + /// add additional underscore (three underscores total) |
| 43 | + fn to_prometheus(&self) -> String { |
| 44 | + // Replace invalid characters with underscore |
| 45 | + let processed: String = self |
| 46 | + .0 |
| 47 | + .chars() |
| 48 | + .enumerate() |
| 49 | + .map(|(i, c)| { |
| 50 | + if i == 0 { |
| 51 | + if c.is_ascii_alphabetic() || c == '_' { |
| 52 | + c |
| 53 | + } else { |
| 54 | + '_' |
| 55 | + } |
| 56 | + } else if c.is_ascii_alphanumeric() || c == '_' { |
| 57 | + c |
| 58 | + } else { |
| 59 | + '_' |
| 60 | + } |
| 61 | + }) |
| 62 | + .collect(); |
| 63 | + |
| 64 | + // If the label name starts with two underscores, add an additional |
| 65 | + if processed.starts_with("__") && !processed.starts_with("___") { |
| 66 | + format!("_{processed}") |
| 67 | + } else { |
| 68 | + processed |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + mod serialization_of_label_name_to_prometheus { |
| 75 | + use rstest::rstest; |
| 76 | + |
| 77 | + use crate::label::LabelName; |
| 78 | + use crate::prometheus::PrometheusSerializable; |
| 79 | + |
| 80 | + #[rstest] |
| 81 | + #[case("1 valid name", "valid_name", "valid_name")] |
| 82 | + #[case("2 leading underscore", "_leading_underscore", "_leading_underscore")] |
| 83 | + #[case("3 leading lowercase", "v123", "v123")] |
| 84 | + #[case("4 leading uppercase", "V123", "V123")] |
| 85 | + fn valid_names_in_prometheus(#[case] case: &str, #[case] input: &str, #[case] output: &str) { |
| 86 | + assert_eq!(LabelName::new(input).to_prometheus(), output, "{case} failed: {input:?}"); |
| 87 | + } |
| 88 | + |
| 89 | + #[rstest] |
| 90 | + #[case("1 invalid start 1", "9invalid_start", "_invalid_start")] |
| 91 | + #[case("2 invalid start 2", "@test", "_test")] |
| 92 | + #[case("3 invalid dash", "invalid-char", "invalid_char")] |
| 93 | + #[case("4 invalid spaces", "spaces are bad", "spaces_are_bad")] |
| 94 | + #[case("5 invalid special chars", "a!b@c#d$e%f^g&h*i(j)", "a_b_c_d_e_f_g_h_i_j_")] |
| 95 | + #[case("6 invalid colon", "my:metric/version", "my_metric_version")] |
| 96 | + #[case("7 all invalid characters", "!@#$%^&*()", "__________")] |
| 97 | + #[case("8 non_ascii_characters", "ñaca©", "_aca_")] |
| 98 | + fn names_that_need_changes_in_prometheus(#[case] case: &str, #[case] input: &str, #[case] output: &str) { |
| 99 | + assert_eq!(LabelName::new(input).to_prometheus(), output, "{case} failed: {input:?}"); |
| 100 | + } |
| 101 | + |
| 102 | + #[rstest] |
| 103 | + #[case("1 double underscore start", "__private", "___private")] |
| 104 | + #[case("2 double underscore only", "__", "___")] |
| 105 | + #[case("3 processed to double underscore", "^^name", "___name")] |
| 106 | + #[case("4 processed to double underscore after first char", "0__name", "___name")] |
| 107 | + fn names_starting_with_double_underscore(#[case] case: &str, #[case] input: &str, #[case] output: &str) { |
| 108 | + assert_eq!(LabelName::new(input).to_prometheus(), output, "{case} failed: {input:?}"); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + #[should_panic(expected = "Label name cannot be empty. It must have at least one character.")] |
| 113 | + fn empty_name() { |
| 114 | + let _name = LabelName::new(""); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments