PromQL Parser for Python

0.8.0 · active · verified Tue Apr 14

promql-parser is a Python binding that provides a fast and robust PromQL (Prometheus Query Language) parser. It leverages a Rust-based parser for efficient conversion of PromQL queries into an Abstract Syntax Tree (AST), enabling programmatic analysis and manipulation of Prometheus queries. The current version is 0.8.0, with releases typically following updates to the underlying Rust parser.

Warnings

Install

Imports

Quickstart

Parse a PromQL query string into its Abstract Syntax Tree (AST) representation. The returned AST is a Python object that mirrors the structure of the PromQL query, allowing for detailed inspection of metric names, labels, functions, and operators.

from promql_parser import parse

query = 'rate(http_requests_total{code="200", job="prometheus"}[5m])'
ast = parse(query)
print(ast)
# Example of accessing AST components (exact structure depends on query)
# print(ast.expr.name) # For a VectorSelector, might access name
# print(ast.expr.matchers.matchers[0].name) # Accessing a matcher's name

view raw JSON →