|
| 1 | +use crate::Client; |
| 2 | +use nom_openmetrics::{ |
| 3 | + parser::{exposition, prometheus}, |
| 4 | + Family, MetricDescriptor, Sample, |
| 5 | +}; |
| 6 | +use nu_protocol::{record, LabeledError, Record, Span, Value}; |
| 7 | + |
| 8 | +#[derive(Default)] |
| 9 | +pub enum ParseFormat { |
| 10 | + #[default] |
| 11 | + Prometheus, |
| 12 | + Openmetrics, |
| 13 | +} |
| 14 | + |
| 15 | +pub struct Parse<'a> { |
| 16 | + input: &'a Value, |
| 17 | + format: ParseFormat, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a> Parse<'a> { |
| 21 | + pub fn new(input: &'a Value) -> Self { |
| 22 | + Self { |
| 23 | + input, |
| 24 | + format: Default::default(), |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + pub fn run(self) -> Result<Value, LabeledError> { |
| 29 | + let Self { input, format } = self; |
| 30 | + |
| 31 | + let (_, families) = match format { |
| 32 | + ParseFormat::Prometheus => prometheus(input.as_str()?).unwrap(), |
| 33 | + ParseFormat::Openmetrics => exposition(input.as_str()?).unwrap(), |
| 34 | + }; |
| 35 | + |
| 36 | + let families = families |
| 37 | + .iter() |
| 38 | + .map(|family| family_to_value(family)) |
| 39 | + .collect(); |
| 40 | + |
| 41 | + Ok(Value::list(families, Span::unknown())) |
| 42 | + } |
| 43 | + |
| 44 | + pub fn set_format(&mut self, format: ParseFormat) { |
| 45 | + self.format = format; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a> Client for Parse<'a> {} |
| 50 | + |
| 51 | +fn family_to_value(family: &Family) -> Value { |
| 52 | + let descriptors = family |
| 53 | + .descriptors |
| 54 | + .iter() |
| 55 | + .map(|descriptor| descriptor_to_value(descriptor)) |
| 56 | + .collect(); |
| 57 | + |
| 58 | + let samples = family |
| 59 | + .samples |
| 60 | + .iter() |
| 61 | + .map(|sample| sample_to_value(sample)) |
| 62 | + .collect(); |
| 63 | + |
| 64 | + let record = record! { |
| 65 | + "descriptors" => Value::list(descriptors, Span::unknown()), |
| 66 | + "samples" => Value::list(samples, Span::unknown()), |
| 67 | + }; |
| 68 | + |
| 69 | + Value::record(record, Span::unknown()) |
| 70 | +} |
| 71 | + |
| 72 | +fn descriptor_to_value(descriptor: &MetricDescriptor) -> Value { |
| 73 | + let record = match descriptor { |
| 74 | + MetricDescriptor::Type { metric, r#type } => record! { |
| 75 | + "descriptor" => Value::string("type", Span::unknown()), |
| 76 | + "metric" => Value::string(*metric, Span::unknown()), |
| 77 | + "type" => Value::string(r#type.to_string(), Span::unknown()) |
| 78 | + }, |
| 79 | + MetricDescriptor::Help { metric, help } => record! { |
| 80 | + "descriptor" => Value::string("help", Span::unknown()), |
| 81 | + "metric" => Value::string(*metric, Span::unknown()), |
| 82 | + "help" => Value::string(help, Span::unknown()) |
| 83 | + }, |
| 84 | + MetricDescriptor::Unit { metric, unit } => record! { |
| 85 | + "descriptor" => Value::string("unit", Span::unknown()), |
| 86 | + "metric" => Value::string(*metric, Span::unknown()), |
| 87 | + "unit" => Value::string(*unit, Span::unknown()) |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + Value::record(record, Span::unknown()) |
| 92 | +} |
| 93 | + |
| 94 | +fn sample_to_value(sample: &Sample) -> Value { |
| 95 | + let mut record = Record::new(); |
| 96 | + |
| 97 | + record.insert("name", Value::string(sample.name(), Span::unknown())); |
| 98 | + record.insert("value", Value::float(sample.number(), Span::unknown())); |
| 99 | + |
| 100 | + Value::record(record, Span::unknown()) |
| 101 | +} |
0 commit comments