Expand description
§Rusty PromQL Parser
A Rust parser for the Prometheus Query Language (PromQL) using the nom parser combinator library.
This crate provides a complete parser for PromQL expressions, producing an Abstract Syntax Tree (AST) that can be used for analysis, transformation, or evaluation.
§Quick Start
The main entry point is the expr() function, which parses a PromQL expression
and returns the remaining input along with the parsed AST:
use rusty_promql_parser::expr;
let input = r#"http_requests_total{job="api"}"#;
let (rest, ast) = expr(input).expect("failed to parse");
assert!(rest.is_empty());
println!("{:#?}", ast);§Examples
§Parsing a metric with label filtering
use rusty_promql_parser::expr;
let input = r#"go_gc_duration_seconds{instance="localhost:9090", job="alertmanager"}"#;
let (rest, ast) = expr(input).expect("failed to parse");
assert!(rest.is_empty());§Parsing aggregation operators
use rusty_promql_parser::expr;
let input = r#"sum by (app, proc) (
instance_memory_limit_bytes - instance_memory_usage_bytes
) / 1024 / 1024"#;
let (rest, ast) = expr(input).expect("failed to parse");
assert!(rest.is_empty());§Parsing rate queries
use rusty_promql_parser::expr;
let input = "rate(http_requests_total[5m])";
let (rest, ast) = expr(input).expect("failed to parse");
assert!(rest.is_empty());§AST Types
The parser produces an Expr enum which can be one of:
Expr::Number- Numeric literals (42,3.14,Inf,NaN)Expr::String- String literals ("hello",'world')Expr::VectorSelector- Instant vector selectors (metric{label="value"})Expr::MatrixSelector- Range vector selectors (metric[5m])Expr::Call- Function calls (rate(...),histogram_quantile(...))Expr::Aggregation- Aggregation expressions (sum by (job) (...))Expr::Binary- Binary operations (a + b,foo and bar)Expr::Unary- Unary operations (-metric)Expr::Paren- Parenthesized expressions ((a + b))Expr::Subquery- Subqueries (metric[5m:1m])
§Modules
ast- Abstract Syntax Tree type definitionslexer- Low-level token parsers (numbers, strings, durations, identifiers)parser- Expression and statement parsers
§Display
All AST types implement std::fmt::Display, allowing you to convert parsed
expressions back to PromQL strings:
use rusty_promql_parser::expr;
let (_, ast) = expr("1 + 2 * 3").unwrap();
assert_eq!(ast.to_string(), "1 + 2 * 3");Re-exports§
pub use ast::Aggregation;pub use ast::BinaryExpr;pub use ast::BinaryModifier;pub use ast::BinaryOp;pub use ast::Call;pub use ast::Expr;pub use ast::GroupModifier;pub use ast::GroupSide;pub use ast::SubqueryExpr;pub use ast::UnaryExpr;pub use ast::UnaryOp;pub use ast::VectorMatching;pub use ast::VectorMatchingOp;pub use lexer::number;pub use lexer::number;pub use parser::aggregation::Grouping;pub use parser::aggregation::GroupingAction;pub use parser::expr;pub use parser::expr;pub use parser::selector::LabelMatchOp;pub use parser::selector::LabelMatcher;pub use parser::selector::MatrixSelector;pub use parser::selector::VectorSelector;