Crate promql [] [src]

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

See official documentation for query syntax description.

Example


use promql::*;

let ast = parse(b"
    sum(1 - something_used{env=\"production\"} / something_total) by (instance)
    and ignoring (instance)
    sum(rate(some_queries{instance=~\"localhost\\\\d+\"} [5m])) > 100
").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 { x, op: Op::And(op_mod), y } = 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 } = *x {
        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\"");
}

Structs

AggregationMod
LabelMatch

Single label filter.

OpGroupMod

Vector grouping operator modifier (group_left(…)/group_right(…)).

OpMod

Vector matching operator modifier (on (…)/ignoring (…)).

Vector

This struct represents both instant and range vectors.

Enums

AggregationAction
LabelMatchOp

Label filter operators.

Node

AST node.

Op

PromQL operators

OpGroupSide
OpModAction

Functions

parse

Parse expression string into an AST.