From 304512b668641482302852ce94bcfb55a6463032 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Sat, 9 Jan 2021 23:56:11 +0000 Subject: [PATCH] vmalert-989: return non-empty result in template func `query` stub to pass validation (#1002) On templates validation stage vmalert does not acutally send queries, so for complex chained expression validation may fail. To avoid this, we add a blank sample in response so validation can pass successfully. Later, during the rule execution, stub will be replaced with real `query` function. https://github.com/VictoriaMetrics/VictoriaMetrics/issues/989 --- app/vmalert/config/testdata/rules2-good.rules | 1 + app/vmalert/notifier/template_func.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/vmalert/config/testdata/rules2-good.rules b/app/vmalert/config/testdata/rules2-good.rules index 6c9141e90..00e4cb8ee 100644 --- a/app/vmalert/config/testdata/rules2-good.rules +++ b/app/vmalert/config/testdata/rules2-good.rules @@ -17,6 +17,7 @@ groups: (up == 1) labels: job: '{{ $labels.job }}' + dynamic: '{{ $x := query "up" | first | value }}{{ if eq 1.0 $x }}one{{ else }}unknown{{ end }}' annotations: description: Job {{ $labels.job }} is up! summary: All instances up {{ range query "up" }} diff --git a/app/vmalert/notifier/template_func.go b/app/vmalert/notifier/template_func.go index 043339b87..8751abcc7 100644 --- a/app/vmalert/notifier/template_func.go +++ b/app/vmalert/notifier/template_func.go @@ -178,7 +178,9 @@ func InitTemplateFunc(externalURL *url.URL) { // it is present here only for validation purposes, when there is no // provided datasource. "query": func(q string) ([]datasource.Metric, error) { - return nil, nil + // return non-empty slice to pass validation with chained functions in template + // see issue #989 for details + return []datasource.Metric{{}}, nil }, "first": func(metrics []datasource.Metric) (datasource.Metric, error) { if len(metrics) > 0 {