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

Add date and tz functions to templates #3812

Conversation

grobinson-grafana
Copy link
Contributor

This commit adds the date and tz functions to templates. This means users can now format time in a specified format and also change the timezone to their specific locale.

An example of how these functions work, and can be composed together, can be seen here:

{{ .StartsAt | tz "Europe/Paris" | date "15:04:05 MST" }}

This commit adds the date and tz functions to templates. This means
users can now format time in a specified format and also change
the timezone to their specific locale.

An example of how these functions work, and can be composed together,
can be seen here:

	{{ .StartsAt | tz "Europe/Paris" | date "15:04:05 MST" }}

Signed-off-by: George Robinson <[email protected]>
Signed-off-by: George Robinson <[email protected]>
@verdel
Copy link

verdel commented Apr 19, 2024

Perhaps we should add a duration function? This would, for example, allow generating links to a grafana dashboards where a time range for display can be passed.

We will pass a time offset in the annotation and use it to generate the link in the following way:

"annotations": {
        "title": "Alert Summary text",
        "description": "Detailed alert description text john doe jane doe some veeery long text needed",
        "log_url": "https://grafana.example.com ...from=%from%, to=%to%",
        "grafana_dashboard_range": "-2m"
      },
{{ define "test" }}
{{ .Annotations.message }}
{{- "\\n" -}}
{{ (index .Alerts 0).Annotations.log_url | reReplaceAll "%from%" (printf "%d000" ((index .Alerts 0).StartsAt.Add (duration (index .Alerts 0).Annotations.grafana_dashboard_range)).Unix) | reReplaceAll "%to%" (printf "%d000" (index .Alerts 0).StartsAt.Unix)}}
{{ end }}

The function itself is very simple:

// duration returns the time.Duration representation of a given string
"duration": func(text string) time.Duration {
	d, _ := time.ParseDuration(text)
	return d
}

@grobinson-grafana
Copy link
Contributor Author

@verdel can you open a separate issue for that?

@@ -506,6 +506,16 @@ func TestTemplateFuncs(t *testing.T) {
title: "Template using reReplaceAll",
in: `{{ reReplaceAll "ab" "AB" "abc" }}`,
exp: "ABc",
}, {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add unhappy cases? I'm not sure what happens when you pass a non-valid format to date or when empty value time and an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah sure! Template panics like other functions. As far as I can tell, there is no discernible difference between returning an error and panicking in the function, both result in a panic:

"reReplaceAll": func(pattern, repl, text string) string {
	re := regexp.MustCompile(pattern) // panics if regex is invalid
	return re.ReplaceAllString(text, repl)
},
// tz returns the time in the timezone.
"tz": func(name string, t time.Time) (time.Time, error) {
	loc, err := time.LoadLocation(name)
	if err != nil {
		return time.Time{}, err // panics with err
	}
	return t.In(loc), nil
},

Signed-off-by: George Robinson <[email protected]>
Signed-off-by: George Robinson <[email protected]>
Signed-off-by: George Robinson <[email protected]>
Copy link
Member

@gotjosh gotjosh left a comment

Choose a reason for hiding this comment

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

LGTM

@gotjosh gotjosh merged commit dc1e1a2 into prometheus:main Apr 22, 2024
11 checks passed
@grobinson-grafana grobinson-grafana deleted the grobinson/add-date-and-timezone-functions-to-templates branch June 25, 2024 15:59
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.

None yet

3 participants