Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add time helpers #3720

Closed
wants to merge 2 commits into from

Conversation

freak12techno
Copy link
Contributor

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

Signed-off-by: Techno Freak <freak12techno@gmail.com>
{
title: "Template using since",
in: "{{ . | since | humanizeDuration }}",
data: time.Now().Add(-1 * time.Hour),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I am not sure how to test this one properly without mocking time.Since, which as far as I know cannot be done without some kinds of workaround, this one looks ugly and flaky (as it relies on local time and will fail if the time between creating this variable and rendering is more than 1 second), but it kinda works, let me know if there's a better way to do it

Signed-off-by: Techno Freak <freak12techno@gmail.com>
@freak12techno
Copy link
Contributor Author

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

@gotjosh
Copy link
Member

gotjosh commented Feb 28, 2024

@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?

@freak12techno
Copy link
Contributor Author

@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?

@gotjosh
Copy link
Member

gotjosh commented Feb 28, 2024

Sounds perfect to me!

@freak12techno
Copy link
Contributor Author

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

@freak12techno freak12techno deleted the add-time-helpers branch May 13, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing a way to use time.Since and pretty format duration in templates
2 participants