Chintaman
Chintaman

Reputation: 101

How to iterate alerts in Prometheus Alertmanager?

I am trying to get timestamp (value of StartsAt) into email body of any alert. There is need to iterate alerts, to get timestamp from element. Need help, how to do that properly, so that exact timestamp of that generated alert will be collected in description of yaml file?

Upvotes: 2

Views: 6186

Answers (2)

Chintaman
Chintaman

Reputation: 101

- alert: Alert
  for: 5m
  expr: ...
  annotations:
    timestamp: >
      time: {{ with query "time()" }}{{ . | first | value | humanizeTimestamp }}{{ end }}

Instead of iterating alerts, I have solved this problem in above way. It works, and I am able to get timestamp in email body. Cheers.!

Upvotes: 0

Alin Sînpălean
Alin Sînpălean

Reputation: 10084

Here's a sample from the Prometheus documentation (for how to iterate over all alerts): https://prometheus.io/docs/alerting/notification_examples/#ranging-over-all-received-alerts

Copy-paste that, and replace .Annotations.summary with .startsAt. I.e.

"{{ range .Alerts }}{{ .StartsAt }}\n{{ end }}"

For reference, the data structures available to Alertmanager notification templates are documented here: https://prometheus.io/docs/alerting/notifications/

Upvotes: 2

Related Questions