How to Format Alert Message Array

Hello guys,

I’m trying to format an alert message in “Summary and Annotations” on Grafana with no success.

I’m getting the Value below in the message:

Value: [ var='B0' metric='value' labels={host.name=ipA, metric.application=appA} value=0.8333333333333334 ], [ var='B1' metric='value' labels={host.name=ipB, metric.application=appB} value=0.81 ]

I would like to format the message like:

The disks are almost full for the applications below:
appA - ipA - 83%
appB - ipB - 81%

I have tried so many ways to get the values like:

  • {{ humanizePercentage $labels.value }}
  • {{ $values.host.name }}
  • {{ $value.host.name }}
  • {{ $value.B0.labels.host.name }}
  • etc.

Someone knows how to format the message in this case?

Regards

Welcome @everaldorodrigo

Can you try this?

The disks are almost full for the applications below:
{{ $values.A.Labels.metric.application }} - {{ $values.A.Labels.host.name }} - {{  printf "%.0f"  $values.A.Value }}%
{{ $values.B.Labels.metric.application }} - {{ $values.B.Labels.host.name }} - {{  printf "%.0f"  $values.B.Value }}%

Thank you so much @grant2.

I added it to the Description and I’m getting the message below:

- description = The disks are almost full for the applications below:
<no value> - <no value> - %!f(<nil>)%
<no value> - <no value> - %!f(<nil>)%

I also tried looping the values and didn’t work. Like:

{{ range .Value}}<strong>{{ .Name }}</strong>: {{ .Value }} {{ end }}

{{ range .Value}}<strong>{{ $labels.host.name }}</strong>: {{ .Value }} {{ end }}

{{ range .Labels}}<strong>{{ .Name }}</strong>: {{ .Value }} {{ end }}

A loop was going to work better in my case because the number of values varies. I was trying to get a specific item first because it will be helpful in other situations. However, I couldn’t get the values in both cases.

I appreciate your help!

Can you paste the text you receive when you click Preview alerts?

I’m receiving the text below

[ var='B0' metric='value' labels={host.name=ipA, metric.application=appA} value=0.8333333333333334 ], [ var='B1' metric='value' labels={host.name=ipB, metric.application=appB} value=0.8333333333333334 ], [ var='B2' metric='value' labels={host.name=ipC, metric.application=appC} value=0.8333333333333334 ]
1 Like

I think the problem may be due the fact that you have a dot ( . ) in the label names. Can you try the same thing with but a label like “hostname” (instead of “host.name”)?

Thank you, however I don’t have labels with no dot (.). And it’s going to be hard to change the labels name or add other labels :frowning:

I guess we will never know.

Unless something changes…

Hi @everaldorodrigo
Can you try
{{ $values.B0.Labels.metric.application }} and {{ $values.B0.Value }}

and
{{ $values.B1.Labels.metric.application }} and {{ $values.B1.Value }}

Hi! If a label has a dot in it use the index function from Go templates:

{{ index $values.B0.Labels "metric.application" }}

Hello
New to the new alerting system - we have tons of alerts and i really dislike the new output
How can we make it print only the relevant alerting label?

Is there a way to control how the alerts are being migrated to the unified alerting system? (we have hundreds of legacy alerts on grafana 8)

Hi! What do you mean by relevant?

Hi,

For example:

Values from alert:
[ var=‘B0’ metric=‘{app=“formulario-24-ms”}’ labels={app=formulario-24-ms} value=1 ]

Message with go template (in spanish):
Logs con presencia de ERROR logging level, revise por posibles errores. Detalle:

{{ with $values }}
{{ range $k, $v := . }}
App: {{$v.Labels.app}}
Errores por minuto: {{ $v }}
{{ end }}
{{ end }}

Warning: labels with dot give a lot of problems.

Suerte!

I was using this templating with GO range but since latest version, new alerting format has 2 expressions: B is a reduce (get single value from query) and C is a threshold (evaluates B).

This format is mandatory for managing silence correctly but as a result, the alert template issues value for both queries for each alerts which is not convenient.

However, using variables in the format {{ $labels.instance }} became possible again (because labels are the same between the 2 expressions.
To get value from var='B', simply use this format {{ $values.B }}.