Hi Sebastian,

Ah yes, the issue is that different data is available on the Prometheus
side in alerting rules, than in notification templates on the Alertmanager
side.

* For alerting rules, see:
https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating.
So you only get: $labels, $externalLabels, and $value as variables.

* For Alertmanager notification templates, you get a whole list of alerts
with some more fields:
https://prometheus.io/docs/alerting/latest/notifications/#data

So if you want to access the ".StartsAt" field of an alert, you would have
to access that from an Alertmanager notification template.

As a very simplistic example, you could set the "html" field of an
"email_config" in alertmanager.yml to something like this to range over all
alerts in the notification and output the ".StartsAt" field for each of
them:

  html: "{{ range .Alerts }}{{ .StartsAt }}<br/>{{ end }}"

Of course in practice you probably also want to put in other alert info
(labels, annotations, etc.) in that loop. See the default for the "html"
field in
https://github.com/prometheus/alertmanager/blob/main/template/email.tmpl as
inspiration.

Cheers,
Julius



On Wed, May 4, 2022 at 9:42 AM Sebastian Osztovits <
[email protected]> wrote:

> Hi,
> As you have guessed I want to know the time when the alert first started
> firing.
> I simply tried adding the alerting rules as in example :
>
>
>   - alert: InstanceDown
>     expr: up == 0
>     for: 5m
>     labels:
>       severity: critical
>     annotations:
>       timestamp: >
>         time:  {{ .StartsAt }}
>
> Which leaves me with the error:
> = <error expanding template: error executing template
> __alert_InstanceDown: template: __alert_InstanceDown:1:90: executing
> "__alert_InstanceDown" at <.StartsAt>: can't evaluate field StartsAt in
> type struct { Labels map[string]string; ExternalLabels map[string]string;
> Value float64 }>
>
> Regards,
> Sebastian
>
>
>
> On Tuesday, May 3, 2022 at 9:47:25 PM UTC+2 [email protected] wrote:
>
>> Hi Sebastian,
>>
>> What error are you getting and what's your config? The first answer puts
>> the current time of the evaluation of the alerting rule into the "time"
>> annotation (which wouldn't necessarily be the time the alert started
>> firing, but the last time it was still evaluated as firing). The second
>> Stack Overflow answer is maybe more what you want, accessing the
>> ".StartsAt" field of an alert directly from an Alertmanager notification
>> template, and thus telling you when the alert first started firing. If you
>> share what you already have (and which kind of timestamp you expect), maybe
>> we can help you make it work.
>>
>> Regards,
>> Julius
>>
>> On Tue, May 3, 2022 at 11:49 AM Sebastian Osztovits <
>> [email protected]> wrote:
>>
>>>
>>> Hello,
>>> I'm currently using Alert manager 0.22.2 and Prometheus 2.18.1.
>>> I have tried to alter my alert as shown in stack overflow  post:
>>>
>>>
>>> https://stackoverflow.com/questions/41069912/populating-a-date-time-in-prometheus-alert-manager-e-mail
>>> yet i am met with an error message when doing so.
>>> Does anyone have and email Template where they successfully added a
>>> Timestamp?
>>> Thanks in advance.
>>> Best Regards,
>>> Sebastian
>>>
>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/prometheus-users/e03a03be-4d88-4ded-b969-f36a06c469f2n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/prometheus-users/e03a03be-4d88-4ded-b969-f36a06c469f2n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Julius Volz
>> PromLabs - promlabs.com
>>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/prometheus-users/a2631779-dd7c-40fe-897e-7de27d8c7c0dn%40googlegroups.com
> <https://groups.google.com/d/msgid/prometheus-users/a2631779-dd7c-40fe-897e-7de27d8c7c0dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Julius Volz
PromLabs - promlabs.com

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/CAObpH5ypV_MYjQStHAQ3GW4ReKoDm3GKf8EM5fEGEWpw8PBt_A%40mail.gmail.com.

Reply via email to