feat: add time helpers
Fixes #3717.
The humanizeDirection helper is 100% copied from Prometheus, the since is added by me.
The tests are also copied from Prometheus and adapted, plus I added my own since test.
Tested it locally: creating an alert like this one:
./amtool alert add --alertmanager.url http://localhost:9093 alertname=aaa host=test instance=test3
with a template like this one
{{ define "alerts_list" }}
{{ range . }}{{ if index .Labels "host" -}}
đĨī¸ Host: {{ index .Labels "host" }} ({{ index .Labels "instance" }})
{{- else -}}
đĨī¸ Host: {{ index .Labels "instance" }}
{{- end }}
{{ if eq (index .Labels "severity") "critical" -}}
đ´ Severity: critical
{{- else -}}
đ Severity: warning
{{- end }}
đ Alert: {{ index .Labels "alertname" }}
âšī¸ Details: {{ index .Annotations "summary" }}
â Firing since: {{ .StartsAt }} (for {{ .StartsAt | since | humanizeDuration }})
đ Description: {{ index .Annotations "description" }}
Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
<a href="{{ .GeneratorURL }}">Source</a>
{{ end }}
{{ end }}
produces the following output:
đĨ Alerts Firing
đĨī¸ Host: test (test3)
đ Severity: warning
đ Alert: aaa
âšī¸ Details:
â Firing since: 2024-02-13 22:22:49.535404 +0300 MSK m=+4.535759668 (for 10.01s)
đ Description:
Labels:
- alertname = aaa
- host = test
- instance = test3
Source
@gotjosh can you review or ping somebody to do that?
@freak12techno how do you feel about moving the helper into https://github.com/prometheus/common so that we don't have to copy/pate here?
@gotjosh I am ok with this, I can do the following:
- make a PR on prometheus/common moving what I wrote here to there
- make a PR on this repo utilizing prometheus/common methods
- make a PR on prometheus/prometheus deleting the code from there and utilizing the one on prometheus/common
does this sound decent?
Sounds perfect to me!
I'll close this one as it'll be required to rewrite it anyways.