Summary condition based on value of an expression

Hi All!

I would like to make a some conditionbased summary with the ‘alertng’ engine in our Grafana v8.4.7 (23bf3ef043) but have problems with it.

I have some alert queries expressions etc, where the last is my real ‘alert’ (also with name of ‘alert’) expression with a possible logical 0/1 results:

It is working fine, without any problems, but If I want to refer to it from my Summarization the corresponding simple Go template not works (‘not works’ mean, I get no real and expected results but simply the template string which used to be in case of any errors in the template).

Here is my sample/test template…

{{ if eq .Values.alert 0 ) }} Everything is fine {{ end }}
{{ if ne .Values.alert 0 ) }} There are problems {{ end }}

…which you can see as Summary annotation:

I can get the result of the .Values.alert key, it is finely show me 0 or 1 based on its state, but the mentioned template simply now works. My first idea was a type mismatch between the comparable values (if the result could not be ‘int’), but tried with forced ‘float’ comparition the result is the same, non working template:

{{ if eq .Values.alert 0.0 ) }} Everything is fine {{ end }}

If I try to ask it about its type I get the following:

Go template:

{{ printf "%T" .Values.alert }}



What is it, how could I handle it, what is my mistake?

Thanks for your attentions in advance!

Hello !

I was trying to do something like this :

{{ with $values }}
   {{ range $key, $value := . }}
      {{ if eq $value 0 }}
         {{ $key }} {{ $value }}
      {{ end }}
   {{ end }}
{{ end }}

As in your case, the type of $value was state.templateCaptureValue so the condition was never meeted…

And with Grafana, it appears to be quite difficult to use Golang functions and try to convert state.templateCaptureValue into int type…

To be able to compare, I did this trick :

{{ with $values }}
   {{ range $key, $value := . }}
     {{ $stringValue := print $value }}
      {{ if eq "0" $stringValue }}
         {{ $key }}
      {{ end }}

   {{ end }}
{{ end }}

Regards !

1 Like