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
This commit is contained in:
Roman Khavronenko 2021-01-09 23:56:11 +00:00 committed by Aliaksandr Valialkin
parent 4ee53c3961
commit 304512b668
2 changed files with 4 additions and 1 deletions

View file

@ -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" }}

View file

@ -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 {