rusty-promql-parser

Crates.iorusty-promql-parser
lib.rsrusty-promql-parser
version0.1.0
created_at2025-12-05 09:08:52.427024+00
updated_at2025-12-05 09:08:52.427024+00
descriptionA Prometheus PromQL parser written in Rust
homepage
repositoryhttps://github.com/sintef/rusty-promql-parser
max_upload_size
id1967901
size227,999
Antoine Pultier (fungiboletus)

documentation

https://docs.rs/rusty-promql-parser

README

Rusty PromQL Parser

Crates.io Documentation License

Rust port of the Prometheus PromQL parser using the nom parser combinator library.

Examples

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());
println!("{:#?}", ast);
VectorSelector {
    name: Some("go_gc_duration_seconds"),
    matchers: [
        LabelMatcher { name: "instance", op: Equal, value: "localhost:9090" },
        LabelMatcher { name: "job", op: Equal, value: "alertmanager" }
    ],
}

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());
println!("{:#?}", ast);
BinaryExpr {
    op: Div,
    lhs: BinaryExpr {
        op: Div,
        lhs: Aggregation {
            op: "sum",
            expr: BinaryExpr {
                op: Sub,
                lhs: VectorSelector { name: Some("instance_memory_limit_bytes"), ... },
                rhs: VectorSelector { name: Some("instance_memory_usage_bytes"), ... },
            },
            grouping: Some(Grouping { action: By, labels: ["app", "proc"] })
        },
        rhs: Number(1024.0),
    },
    rhs: Number(1024.0),
}

⚠️ Vibecoded ⚠️

This project is mostly vibecoded, using the official Prometheus PromQL parser (Apache 2.0) and a Rust port by HewlettPackard (MIT) as reference. You are welcome.

Why?

The main goal was to experiment whether vibecoding technology of December 2025 could allow one to port a non-trivial piece of software from Golang to Rust, in a reasonable time frame. Apparently, yes. It took a few hours.

Testing

The advanced stochastic parrots were requested to import the test cases from the original Prometheus parser to ensure some compatibility.

In addition to the unit tests, we run some AFL fuzzing to ensure robustness against malformed inputs. One crash was found and fixed during development: a number overflow panic when dealing with long durations and unit conversions.

This is not perfect, but unit tests, fuzzing, nom combinators, and Rust, should make this parser reasonably robust.

You may not want to use this in production

As stated in the license, this is provided as-is, without warranty of any kind. It is also vibecoded.

But it's also relatively well tested and based on solid foundations with nom and rust, and of course the original Prometheus parser and its exhaustive test suite.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Acknowledgements

This project is ported from Prometheus' promql/parser.

The project supports the Smart Building Hub research infrastructure project, which is funded by the Norwegian Research Council.

Commit count: 0

cargo fmt