Docs: Update docs on comparisions with $values (#74156)

This commit is contained in:
George Robinson 2023-08-31 10:32:09 +01:00 committed by GitHub
parent 3021580d8f
commit 164161b41a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,15 +103,35 @@ If you were to print the value of the expression with RefID `B` in the summary o
The summary will contain just the value:
```
api has an over 5% of responses with 5xx errors: 6.789%
api has over 5% of responses with 5xx errors: 6.78912%
```
However, while `{{ $values.B }}` prints the number 6.789, it is actually a string as you are printing the object that contains both the labels and value for RefID B, not the floating point value of B. To use the floating point value of RefID B you must use the `Value` field from `$values.B`. If you were to humanize the floating point value in the summary of an alert:
However, while `{{ $values.B }}` prints the number 6.78912, it is actually a string as you are printing the object that contains both the labels and value for RefID B, not the floating point value of B. To use the floating point value of RefID B you must use the `Value` field from `$values.B`.
If you were to print the humanized floating point value in the summary of an alert:
```
{{ $labels.service }} has over 5% of responses with 5xx errors: {{ humanize $values.B.Value }}%
```
The summary will contain the humanized value:
```
api has over 5% of responses with 5xx errors: 6.789%
```
You can also compare the floating point value using the `eq`, `ne`, `lt`, `le`, `gt` and `ge` comparison operators:
```
{{ if gt $values.B.Value 50.0 -}}
Critical 5xx error rate
{{ else -}}
Elevated 5xx error rate
{{ end }}
```
When using comparison operators with `$values` make sure to compare it to a floating point number such as `50.0` and not an integer such as `50`. Go templates do not support implicit type coercion, and comparing a floating point number to an integer will break your template.
### No data, execution errors and timeouts
If the query in your alert rule returns no data, or fails because of a datasource error or timeout, then any Threshold, Reduce or Math expressions that use that query will also return no data or an error. When this happens these expression will be absent from `$values`. It is good practice to check that a RefID is present before using it as otherwise your template will break should your query return no data or an error. You can do this using an if statement: