How to generate a GUID or current time for my annotations in Prometheus alerting rule templates

79 views
Skip to first unread message

zichen chuh

unread,
May 19, 2020, 11:59:13 PM5/19/20
to Prometheus Users
I went through documents given by prometheus website and didn't find a clue.

From alerting_rules , only 3 variables are available : $lables, $externalLabels, $value.

Thanks in advance.

Julius Volz

unread,
May 20, 2020, 3:12:51 AM5/20/20
to zichen chuh, Prometheus Users
Not sure what exactly your use case is, but if you just want the current (at the time of alert evaluation) Unix timestamp in an annotation, you should be able to do:

{{ with query "time()" }}{{ . | first | value }}{{ end}}

Note that the "Alert" struct on the Alertmanager templating side will also have timestamp fields on it that tell you when an alert first started firing, and when it will expire if it's no longer sent from Prometheus:




--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/d5b9244a-1582-4816-a314-9b7c9917aa10%40googlegroups.com.


--
Julius Volz
PromLabs - promlabs.com

zichen chuh

unread,
May 20, 2020, 6:26:03 AM5/20/20
to Prometheus Users
Hi Julius, 

Thanks for your prompt response. 

{{ with query "time()" }}{{ . | first | value }}{{ end}}

This expression is exactly what I need. 
And I wanna learn how to assemble such an expression. After searching through this document template , still no clue about these in the expression you wrote: query, first, value. Could you give me an advice?




在 2020年5月20日星期三 UTC+8下午3:12:51,Julius Volz写道:
Not sure what exactly your use case is, but if you just want the current (at the time of alert evaluation) Unix timestamp in an annotation, you should be able to do:

{{ with query "time()" }}{{ . | first | value }}{{ end}}

Note that the "Alert" struct on the Alertmanager templating side will also have timestamp fields on it that tell you when an alert first started firing, and when it will expire if it's no longer sent from Prometheus:




On Wed, May 20, 2020 at 5:59 AM zichen chuh <ziche...@gmail.com> wrote:
I went through documents given by prometheus website and didn't find a clue.

From alerting_rules , only 3 variables are available : $lables, $externalLabels, $value.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to promethe...@googlegroups.com.

Christian Hoffmann

unread,
May 20, 2020, 1:48:52 PM5/20/20
to zichen chuh, Prometheus Users
Hi,

On 5/20/20 12:26 PM, zichen chuh wrote:
> {{ with query "time()" }}{{ . | first | value }}{{ end}}
>
> This expression is exactly what I need. 
> And I wanna learn how to assemble such an expression. After searching
> through this document template <https://golang.org/pkg/text/template/> ,
> still no clue about these in the expression you wrote: query, first,
> value. Could you give me an advice?

You already got the base for the template syntax (golang.org) and some
very specific documentation about the alert data structures and their
methods.

You might be missing the part in-between, i.e. the documentation which
describes Prometheus-providede helper functions:
https://prometheus.io/docs/prometheus/latest/configuration/template_reference/

The example section right next to it is also helpful:
https://prometheus.io/docs/prometheus/latest/configuration/template_examples/


Kind regards,
Christian

zichen chuh

unread,
May 21, 2020, 2:30:35 AM5/21/20
to Prometheus Users
Learned.
Thanks very much.

zichen chuh

unread,
Jun 5, 2020, 3:48:18 AM6/5/20
to Prometheus Users
hi Christian,

I still wanna generate a GUID. After going through these documents, couldn't find any.
Wonder u have a clue.


在 2020年5月21日星期四 UTC+8上午1:48:52,Christian Hoffmann写道:

Christian Hoffmann

unread,
Jun 5, 2020, 3:56:07 AM6/5/20
to zichen chuh, Prometheus Users
Hi,

On 6/5/20 9:48 AM, zichen chuh wrote:
> I still wanna generate a GUID. After going through these documents,
> couldn't find any.
> Wonder u have a clue.

Haven't tried it, but you could check if you can access the alert's
fingerprint and if this is what you want.

This seems to be possible in webhooks since v0.19 and seems to be
possible in notification templates as well.

https://github.com/prometheus/alertmanager/issues/2227
https://github.com/prometheus/alertmanager/blob/66a0ed21bdb0720b4ba083d35acd6ae77fa7b0b5/template/template.go#L251

Kind regards,
Christian

zichen chuh

unread,
Jun 5, 2020, 4:13:29 AM6/5/20
to Prometheus Users
Hi Julius,

Let me explain my use case.
For notices that alert-manger sent by receivers, I'm going to write a webhook to store them and show them on a website.  
Let's say notice-A got sent by a mail-receiver and a wechat-receiver to a user. What I wanna do is to enable the user to be able to click a link within the notice-A, to navigate it on the website.
Apparently every notice should have an ID that is generated by the Prometheus, which evaluate the Alert Expression and send alerts to alert-manager.
After going through all documents I have, still no luck to find such a function to generate a GUID for me.


在 2020年5月20日星期三 UTC+8下午3:12:51,Julius Volz写道:
Not sure what exactly your use case is, but if you just want the current (at the time of alert evaluation) Unix timestamp in an annotation, you should be able to do:

{{ with query "time()" }}{{ . | first | value }}{{ end}}

Note that the "Alert" struct on the Alertmanager templating side will also have timestamp fields on it that tell you when an alert first started firing, and when it will expire if it's no longer sent from Prometheus:



On Wed, May 20, 2020 at 5:59 AM zichen chuh <ziche...@gmail.com> wrote:
I went through documents given by prometheus website and didn't find a clue.

From alerting_rules , only 3 variables are available : $lables, $externalLabels, $value.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to promethe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages