Crate promql

source ·
Expand description

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'}).

Structs§

Enums§

Functions§

  • Parse expression string into an AST.