Repository: VictoriaMetrics/VictoriaMetrics
Pull request #1002 (2021-01-08 21:28:35)

vmalert-989: return non-empty result in template func `query` stub to pass validation
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

codecov[bot]: # [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=h1) Report
> Merging [#1002](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=desc) (2d74768) into [master](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/commit/85962b459fc269a077dd8719f8dd3b5bdd7e8528?el=desc) (85962b4) will **decrease** coverage by `0.20%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/graphs/tree.svg?width=650&height=150&src=pr&token=YND8dYVcXn)](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1002      +/-   ##
==========================================
- Coverage   60.63%   60.42%   -0.21%     
==========================================
  Files         202      202              
  Lines       22984    22983       -1     
==========================================
- Hits        13936    13888      -48     
- Misses       7726     7775      +49     
+ Partials     1322     1320       -2     
```


| [Impacted Files](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [app/vmalert/notifier/template\_func.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-YXBwL3ZtYWxlcnQvbm90aWZpZXIvdGVtcGxhdGVfZnVuYy5nbw==) | `20.90% <0.00%> (ø)` | |
| [lib/mergeset/block\_stream\_reader.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-bGliL21lcmdlc2V0L2Jsb2NrX3N0cmVhbV9yZWFkZXIuZ28=) | `49.30% <0.00%> (-13.20%)` | :arrow_down: |
| [lib/mergeset/part.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-bGliL21lcmdlc2V0L3BhcnQuZ28=) | `79.42% <0.00%> (-2.29%)` | :arrow_down: |
| [app/vmalert/remotewrite/remotewrite.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-YXBwL3ZtYWxlcnQvcmVtb3Rld3JpdGUvcmVtb3Rld3JpdGUuZ28=) | `58.49% <0.00%> (-1.89%)` | :arrow_down: |
| [lib/mergeset/table.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-bGliL21lcmdlc2V0L3RhYmxlLmdv) | `64.82% <0.00%> (-1.69%)` | :arrow_down: |
| [app/vmselect/promql/aggr\_incremental.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-YXBwL3Ztc2VsZWN0L3Byb21xbC9hZ2dyX2luY3JlbWVudGFsLmdv) | `86.43% <0.00%> (-1.56%)` | :arrow_down: |
| [lib/storage/partition.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-bGliL3N0b3JhZ2UvcGFydGl0aW9uLmdv) | `65.55% <0.00%> (-1.00%)` | :arrow_down: |
| [lib/storage/index\_db.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002/diff?src=pr&el=tree#diff-bGliL3N0b3JhZ2UvaW5kZXhfZGIuZ28=) | `64.12% <0.00%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=footer). Last update [85962b4...2d74768](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/1002?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
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 {


Test patch:
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" }}