Prometheus alert rules - check if a label exists in the message field

Viewed 584

An example of the rule I want to append with a label if that label exists.

- alert: TargetDown
      annotations:
        message: '{{`{{`}} printf "%.4g" $value {{`}}`}}% of the {{`{{`}} $labels.job {{`}}`}}/{{`{{`}} $labels.service {{`}}`}} targets in {{`{{`}} $labels.namespace {{`}}`}} namespace are down.'

I want to be able to append pod name with $labels.pod to the message string. So ideally it would look like:

- alert: TargetDown
      annotations:
        message: '{{`{{`}} printf "%.4g" $value {{`}}`}}% of the {{`{{`}} $labels.job {{`}}`}}/{{`{{`}} $labels.service {{`}}`}} targets in namesapce {{`{{`}} $labels.namespace {{`}}`}} in pod {{`{{`}} $labels.pod {{`}}`}} are down.'

So there I added in pod {{`{{`}} $labels.pod {{`}}`}}. But some matches won't have pod labels. I want to change that appended piece of string and print pod label only if it exists. I couldn't find any documentation about it. I saw something like {{if eq ($value) "2"}} dosmth {{end}}, but it is not what I need. I just want to say; okay if $labels.pod exists then print in pod {{`{{`}} $labels.pod {{`}}`}}. Otherwise don't add that to the message.

0 Answers
Related