From 0d2e83e9d7ff154483097e9ae16eca4e0a5f3a3c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 26 Dec 2019 21:31:15 +0200 Subject: [PATCH] lib/metricsql: add example for ExpandWithExprs --- lib/metricsql/utils_example_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/metricsql/utils_example_test.go diff --git a/lib/metricsql/utils_example_test.go b/lib/metricsql/utils_example_test.go new file mode 100644 index 0000000000..2f38da766e --- /dev/null +++ b/lib/metricsql/utils_example_test.go @@ -0,0 +1,28 @@ +package metricsql_test + +import ( + "fmt" + "log" + + "github.com/VictoriaMetrics/VictoriaMetrics/lib/metricsql" +) + +func ExampleExpandWithExprs() { + // mql can contain arbitrary MetricsQL extensions - see https://github.com/VictoriaMetrics/VictoriaMetrics/wiki/ExtendedPromQL + mql := `WITH ( + commonFilters = {job="$job", instance="$instance"}, + f(a, b) = 100*(a/b), + ) + f(disk_free_bytes{commonFilters}, disk_total_bytes{commonFilters})` + + // Convert mql to PromQL + pql, err := metricsql.ExpandWithExprs(mql) + if err != nil { + log.Fatalf("cannot expand with expressions: %s", err) + } + + fmt.Printf("%s\n", pql) + + // Output: + // 100 * (disk_free_bytes{job="$job", instance="$instance"} / disk_total_bytes{job="$job", instance="$instance"}) +}