Alertmanager template dynamic url

26 views Asked by At

I'm trying to create a template that will dynamically pass the correct URL to the Slack receiver action button In my rules, I have dashboard_url annotations, but not for all rules, only for specific

My goal is to place dashboard_url if it is set in Annotations to the URL button, in other cases just use the static dashboard defined in the template, but based on host_type label, and leave it unset if any of the conditions is true

At this moment my template looks like below

{{ define "__dashboard_url" -}}
    {{- if .Annotations.dashboard_url }}
        {{ .Annotations.dashboard_url }}
    {{- else if  eq .CommonLabels.host_type "server" -}}
        {{ template "__grafana" }}/d/Gio_rBZnz/servers?var-host={{ .CommonLabels.hostname }}
    {{- else if eq .CommonLabels.host_type "worker" -}}
        {{ template "__grafana" }}/d/vVazN-Knz/workers?var-host={{ .CommonLabels.hostname }}
    {{- end }}
{{- end }}

Alert rule

groups:
- name: test
  rules:
  - alert: Target unavailable
    expr: up{job="hosts"} == 0
    for: 30s
    labels:
      severity: warning
    annotations:
      description: "Host `{{ $labels.hostname }}` of job `{{ $labels.job }}` has been unreachable"
      dashboard_url: "some_url"

Alertmanager config

actions:
  - type: button
    name: grafana
    text: 'Grafana :grafana:'
    value: 'text'
    url: '{{ template "__dashboard_url" . }}'

Unfortunately, it is failing with the error below

executing \"__dashboard_url\" at <.Annotations.dashboard_url>: can't evaluate field Annotations in type *template.Data"
0

There are 0 answers