-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prometheus parse will parse prometheus or openmetrics format files provided as pipeline input and output parsed metrics data prometheus scrape will scrape a prometheus target and output parsed metrics data
- Loading branch information
Showing
9 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use crate::Client; | ||
use nom_openmetrics::{ | ||
parser::{exposition, prometheus}, | ||
Family, MetricDescriptor, Sample, | ||
}; | ||
use nu_protocol::{record, LabeledError, Record, Span, Value}; | ||
|
||
#[derive(Default)] | ||
pub enum ParseFormat { | ||
#[default] | ||
Prometheus, | ||
Openmetrics, | ||
} | ||
|
||
pub struct Parse<'a> { | ||
input: &'a Value, | ||
format: ParseFormat, | ||
} | ||
|
||
impl<'a> Parse<'a> { | ||
pub fn new(input: &'a Value) -> Self { | ||
Self { | ||
input, | ||
format: Default::default(), | ||
} | ||
} | ||
|
||
pub fn run(self) -> Result<Value, LabeledError> { | ||
let Self { input, format } = self; | ||
|
||
let (_, families) = match format { | ||
ParseFormat::Prometheus => prometheus(input.as_str()?).unwrap(), | ||
ParseFormat::Openmetrics => exposition(input.as_str()?).unwrap(), | ||
}; | ||
|
||
let families = families | ||
.iter() | ||
.map(|family| family_to_value(family)) | ||
.collect(); | ||
|
||
Ok(Value::list(families, Span::unknown())) | ||
} | ||
|
||
pub fn set_format(&mut self, format: ParseFormat) { | ||
self.format = format; | ||
} | ||
} | ||
|
||
impl<'a> Client for Parse<'a> {} | ||
|
||
fn family_to_value(family: &Family) -> Value { | ||
let descriptors = family | ||
.descriptors | ||
.iter() | ||
.map(|descriptor| descriptor_to_value(descriptor)) | ||
.collect(); | ||
|
||
let samples = family | ||
.samples | ||
.iter() | ||
.map(|sample| sample_to_value(sample)) | ||
.collect(); | ||
|
||
let record = record! { | ||
"descriptors" => Value::list(descriptors, Span::unknown()), | ||
"samples" => Value::list(samples, Span::unknown()), | ||
}; | ||
|
||
Value::record(record, Span::unknown()) | ||
} | ||
|
||
fn descriptor_to_value(descriptor: &MetricDescriptor) -> Value { | ||
let record = match descriptor { | ||
MetricDescriptor::Type { metric, r#type } => record! { | ||
"descriptor" => Value::string("type", Span::unknown()), | ||
"metric" => Value::string(*metric, Span::unknown()), | ||
"type" => Value::string(r#type.to_string(), Span::unknown()) | ||
}, | ||
MetricDescriptor::Help { metric, help } => record! { | ||
"descriptor" => Value::string("help", Span::unknown()), | ||
"metric" => Value::string(*metric, Span::unknown()), | ||
"help" => Value::string(help, Span::unknown()) | ||
}, | ||
MetricDescriptor::Unit { metric, unit } => record! { | ||
"descriptor" => Value::string("unit", Span::unknown()), | ||
"metric" => Value::string(*metric, Span::unknown()), | ||
"unit" => Value::string(*unit, Span::unknown()) | ||
}, | ||
}; | ||
|
||
Value::record(record, Span::unknown()) | ||
} | ||
|
||
fn sample_to_value(sample: &Sample) -> Value { | ||
let mut record = Record::new(); | ||
|
||
record.insert("name", Value::string(sample.name(), Span::unknown())); | ||
record.insert("value", Value::float(sample.number(), Span::unknown())); | ||
|
||
Value::record(record, Span::unknown()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use nom_openmetrics::{Family, Sample}; | ||
use nu_protocol::{LabeledError, Record, Span, Value}; | ||
|
||
use crate::client::Client; | ||
|
||
pub struct Scrape { | ||
target: String, | ||
} | ||
|
||
impl Scrape { | ||
pub fn new(target: String) -> Self { | ||
Self { target } | ||
} | ||
|
||
pub fn run(self) -> Result<Value, LabeledError> { | ||
let Self { ref target, .. } = self; | ||
|
||
self.runtime()?.block_on(async { | ||
let body = reqwest::get(target).await.unwrap().bytes().await.unwrap(); | ||
|
||
let body = String::from_utf8(body.to_vec()).unwrap(); | ||
|
||
let (_, families) = nom_openmetrics::parser::prometheus(&body).unwrap(); | ||
|
||
let families = families | ||
.iter() | ||
.map(|family| family_to_value(family)) | ||
.collect(); | ||
|
||
Ok(Value::list(families, Span::unknown())) | ||
}) | ||
} | ||
} | ||
|
||
impl Client for Scrape {} | ||
|
||
fn family_to_value(family: &Family) -> Value { | ||
let mut record = Record::new(); | ||
|
||
let samples = family | ||
.samples | ||
.iter() | ||
.map(|sample| sample_to_value(sample)) | ||
.collect(); | ||
|
||
record.insert("samples", Value::list(samples, Span::unknown())); | ||
|
||
Value::record(record, Span::unknown()) | ||
} | ||
|
||
fn sample_to_value(sample: &Sample) -> Value { | ||
let mut record = Record::new(); | ||
|
||
record.insert("name", Value::string(sample.name(), Span::unknown())); | ||
record.insert("value", Value::float(sample.number(), Span::unknown())); | ||
|
||
Value::record(record, Span::unknown()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::{ | ||
client::{Parse, ParseFormat}, | ||
Prometheus, | ||
}; | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{LabeledError, Signature, Span, SyntaxShape, Type, Value}; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct ParseCommand; | ||
|
||
impl SimplePluginCommand for ParseCommand { | ||
type Plugin = Prometheus; | ||
|
||
fn name(&self) -> &str { | ||
"prometheus parse" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build(self.name()) | ||
.description(self.description()) | ||
.named( | ||
"format", | ||
SyntaxShape::String, | ||
"Metrics format, prometheus (default) or openmetrics", | ||
None, | ||
) | ||
.input_output_types(vec![(Type::String, Type::table())]) | ||
} | ||
|
||
fn description(&self) -> &str { | ||
"Parse prometheus or openmetrics output" | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
_engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let format = call | ||
.get_flag_value("format") | ||
.unwrap_or(Value::string("prometheus", Span::unknown())); | ||
let format_span = format.span(); | ||
let format = format.into_string()?; | ||
|
||
let mut parser = Parse::new(input); | ||
|
||
match format.as_str() { | ||
"prometheus" => parser.set_format(ParseFormat::Prometheus), | ||
"openmetrics" => parser.set_format(ParseFormat::Openmetrics), | ||
_ => { | ||
return Err(LabeledError::new("Invalid format") | ||
.with_label("must be prometheus or openmetrics", format_span)); | ||
} | ||
} | ||
|
||
parser.run() | ||
} | ||
} |
Oops, something went wrong.