Victoriametrics: vmalert expression variable at rule templating.

Created on 15 May 2020  路  9Comments  路  Source: VictoriaMetrics/VictoriaMetrics

Is your feature request related to a problem? Please describe.
It`s nice option to have expression as variable for rules.

It would allow to add links to external monitoring system as part or label or annotations and use for quick incident investigation.

currently, it`s possible to use only value and labels.

Describe the solution you'd like
Something like that

        - alert: NodeExporterDown
          expr: absent(up{job="node"} == 1)
          for: 1m
          labels:
            severity: major
            # env - some external label
          annotations:
            link: <a href= 'https://grafanaurl.com/explore?orgId=1&left=["now-1h","now","prometheus",{"expr": "{{$expression}}"},{"mode":"Metrics"},{"ui":[true,true,true,"none"]}]'>link</a>
            value: "{{ $value}}"
            back:  job restored node {{$labels.instance}}
````

 probably it  would be also possible to add external label to vmagent and create global label with link

```yaml
global:
  external_labels:
      link: "https://mymonitoting.system/explore/{{$labels.env}}/query?q={{$expression}}"

Describe alternatives you've considered
As alternative, copy paste expression to every event,some close to real world example:

        - alert: NodeExporterDown
          expr: absent(up{job="node*"} == 1)
          for: 1m
          labels:
            severity: major
            # env - some external label
          annotations:
            link: <a href= 'https://grafanaurl.com/explore?orgId=1&left=["now-1h","now","prometheus",{"expr": "absent(up{job=\"node*\"} == 1)"},{"mode":"Metrics"},{"ui":[true,true,true,"none"]}]'>link</a>
            value: "{{ $value}}"
            back:  job restored node {{$labels.instance}}

enhancement vmalert

Most helpful comment

tested with latest release, looks awesome, thx!
example of rule at helm chart:

  groups:
    - name: vmalert
      rules:
        - alert: vmalert_config_reload_failed
          expr: vmalert_config_last_reload_errors_total - vmalert_config_last_reload_errors_total offset 1m > 0
          for: 30s
          labels:
            severity: major
            service: vmalert
            instance: "{{$labels.kubernetes_pod_name}}"
          annotations:
            value: "{{ $value}}"
            glink: "<a href= 'https://grafana.example.com/explore?orgId=1&left=[\"now-1h\",\"now\",\"{{$labels.environment}}\",{\"expr\": \"{{$expr |  quotesEscape | pathEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]' >link</a>"
            description: "failed to reload configuration"
            back: "config reload if normal "

All 9 comments

Sounds good! It may also require to add function to escape expression for passing in URL.
What do you think @kreedom?

yep, escape for double quotes is needed in my case. There is already reReplaceAll, so its possible to use it for something like:

 'link: <a href= https://grafanaurl.com/explore?orgId=1&left=["now-1h","now","prometheus",{"expr": "{{$expression | reReplaceAll "\"" "\\\"" }}"},{"mode":"Metrics"},{"ui":[true,true,true,"none"]}]>link'</a>

It looks really bad, but it`s not a problem to add escape function.

I will investigate options and send mr in success.

sounds good i will handle the case

@f41gh7 the new functionality merged to master in 7e173655ba672385a3ff262593897d544aff15aa

thx, it works!

but escape functions work incorrect in my case, it escapes url, but after translation i have url:

https://grafanaurl.com/explore?orgId=1&left=["now-1h","now","prometheus",{"expr": "absent(up{job="node*"} == 1)"},{"mode":"Metrics"}

when i need:

https://grafanaurl.com/explore?orgId=1&left=["now-1h","now","prometheus",{"expr": "absent(up{job=\"node*\"} == 1)"},{"mode":"Metrics"}

with escaped expression double quotes .

but after i`ve added simple function

        "quoteEscape": func(q string) string {
            return strings.ReplaceAll(q,`"`,`\"`)
        },

Everything was ok.

my final annotation was:

link: "<a href= 'https://grafanaurl.com/explore?orgId=1&left=[\"now-1h\",\"now\",\"prometheus\",{\"expr\": \"${{expr | quoteEscape}}\"} == 1)\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]'>link</a>"

FYI, the expression variable and escaping mentioned by @kreedom above has been integrated into v1.35.6.

As I understood there are remaining issues here, so no closing the issue.

tested with latest release, looks awesome, thx!
example of rule at helm chart:

  groups:
    - name: vmalert
      rules:
        - alert: vmalert_config_reload_failed
          expr: vmalert_config_last_reload_errors_total - vmalert_config_last_reload_errors_total offset 1m > 0
          for: 30s
          labels:
            severity: major
            service: vmalert
            instance: "{{$labels.kubernetes_pod_name}}"
          annotations:
            value: "{{ $value}}"
            glink: "<a href= 'https://grafana.example.com/explore?orgId=1&left=[\"now-1h\",\"now\",\"{{$labels.environment}}\",{\"expr\": \"{{$expr |  quotesEscape | pathEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]' >link</a>"
            description: "failed to reload configuration"
            back: "config reload if normal "

thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

isality picture isality  路  3Comments

valyala picture valyala  路  4Comments

Serrvosky picture Serrvosky  路  3Comments

v98765 picture v98765  路  3Comments

prdatur picture prdatur  路  3Comments