rusty-promql-parser 0.1.0

A Prometheus PromQL parser written in Rust
Documentation
  • Coverage
  • 79.61%
    203 out of 255 items documented34 out of 131 items with examples
  • Size
  • Source code size: 227.49 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 15.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 30s Average build duration of successful builds.
  • all releases: 30s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SINTEF/rusty-promql-parser
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • fungiboletus

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.