1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Metric label.
//!
//! # References
//!
//! - [Data model](https://prometheus.io/docs/concepts/data_model/)
//! - [Metric and label naming](https://prometheus.io/docs/practices/naming/)
use atomic_immut::AtomicImmut;
use std;
use std::fmt;
use std::ops::Deref;

use {ErrorKind, Result};

/// Metric label.
///
/// A label is a key-value pair.
///
/// Label names may contain ASCII letters, numbers, as well as underscores.
/// They must match the regex `[a-zA-Z_][a-zA-Z0-9_]*`.
/// Label names beginning with `__` are reserved for internal use.
///
/// Label values may contain any Unicode (utf-8) characters.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Label {
    name: String,
    value: String,
}
impl Label {
    /// Makes a new `Label` instance.
    ///
    /// # Errors
    ///
    /// If `name` contains invalid characters, this function returns `ErrorKind::InvalidInput` error.
    ///
    /// # Examples
    ///
    /// ```
    /// use prometrics::ErrorKind;
    /// use prometrics::label::Label;
    ///
    /// let label = Label::new("foo", "bar").unwrap();
    /// assert_eq!(label.name(), "foo");
    /// assert_eq!(label.value(), "bar");
    /// assert_eq!(label.to_string(), r#"foo="bar""#);
    ///
    /// // Reserved name
    /// assert_eq!(Label::new("__foo", "bar").err().map(|e| *e.kind()),
    ///            Some(ErrorKind::InvalidInput));
    //
    /// // Invalid name
    /// assert_eq!(Label::new("fo-o", "bar").err().map(|e| *e.kind()),
    ///            Some(ErrorKind::InvalidInput));
    /// ```
    pub fn new(name: &str, value: &str) -> Result<Self> {
        track!(
            Self::validate_name(name),
            "name={:?}, value={:?}",
            name,
            value
        )?;
        Ok(Label {
            name: name.to_string(),
            value: value.to_string(),
        })
    }

    /// Returns the name of this label.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the value of this label.
    pub fn value(&self) -> &str {
        &self.value
    }

    fn validate_name(name: &str) -> Result<()> {
        // REGEX: [a-zA-Z_][a-zA-Z0-9_]*
        track_assert!(!name.is_empty(), ErrorKind::InvalidInput);
        track_assert!(!name.starts_with("__"), ErrorKind::InvalidInput, "Reserved");
        match name.as_bytes()[0] as char {
            'a'..='z' | 'A'..='Z' | '_' => {}
            _ => track_panic!(ErrorKind::InvalidInput),
        }
        for c in name.chars().skip(1) {
            match c {
                'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {}
                _ => track_panic!(ErrorKind::InvalidInput),
            }
        }
        Ok(())
    }
}
impl fmt::Display for Label {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // > `label_value` can be any sequence of UTF-8 characters,
        // > but the backslash, the double-quote, and the line-feed
        // > characters have to be escaped as `\\`, `\"`, and `\n`, respectively.
        write!(f, "{}=\"", self.name)?;
        for c in self.value.chars() {
            match c {
                '\\' => write!(f, "\\\\")?,
                '\n' => write!(f, "\\\\n")?,
                '"' => write!(f, "\\\"")?,
                _ => write!(f, "{}", c)?,
            }
        }
        write!(f, "\"")
    }
}

/// A map of labels (i.e., key-value pairs).
#[derive(Debug)]
pub struct Labels(AtomicImmut<Vec<Label>>);
impl Labels {
    /// Returns the number of labels contained in this map.
    pub fn len(&self) -> usize {
        self.0.load().len()
    }

    /// Returns `true` if this map has no labels, otherwise `false`.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the label which has the name `name`.
    pub fn get(&self, name: &str) -> Option<&Label> {
        self.iter().find(|l| l.name() == name)
    }

    /// Returns an iterator which visiting all labels in this map.
    pub fn iter(&self) -> Iter {
        let labels = self.0.load();
        let inner = unsafe { std::mem::transmute(labels.iter()) };
        Iter { labels, inner }
    }

    pub(crate) fn new(labels: Vec<Label>) -> Self {
        Labels(AtomicImmut::new(labels))
    }
}
impl fmt::Display for Labels {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{{")?;
        for (i, label) in self.iter().enumerate() {
            if i != 0 {
                write!(f, ",")?;
            }
            write!(f, "{}", label)?;
        }
        write!(f, "}}")?;
        Ok(())
    }
}

/// A mutable map of labels (i.e., key-value pairs).
#[derive(Debug)]
pub struct LabelsMut<'a> {
    inner: &'a Labels,
    reserved: Option<&'static str>,
}
impl<'a> LabelsMut<'a> {
    /// Inserts the label.
    pub fn insert(&mut self, name: &str, value: &str) -> Result<()> {
        track_assert_ne!(
            self.reserved.map(|s| &*s),
            Some(name),
            ErrorKind::InvalidInput
        );
        let label = track!(Label::new(name, value))?;
        self.inner.0.update(move |labels| {
            let mut labels = labels.clone();
            labels.retain(|l| l.name != label.name);
            labels.push(label.clone());
            labels.sort();
            labels
        });
        Ok(())
    }

    /// Removes the label which has the name `name` if it exists.
    pub fn remove(&mut self, name: &str) {
        self.inner
            .0
            .update(|labels| labels.iter().filter(|l| l.name != name).cloned().collect());
    }

    /// Clears the all labels.
    pub fn clear(&mut self) {
        self.inner.0.store(Vec::new());
    }

    pub(crate) fn new(labels: &'a Labels, reserved: Option<&'static str>) -> Self {
        LabelsMut {
            inner: labels,
            reserved,
        }
    }
}
impl<'a> Deref for LabelsMut<'a> {
    type Target = Labels;
    fn deref(&self) -> &Self::Target {
        self.inner
    }
}

/// An iterator over the labels of a `Labels`.
#[derive(Debug)]
pub struct Iter<'a> {
    labels: std::sync::Arc<Vec<Label>>,
    inner: std::slice::Iter<'a, Label>,
}
impl<'a> Iterator for Iter<'a> {
    type Item = &'a Label;
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}