Format PromQL from your terminal.
A small Go library + CLI that prettifies PromQL queries.
- 🧹 Prettify any PromQL / MetricsQL query into a readable, multi-line form.
- 🔀 Two backends —
vic(VictoriaMetricsmetricsql.Prettify, the default) andmain(a custom Prometheus-AST formatter with Grafana$variablesupport). - 📦 Library and CLI — drop it into your Go code, or run it from the shell.
- 🪶 Zero config, no network — pure Go, formats locally.
- 🧩 Powers the promql-formatter Chrome extension via WebAssembly.
$ promql-formatter 'sum(rate(http_requests_total[1m]))by(job) / sum(rate(http_requests_total[1m] offset 1d))by(job) > 1.5'(
sum(rate(http_requests_total[1m])) by(job)
/
sum(rate(http_requests_total[1m] offset 1d)) by(job)
)
>
1.5
go install github.com/xhinliang/promql-formatter-cli/cmd/promql-formatter@latestOr grab a source build:
git clone https://github.com/XhinLiang/promql-formatter-cli.git
cd promql-formatter-cli
go build -o promql-formatter ./cmd/promql-formatter# format an argument (default backend: vic)
promql-formatter 'sum(rate(http_requests_total[1m])) by (job)'
# read from stdin, pick a backend
echo 'sum(rate(http_requests_total[1m])) by (job)' | promql-formatter --formatter main| Flag | Description | Default |
|---|---|---|
--formatter |
Backend: vic or main |
vic |
--version |
Print version and exit |
import promqlfmt "github.com/xhinliang/promql-formatter-cli"
func main() {
out, err := promqlfmt.Format("sum(rate(x[1m]))by(a)", "vic")
// or: promqlfmt.FormatVic(...) / promqlfmt.FormatMain(...)
_ = err
println(out)
}| Backend | Engine | Notes |
|---|---|---|
vic (default) |
VictoriaMetrics metricsql.Prettify |
Robust, MetricsQL-aware prettifier. |
main |
Prometheus parser + custom AST visitor | Adds Grafana $variable / $__interval handling and offset fix-ups. |
Issues and PRs are welcome — see CONTRIBUTING.md. Run go test ./... and go vet ./... before opening a PR.
The main backend originates from the o11y.tools PromQL prettifier (Apache-2.0). The vic backend wraps VictoriaMetrics metricsql.