How to round to 'two decimals' the value in the new 9.3.1 alert system?

With the latest updates few days ago, my solution to round the values doesn’t work anymore, and I can’t understand why I’m seeing a value with tons of decimals, for example this alert returns me the correct value in Grafana ed everywhere, but when the alert is triggered, Grafana do it with a tons of decimals, here the screenshots to explain:

I tried to use the round operation and other things but no way to fix it.

With 9.2 was working, maybe it’s a bug and not my fault?

Thanks

If your data displays an unnecessarily large number of digits to the right of the decimal (such as 2.355555555555 shown above), and you want your Grafana alerts to be more legible (for example, 2.36), try using {{ printf "%.2f" $v }}

In annotation AlertValues, we could write the following:

{{  with $values }} {{  range $k, $v := . }} CPU: {{ printf "%.2f"  $v }} %
{{ end }} {{ end }}
1 Like

Thanks, bash scripting inside Go? What the… :slight_smile:

Anyway this works for the decimals but it returns me this weird query:

CPU: {map[%!f(string=in):%!f(string=19)] 1.56}%  

using as message:

{{ with $values }}{{ range $k, $v := . }}CPU: {{ printf "%.2f" $v }}% 
{{ end }} {{ end }}

Uploading: Screenshot 2022-12-03 at 14.01.17.jpg…

Maybe @georgerobinson can help figure this one out.

Hi! In this example $v is a structure (it contains Labels and a Value). When we attempt to print this structure in a template with $v it calls the String method on this structure, which returns the floating point value as a string without rounding to some number of decimal places.

{{ with $values }}{{ range $k, $v := . }}CPU: {{ printf "%.2f" $v }}% 
{{ end }} {{ end }}

To use the raw floating point value just change the argument to printf from $v to $v.Value:

{{ with $values }}{{ range $k, $v := . }}CPU: {{ printf "%.2f" $v.Value }}% 
{{ end }} {{ end }}
2 Likes

Yep, what he said . :arrow_up:

Thank you @georgerobinson !

1 Like

Great thanks! This works finally:

@giuliomagnifico

That looks great. Just out of curosity, what is your contact point? MS Teams? Something else?

No I’m using Pushover, I prefer it, I can also handle different sounds for different instances.

I’m still developing it as you can see from these screenshots :sweat_smile:

But when it will be completed it will report all the alerts and values for firing and resolved issues.


If you’re interested I’m witing a “how to”, as soon as I finished I’ll send it to you. It’s not complicated but a bit tricky.

My goal is read only a iOS push notification with the value and if I open the push I get all the details/silence url/dashboard id.

And I don’t know why for some resolved alerts it reports me the value and not for some others.

That’s awesome. The “how to” would definitely be useful for me, so please send to me.