Template function not available in alerting annotations?

|###
Background

I want use the reReplaceAll in alerting annotation
Because I have a timestamp in scientific notation, be used for part of URL.

Example:

timestamp: 1.6613856e+12


* ```
URL: http://myhost?param=1.6613856e**%2B**12

As you see, I need escape “+” to “%2B”
So that I found template function reReplaceAll.

But the reReplaceAll didn’t work as expected.

We get the value from ES data source
$values.E0.Value is 1.6613856e+12

What happened:
image
image

What you expected to happen:
Below values should be :

Test_fixed: 1.6613352e%2B12

Test_single_value: 1.6613352e

Test_single_value_fixed: 1.6613352e

Test_v2: 1.6613352e12

Test_v3: 1.6613352e+12

Test_v3_fixed_value: 1.6613352e+12

How to reproduce it (as minimally and precisely as possible):

  1. setup an ES data source.
  2. setup an alerting rule, use above data source.
  3. config annotations in alerting, such as above image.
  4. waiting alert email…

Anything else we need to know?:
None

Environment:

  • Grafana version: 9.0.6, build based on “grafana/grafana:9.0.6-ubuntu”. ubuntu.Dockerfile
  • Data source type & version: ElasticSearch
  • OS Grafana is installed on: ubuntu
  • User OS & Browser: MAC OS, chrome
  • Grafana plugins: none
  • Others:|
1 Like

Just a comment: It seems that the $values.X format does not work. The variable $values is an internal Grafana map and for some reason is not recognized by the template functions. Myself I solved this by getting the good-old Prometheus variable $value and parse it. Still, Grafana should solve the issue somehow, but this workaround can save the day.

Per instance, given that $value contains something of the form:

[ var=‘B’ labels={instance=XXX} value=X.XXXXXXXXXXXXXXX], [ var=‘C’ labels={instance=XXX} value=X ]

(the order of 'B" and ‘C’ can differ), one could get the numeric value of the (reduced) metric up to two decimal points like this:

{{ reReplaceAll “.*\\[ var=‘B’ [^\\]]*value=([0-9]+(\\.[0-9]{2})?).*” “$1” $value }}

(Mind the double \ to escape reserved characters)