alertmanager icon indicating copy to clipboard operation
alertmanager copied to clipboard

feat: add time helpers

Open freak12techno opened this issue 1 year ago â€ĸ 4 comments

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

freak12techno avatar Feb 13 '24 19:02 freak12techno

@gotjosh can you review or ping somebody to do that?

freak12techno avatar Feb 24 '24 00:02 freak12techno

@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 avatar Feb 28 '24 11:02 gotjosh

@gotjosh I am ok with this, I can do the following:

  1. make a PR on prometheus/common moving what I wrote here to there
  2. make a PR on this repo utilizing prometheus/common methods
  3. make a PR on prometheus/prometheus deleting the code from there and utilizing the one on prometheus/common

does this sound decent?

freak12techno avatar Feb 28 '24 12:02 freak12techno

Sounds perfect to me!

gotjosh avatar Feb 28 '24 13:02 gotjosh

I'll close this one as it'll be required to rewrite it anyways.

freak12techno avatar May 13 '24 14:05 freak12techno