alertmanager icon indicating copy to clipboard operation
alertmanager copied to clipboard

Pass Go's `time` function into the alertmanager templates to be able to do time calculations

Open aboukhal opened this issue 1 year ago • 15 comments

Proposal

Currently, it's not possible to do time calculations when sending alerts to Slack for example. Explicitly converting to another time zone or calculating a duration between startsAt and now would be a tremendous help. There is nothing on Slack/Mattermost's side to do any conversions or calculations there.

This would be easily solved by passing the default time functions into the template at this location. https://github.com/prometheus/alertmanager/blob/680568b518ddd728f46101952e006e7b13910901/template/template.go#L170

aboukhal avatar Mar 12 '24 09:03 aboukhal

We added date and timezone functions here.

grobinson-grafana avatar Apr 25 '24 18:04 grobinson-grafana

This is awesome. Although, since so little from time is passed through, calculations are still not possible.

aboukhal avatar Apr 25 '24 19:04 aboukhal

Perhaps I misunderstood, what calculations would you like to do?

grobinson-grafana avatar Apr 25 '24 21:04 grobinson-grafana

As I wrote initially:

or calculating a duration between startsAt and now

In an alert I would like to create text, such as "started 3 hours, 22 minutes ago". This is especially helpful for a repeated message, to see that this is ongoing instead of new, or for the resolved message, to have a clear documentation of how long an issue was present.

aboukhal avatar Apr 27 '24 04:04 aboukhal

As I wrote initially:

or calculating a duration between startsAt and now

In an alert I would like to create text, such as "started 3 hours, 22 minutes ago". This is especially helpful for a repeated message, to see that this is ongoing instead of new, or for the resolved message, to have a clear documentation of how long an issue was present.

Oh OK. You want a humanize function / or ago function. I think we can add that!

grobinson-grafana avatar Apr 27 '24 09:04 grobinson-grafana

Keep in mind the started at text will be relative to the time the notification was sent. So if the alert started at 09:00 UTC, and the notification was sent at 10:00 UTC, then the text would be "started one hour ago". However, suppose you don't see the notification until 11:00 UTC, the text will still say "started one hour ago" even though it actually started two hours ago.

grobinson-grafana avatar Apr 27 '24 10:04 grobinson-grafana

Related:

https://github.com/prometheus/alertmanager/issues/3717

https://github.com/prometheus/alertmanager/pull/3720

https://github.com/prometheus/common/pull/627

bryanasdev000 avatar May 13 '24 06:05 bryanasdev000

Keep in mind the started at text will be relative to the time the notification was sent. So if the alert started at 09:00 UTC, and the notification was sent at 10:00 UTC, then the text would be "started one hour ago". However, suppose you don't see the notification until 11:00 UTC, the text will still say "started one hour ago" even though it actually started two hours ago.

Yes, this is expected and is consistent with how chatops usually displays things.

aboukhal avatar May 13 '24 11:05 aboukhal

Is there a specific design decision why you don't want to pass though the entire time through, instead of covering the use cases one by one? That way the end user would be able to do anything that is covered by Go's time functions.

aboukhal avatar May 13 '24 11:05 aboukhal

Hi! :wave: Most of the functions in time are methods, so you should be able to use them. We don't pass all of the functions in because there are some we don't want to add to templates like time.Sleep.

grobinson-grafana avatar May 13 '24 14:05 grobinson-grafana

I want to add time.Since and pretty formatting a duration and filed an issue on that here: https://github.com/prometheus/alertmanager/issues/3717 We agreed to move humanizeDuration from prometheus to common package to reuse it here, so I did this https://github.com/prometheus/common/pull/627 but it's still in review. After this is done and merged, we can import it here and use it here as well.

Can someone from Alertmanager team review it? (@grobinson-grafana maybe?)

freak12techno avatar May 13 '24 14:05 freak12techno

I can do a review! 🙂 I can't merge it as I'm not an Alertmanager maintainer, but I'll let @gotjosh know when I've reviewed it for him to look at too.

grobinson-grafana avatar May 13 '24 15:05 grobinson-grafana

I believe this issue is fixed by #3863. I would love to test it - is there a schedule for the next release? If it works as advertised, I'll happily close this ticket myself. Thanks for your effort, @freak12techno

aboukhal avatar Aug 06 '24 13:08 aboukhal

@aboukhal yes, you can build from source to test it. As per the new release, I am not an Alertmanager maintainer, so I do not know. @gotjosh can you maybe ping someone or answer here?

freak12techno avatar Aug 06 '24 13:08 freak12techno

It's... challenging to build external projects in our environment to say the least. Otherwise I would have been more actively involved in this issue from the start. So unfortunately I can only test it once it's released.

aboukhal avatar Aug 06 '24 13:08 aboukhal