-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add date and tz functions to templates #3812
Conversation
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]>
Perhaps we should add a We will pass a time offset in the "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"
},
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
} |
@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", | |||
}, { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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: