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
use crate::comment::{comment_parser, CommentType};
use crate::common::empty_line_parser;
use crate::samples::{parse_sample, SampleEntry};
use crate::types::{Err, Metric, MetricType, Sample};
use nom::branch::alt;
use nom::combinator::map;
use nom::IResult;
use std::collections::HashMap;

// Restrict this to internal visibility only
pub(crate) mod comment;
pub(crate) mod common;
pub(crate) mod samples;
pub mod types;

#[derive(Debug)]
enum LineType<'a> {
    Empty,
    Sample(SampleEntry<'a>),
    Comment(CommentType<'a>),
}

fn parse_line(input: &str) -> IResult<&str, LineType> {
    alt((
        map(comment_parser, |l| LineType::Comment(l)),
        map(parse_sample, |l| LineType::Sample(l)),
        map(empty_line_parser, |_| LineType::Empty),
    ))(input)
}

struct InputIter<'a>(&'a str);

impl<'a> Iterator for InputIter<'a> {
    type Item = Result<LineType<'a>, Err>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.0.len() == 0 {
            None
        } else {
            match parse_line(self.0) {
                Ok(res) => {
                    self.0 = res.0;
                    Some(Ok(res.1))
                }
                Result::Err(err) => Some(Result::Err(Err::from(err))),
            }
        }
    }
}

impl<'a> Into<Metric> for SampleEntry<'a> {
    fn into(self) -> Metric {
        Metric {
            name: self.name.to_string(),
            data_type: MetricType::Untyped,
            samples: vec![self.into()],
        }
    }
}

impl<'a> Into<Sample> for SampleEntry<'a> {
    fn into(self) -> Sample {
        Sample {
            labels: self
                .labels
                .iter()
                .map(|(&k, v)| (k.to_string(), v.to_string()))
                .collect(),
            value: self.value,
            timestamp: self.timestamp_ms,
        }
    }
}

impl Metric {
    fn append_sample_entry(&mut self, s: SampleEntry) {
        assert_eq!(
            s.name, self.name,
            "Names should be equal when calling update on a metric"
        );
        self.push_sample(s.into());
    }
    fn append_type_def(&mut self, s: &str, t: MetricType) {
        assert_eq!(
            s,
            &self.name[..],
            "Names should be equal when calling update on a metric"
        );
        self.data_type = t;
    }
}

fn add_comment<'a, 'b>(map: &mut HashMap<&'a str, Metric>, c: CommentType<'a>) {
    if let CommentType::Type(s, t) = c {
        if let Some(x) = map.get_mut(s) {
            x.append_type_def(s, t);
        } else {
            map.insert(s, Metric::new(s, t));
        }
    }
}

fn add_sample<'a, 'b>(map: &'b mut HashMap<&'a str, Metric>, s: SampleEntry<'a>) {
    if let Some(x) = map.get_mut(s.name) {
        x.append_sample_entry(s);
    } else {
        map.insert(s.name, s.into());
    };
}

/// Parse a string and return a vector of metrics extracted from it.
pub fn parse_complete<'a>(input: &'a str) -> Result<Vec<Metric>, Err> {
    let mut acc: HashMap<&'a str, Metric> = HashMap::new();
    for l in InputIter(input) {
        match l? {
            LineType::Comment(c) => add_comment(&mut acc, c),
            LineType::Sample(s) => add_sample(&mut acc, s),
            LineType::Empty => {}
        };
    }
    let mut res: Vec<Metric> = acc.drain().map(|(_, v)| v).collect();
    // Make the order constant
    res.sort_unstable_by(|a, b| a.name.cmp(&b.name));
    Ok(res)
}

#[cfg(test)]
fn assert_metric(m: &Metric, name: &str, tpe: MetricType, samples: Vec<Sample>) {
    assert_eq!(m.name, name, "name {:?}", m);
    assert_eq!(m.data_type, tpe, "type {:?}", m);
    assert_eq!(m.samples, samples);
}

#[test]
fn test_parse_summary() {
    let res = parse_complete(
        r#"
# TYPE chain_account_commits summary
chain_account_commits {quantile="0.5"} 0

# TYPE chain_account_commits summary
chain_account_commits {quantile="0.75"} 123

# TYPE chain_account_commits summary
chain_account_commits {quantile="0.95"} 50
"#,
    )
    .unwrap();
    assert_eq!(res.len(), 1);
    assert_metric(
        &res[0],
        "chain_account_commits",
        MetricType::Summary,
        vec![
            Sample::new(0f64, Option::None, vec!["quantile", "0.5"]),
            Sample::new(123f64, Option::None, vec!["quantile", "0.75"]),
            Sample::new(50f64, Option::None, vec!["quantile", "0.95"]),
        ],
    );
}

#[test]
fn test_parse_complete() {
    let res = parse_complete(
        r#"
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"} 1028 1395066363000

rpc_duration_seconds_count 2693
"#,
    )
    .unwrap();
    assert_eq!(res.len(), 2);
    assert_metric(
        &res[0],
        "http_requests_total",
        MetricType::Counter,
        vec![
            Sample::new(
                1027f64,
                Option::Some(1395066363000),
                vec!["method", "post", "code", "200"],
            ),
            Sample::new(
                1028f64,
                Option::Some(1395066363000),
                vec!["method", "post", "code", "400"],
            ),
        ],
    );
    assert_metric(
        &res[1],
        "rpc_duration_seconds_count",
        MetricType::Untyped,
        vec![Sample::new(2693f64, None, vec![])],
    );
}