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

#1311 in Database interfaces

Download history 19/week @ 2024-07-31 7/week @ 2024-08-07 16/week @ 2024-08-14 10/week @ 2024-08-21 90/week @ 2024-08-28 43/week @ 2024-09-04 95/week @ 2024-09-11 178/week @ 2024-09-18 107/week @ 2024-09-25 148/week @ 2024-10-02 35/week @ 2024-10-09 9/week @ 2024-10-16 7/week @ 2024-10-23 6/week @ 2024-10-30 50/week @ 2024-11-06 41/week @ 2024-11-13

105 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
~58K SLoC