Skip to content

jiacai2050/promql-prettier

Repository files navigation

https://github.com/jiacai2050/promql-prettier/actions/workflows/CI.yaml/badge.svg

PromQL Prettier

Have trouble in reading PromQL written by your colleagues or SRE team? promql-prettier to the rescue!

Thanks for WebAssembly, you can try it in your browser!

You can also download it from releases and integrate with your favorite editors.

Examples

Original:
go_goroutines{instance!="localhost:9090", job!~"prometheus.*"}
Pretty print:
go_goroutines{instance!="localhost:9090", job!~"prometheus.*"}

Original:
{__name__="go_goroutines",instance="localhost:9090",job="prometheus"}
Pretty print:
go_goroutines{instance="localhost:9090", job="prometheus"}

Original:
(((metric_name_long)))
Pretty print:
metric_name_long

Original:
histogram_quantile(0.9, rate(instance_cpu_time_seconds{app="lion", proc="web",job="cluster-manager"}[5m]))
Pretty print:
histogram_quantile (
  0.9,
  rate (
    instance_cpu_time_seconds{app="lion", proc="web", job="cluster-manager"}[5m:]
  )
)

Original:
topk(5, (sum without(env) (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"})))
Pretty print:
topk (
  5,
  sum without (env) (
    instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
  )
)

Original:
sum (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}) without (label)
Pretty print:
sum without (label) (
  instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
)

Original:
sum (instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"} + http_request_total{job="apiserver", handler="/api/comments"}) without (label)
Pretty print:
sum without (label) (
    instance_cpu_time_ns{app="lion", proc="web", rev="34d0f99", env="prod", job="cluster-manager"}
  +
    http_request_total{job="apiserver", handler="/api/comments"}
)

Original:
first_long{foo="bar", hello="world"} + second{foo="bar"} + third{foo="bar", localhost="9090"} + forth
Pretty print:
  (
      (
          first_long{foo="bar", hello="world"}
        +
          second{foo="bar"}
      )
    +
      third{foo="bar", localhost="9090"}
  )
+
  forth

Editor integration

Emacs

(defun my/format-promql (s e)
  (interactive "r")
  (shell-command-on-region s e "promql-prettier"
                           :replace t))

Reference

LICENSE

Copyright (c) 2022 Jiacai Liu <jiacai2050@gmail.com>

PromQL Prettier is distributed under GPL-3.0 license.