How To Use Alert Message Templates in Grafana

Hello! According to your screenshot you don’t have a label with a name “instance”. May be you can try this?
Servicio:{{ $labels.name }} Estado de servicio: {{ $values.D.Value }}

nope :frowning_face:

imagen

Hi @rodrigoparavano,
If you want to get label value from query then you can do with:

{{ $values.<queryname>.Labels.<tag> }}

For example:

On server {{ $values.B.Labels.host }} port {{ $values.B.Labels.port }} is closed (not listening).

would print:

On server monitoringserver port 8282 is closed (not listening).
Alert configuration and output


Bonus:

Dynamic Severity labels

You can also do more advanced stuff like dynamic label value setting based on query values. For example you can set custom label Severity which would have next values:

Fatal for values >= 98
Critical for values >= 95 AND < 98
Warning for values >= 90 AND < 95
NoData if $value.B does not exist

Severity label formula:

{{ if $values.result }}{{ if (ge $values.result.Value 98.0) }}Fatal{{ else if and (lt $values.result.Value 98.0) (ge $values.result.Value 90.0) }}Critical{{ else if and (lt $values.result.Value 90.0) (ge $values.result.Value 85.0) }}Warning{{ else }}None{{ end }}{{ else }}NoData{{ end }}

Note that here my query in grafana alert is called result. Be aware that every time new severity label is created (for example Severity=Critical) a new alert check will be created and the alert with old Severity label (e.g. Severity=Warning) will be resolved with "grafana_state_reason": "MissingSeries". Newly created check would first go to pending (if you set it) and then to alerting state.

In order to avoid quick changes of Severity label (and thus avoiding alerts being in pending mode) I usually use mean function in my query with time range of 5 minutes (mean of values over larger time range provides more stable number but it is slower at alerting). Also, I always first round numbers (with Math expression) and then perform Threshold expression over rounded numbers. Reason behind is also to avoid fast label changing.

Last thing to mention is part of formula which checks if data in result variable exists:

{{ if $values.result }}........{{ else }}NoData{{ end }}

This is used so that when data is missing (e.g. agent has crashed or somebody stopped it) you get Severity=NoData instead of broken formula like on next picture:

Hope this helps. :slight_smile:

Best regards,
ldrascic

thks!!! but, no value persist haha

i dont know why dont see de labels grrr

my querys configs:

Hi! It looks like you are using Classic Conditions, which because can have multiple conditions (i.e. an average, a min, and a max all together) you need to append the index of the condition too. For example, in your screenshot you would use:

{{ $values.D0.Labels.name }}

to get the name label for query A.

1 Like

thanks to everyone!

i stop using classics condition and use reduce and math, its more easy!

my last cuestion is how to use math condition when i have more than one expression to evaluate.I need to evaluate with “OR”. for example:

$E < 1 or $F < 1 or $G < 1

You use || for or and && for and

1 Like

Just to expand what @georgerobinson said…

If for example you want to trigger an alert any time the value of B is less than 30 or more than 60, then you would write as $B < 30 || $B > 60 :

1 Like

Great!

Query:

like this:

or like this:

As this topic interests a lot of members, I created a post here to share examples of alerting templates.
Feel free to give your feedback in answers there.

1 Like