Kapacitor unable to parse alert-template-file which has if else statement condition for .Level

I would like to replace the custom .Level string value of "OK" and "Critical" with "UP" and "Down" . In my alert-template-file I have the following:

{
"id": "{{.ID}}",
"message": "{{.Message}}",
"time": "{{.Time}}",
"level": "{{ if eq .Level "OK" }}Up{{ else }}Down{{ end }}",
"data": {{ .Data }}
}

When Kapacitor tries to send the alerts, I get an error that states:

error calling eq: incompatible types for comparison

Is what I am doing not supported for alert-template-files ? Is using if else in such way not proper syntax ?

I would appreciate if someone can help me understand this.

Thank you.

Hello @hext,
I think you’re close but how you evaluate the level and also you need to include that if else in the message, not the level definition. See thee example below as reference.

  var data = stream
        |from()...
    // Trigger critical alert if the throughput drops below 100 points per 10s and checked every 10s.
    data
        |stats(10s)
            .align()
        |derivative('emitted')
            .unit(10s)
            .nonNegative()
        |alert()
            .id('node \'stream0\' in task \'{{ .TaskName }}\'')
            .message('{{ .ID }} is {{ if eq .Level "OK" }}alive{{ else }}dead{{ end }}: {{ index .Fields "emitted" | printf "%0.3f" }} points/10s.')
            .crit(lambda: "emitted" <= 100.0)
    //Do normal processing of data

Alternatively, you might find this levelfield useful which creates an optional field key to add to the data, containing the alert level as a string:

Hi @Anaisdg,

Thank you for clarifying this. I was under the impression that if else statement were possible in the .Level definition, but what you’ve provided is a good work around.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.

Hello @hext,
You might be right! I’m constantly learning from community. I’m glad that works for you.