6 releases (3 breaking)

Uses old Rust 2015

0.5.0 Jan 29, 2024
0.4.2 Sep 25, 2021
0.4.0 May 23, 2020
0.3.0 Feb 22, 2018
0.2.0 Jan 15, 2018

#2017 in Database interfaces

35 downloads per month

Apache-2.0

64KB
2.5K SLoC

PromQL parser for Rust

This crate allows one to parse PromQL query into some AST.

See official documentation for query syntax description, or crate documentation to dive right into code.


lib.rs:

This crate allows one to parse PromQL query into some AST.

See official documentation for query syntax description.

Example


use promql::*;

let opts = ParserOptions::new()
.allow_dots(false)
.build();

// query can also be `&str`
let query: &[u8] = br#"
sum(1 - something_used{env="production"} / something_total) by (instance)
and ignoring (instance)
sum(rate(some_queries{instance=~"localhost\\d+"} [5m])) > 100
"#;
let ast = parse(query, &opts).unwrap(); // or show user that their query is invalid

// now we can look for all sorts of things

// AST can represent an operator
if let Node::Operator { op: Op::And(op_mod), args } = ast {
// operators can have modifiers
assert_eq!(op_mod, Some(OpMod {
action: OpModAction::Ignore,
labels: vec!["instance".to_string()],
group: None,
}));

// aggregation functions like sum are represented as functions with optional modifiers (`by (label1, …)`/`without (…)`)
if let Node::Function { ref name, ref aggregation, ref args } = args[0] {
assert_eq!(*name, "sum".to_string());
assert_eq!(*aggregation, Some(AggregationMod {
action: AggregationAction::By,
labels: vec!["instance".to_string()],
}));

//
}
} else {
panic!("top operator is not an 'and'");
}

Types

This parser emits Vec<u8> for most string literals because PromQL, like Go, allows raw byte sequences to be included in the string literals (e.g. {omg=''} is equivalent to both {omg='\u221e'} and {omg='\xe2\x88\x9e'}).

Dependencies

~2.5MB
~52K SLoC