TL;DR How do I use a label to select which template to use? (Nest variable in expression)
I am trying to use one contact point to send 2 different templates.
One template would be used for a missed check in.
The other would be for a parameter, such as high CPU usage.
When I fire an alert, I’d like to have the following labels:
- name: “Service A”
- contact: “Teams”
- message_template: “missed_checkin”
- metric_name: “CPU”
The metric_name would be an optional label, only used in the second template.
The message_template would be either “missed_checkin” or “metric_threshold”
In the contact point’s message I have tried to use the following
{{ range .Alerts.Firing }}
{{ template "${.Labels.message_template}_firing".}}
{{ end }}
{{ range .Alerts.Resolved }}
{{ template "${.Labels.message_template}_resolved".}}
{{ end }}
With the following templates defined outside the contact point
{{ define "missed_checkin_firing" }}
{{ range .}}
{{ .Labels.Name }} is down.
{{ end }}
{{ end }}
{{ define "missed_checkin_resolved" }}
{{ range .}}
{{ .Labels.Name }} is back up.
{{ end }}
{{ end }}
{{ define "metric_threshold_firing" }}
{{ range .}}
{{ .Labels.Name }} has high {{ .Labels.metric_name }} usage.
{{ end }}
{{ end }}
{{ define "metric_threshold_resolved" }}
{{ range .}}
{{ .Labels.Name }}'s high {{ .Labels.metric_name }} usage has resolved.
{{ end }}
{{ end }}