promql

    PromQL 查询解析器本地候选

    Download zip
    Version
    0.4.0
    License
    MIT
    Last updated
    4 hours ago
    Downloads
    1

    #可执行 API 示例

    对照 Prometheus 3.14.0,提供全部 90 个函数签名与静态类型检查。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "typed PromQL core rejects mismatched arguments" {
    assert_eq(
    @promql.infer_type(@promql.parse("sum by(job)(rate(up[5m])) / 2")),
    @promql.InstantVector,
    )
    assert_eq(@promql.infer_type(@promql.parse("time()+2")), @promql.Scalar)
    for
    s in [
    "rate(up)", "rate(up[5m],up)", "sum(2)", "up[5m]+up", "unknown(up)", "1 or up",
    "up unless 2",
    ] {
    assert_true(
    try {
    ignore(@promql.infer_type(@promql.parse(s)))
    false
    } catch {
    _ => true
    },
    )
    }
    }

    实验特性默认关闭,需通过 ParserOptions 显式开启。资源边界和剩余诊断/格式化/遍历差距见 README。

    ParseError

    pub suberror ParseError {
    Invalid(String)
    Located(String, SourceSpan)
    } derive(
    Debug
    )

    DurationExpr

    pub(all) enum DurationExpr {
    DurationValue(String)
    DurationUnary(String, DurationExpr)
    DurationBinary(String, DurationExpr, DurationExpr)
    DurationCall(String, Array[DurationExpr])
    } derive(Eq,
    Debug
    )

    Expr

    pub(all) enum Expr {
    Number(String)
    StringLiteral(String)
    StringBytes(Bytes)
    Selector(String, Array[(String, String, String)])
    SelectorBytes(String, Array[(Bytes, String, Bytes)])
    Parenthesized(Expr)
    Range(Expr, String)
    Call(String, Array[Expr])
    Aggregate(String, Array[String], Bool, Expr)
    Binary(String, Expr, Expr)
    Unary(String, Expr)
    BinaryMatch(String, VectorMatching, Expr, Expr)
    BinaryFill(String, VectorMatching, String?, String?, Expr, Expr)
    AggregateParam(String, Array[String], Bool, Expr, Expr)
    Subquery(Expr, String, String?)
    Offset(Expr, String)
    At(Expr, String)
    RangeExpression(Expr, DurationExpr)
    SubqueryExpression(Expr, DurationExpr, DurationExpr?)
    OffsetExpression(Expr, DurationExpr)
    ExtendedRange(Expr, String)
    } derive(Eq,
    Debug
    )

    FunctionSignature

    pub(all) struct FunctionSignature {
    name : String
    arguments : Array[QueryType]
    min_args : Int
    max_args : Int?
    result : QueryType
    experimental : Bool
    } derive(Eq, ToJson,
    Debug
    )

    ParserOptions

    pub(all) struct ParserOptions {
    experimental_functions : Bool
    duration_expressions : Bool
    extended_ranges : Bool
    fill_modifiers : Bool
    } derive(Eq, ToJson,
    Debug
    )

    ParserOptions::new

    fn ParserOptions::new(experimental_functions? : Bool, duration_expressions? : Bool, extended_ranges? : Bool, fill_modifiers? : Bool) -> ParserOptions

    QueryType

    pub(all) enum QueryType {
    Scalar
    InstantVector
    RangeVector
    StringValue
    } derive(Eq, ToJson,
    Debug
    )

    SourceSpan

    pub(all) struct SourceSpan {
    start : Int
    end : Int
    line : Int
    column : Int
    } derive(Eq, ToJson,
    Debug
    )

    Offsets and columns count Unicode scalar values, starting at zero and one respectively.

    VectorMatching

    pub(all) struct VectorMatching {
    return_bool : Bool
    mode : String?
    labels : Array[String]
    group : String?
    include_labels : Array[String]
    } derive(Eq,
    Debug
    )

    duration_value

    fn duration_value(text : String) -> Double raise ParseError

    Literal duration converted through signed-64 nanoseconds, as in Prometheus.

    function_signatures

    fn function_signatures() -> Array[FunctionSignature]

    infer_type

    fn infer_type(expr : Expr, options? : ParserOptions) -> QueryType raise ParseError

    Statically check the AST against the pinned Prometheus function signatures.

    number_value

    fn number_value(literal : String) -> Double raise ParseError

    Convert a PromQL numeric or duration literal using the pinned language rules.

    parse

    fn parse(source : String, options? : ParserOptions) -> Expr raise ParseError

    Parse and statically validate a query. Experimental syntax is opt-in.

    regex_matches_empty

    fn regex_matches_empty(pattern : String) -> Bool raise ParseError

    Validate supported RE2 syntax and report whether its fully anchored form matches "".

    timestamp_value

    fn timestamp_value(text : String) -> Int64 raise ParseError

    Timestamp in milliseconds, with round-half-away-from-zero conversion. Accepted seconds follow upstream bounds; overflowing milliseconds use Go/amd64's minimum-int64 sentinel.