-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 timezone support to time intervals. #2782
Add timezone support to time intervals. #2782
Conversation
9ac5bea
to
6801a4c
Compare
It seems I've fixed the tests that were failing, but I'm not sure what's going wrong with the 'mixin' part of the CI pipeline, it looks unrelated to my changes... |
@benridley looks like this was addressed in #2785 so a rebase on top of main should resolve this for you |
6801a4c
to
331a818
Compare
Thanks @philipgough, I've just rebased so we'll see how it goes. |
docs/configuration.md
Outdated
@@ -284,6 +285,11 @@ Inclusive on both ends. | |||
`year_range`: A numerical list of years. Ranges are accepted. For example, `['2020:2022', '2030']`. | |||
Inclusive on both ends. | |||
|
|||
`time_zone`: A string that matches an IANA time zone name. For example, | |||
`'Australia/Sydney'`. The time zone will be used to You may also use `'Local'` to use the local time where Alertmanager is running or `'UTC'`. **Note:** On Windows, only |
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.
The time zone will be used to You may also use
'Local'
this doesn't read correctly?
timeinterval/timeinterval.go
Outdated
if runtime.GOOS == "windows" && str != "Local" && str != "UTC" { | ||
return fmt.Errorf("unable to load time location. Timezones other than 'Local' and 'UTC' may not be supported on Windows without using a custom timezone file: %w", err) | ||
} | ||
return err |
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.
I'm wondering if we could make the error message more targeted.
if runtime.GOOS == "windows" && str != "Local" && str != "UTC" { | |
return fmt.Errorf("unable to load time location. Timezones other than 'Local' and 'UTC' may not be supported on Windows without using a custom timezone file: %w", err) | |
} | |
return err | |
if runtime.GOOS == "windows" { | |
if zoneinfo := os.GetEnv("ZONEINFO"); zoneinfo != "" { | |
return fmt.Errorf("%w (ZONEINFO=%q)", err, zoneinfo) | |
} | |
return fmt.Errorf("%w (on Windows platforms, you may have to pass the time zone database using the ZONEINFO environment variable, see https://pkg.go.dev/time#LoadLocation for details)", err) | |
} | |
return err |
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.
Thanks @simonpasquier.
Yeah I think that's a good idea. I've also been thinking more, and I'm wondering if we just go the extra distance and embed the timezone database on Windows platforms? Obviously it's not ideal because we can have stale timezone information between releases of AM, but I think that's probably better than nothing. What do you think?
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.
I would defer it for a later PR if people feel that it's really needed. As you said, we're at risk of providing stale information.
As discussed elsewhere, while "timezone" in the context of the TZ database indeed refers to a location (e.g. "Australia/Sydney"), the general understanding of "timezone" is more ambiguous. In many other contexts, a timezone is something like "PDT" or "PST" (or even "PST/PDT" 😱 ). As a concrete example particularly relevant for Go developers, look at the terminology in https://pkg.go.dev/time#Time . Therefore, I suggest to use a word like "location". Even if you come from a TZ database context, you will still understand what that is, while those from other contexts will have a much easier life (not trying to enter "PDT" and wondering why it doesn't work). |
Sure @beorn7, I think that's a good suggestion and keeps the terminology consistent with Go's stdlib. Happy to add this. |
Great feedback from @beorn7 (as usual) on location vs. time zone, +1 for me. |
Hey, wondering if this is still being worked on as we'd need this feature. Also assuming this works well with daylight saving? |
Hey @piotr1212, Yeah it's coming - I was mainly waiting on #2779 because I knew I'd need to rebase and fix this code up once that was merged. I'll probably work on it this weekend and get it tidied up. And yes, it will work fine with daylight savings - Provided you aren't running AM on Windows. |
Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Makes it clear that the default is UTC, but others are supported. Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Previously tests were using time zone names that were unsupported by the RFC822 parser. This switches the tests to use RFC822Z and specifies the zones by number. Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Co-authored-by: Sylvain Rabot <[email protected]> Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
Signed-off-by: Ben Ridley <[email protected]>
3b0836c
to
07ba642
Compare
Signed-off-by: Ben Ridley <[email protected]>
I've just rebased onto main, and also added in the following:
|
@simonpasquier could we go ahead and merge this please ? |
Co-authored-by: Sylvain Rabot <[email protected]> Signed-off-by: Ben Ridley <[email protected]>
Thanks for those test corrections @sylr, I've just merged them. |
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.
We could maybe add a blank import to https://pkg.go.dev/time/tzdata in cmd/alertmanager/main.go to embed a copy of the timezone database but it can be done later if we see fit.
* Add explicit UTC to time interval tests Signed-off-by: Ben Ridley <[email protected]> * Add timezone support to time intervals Signed-off-by: Ben Ridley <[email protected]> * Update time interval documentation with time zone info Signed-off-by: Ben Ridley <[email protected]> * Refactor notification tests to test timezone support Signed-off-by: Ben Ridley <[email protected]> * Make use of Local more clear Signed-off-by: Ben Ridley <[email protected]> * Fix documentation about timezone support. Makes it clear that the default is UTC, but others are supported. Signed-off-by: Ben Ridley <[email protected]> * Remove commented/unused function Signed-off-by: Ben Ridley <[email protected]> * Fix tests using incorrect timezones Previously tests were using time zone names that were unsupported by the RFC822 parser. This switches the tests to use RFC822Z and specifies the zones by number. Signed-off-by: Ben Ridley <[email protected]> * Add a few more timezone test cases Signed-off-by: Ben Ridley <[email protected]> * Remove unnecessary if/else branch Co-authored-by: Sylvain Rabot <[email protected]> Signed-off-by: Ben Ridley <[email protected]> * Rename timezone to location for consistency with Go stdlib Signed-off-by: Ben Ridley <[email protected]> * Make Windows timezone error more specific Signed-off-by: Ben Ridley <[email protected]> * Update docs to use 'location' Signed-off-by: Ben Ridley <[email protected]> * Apply suggestions from code review Co-authored-by: Sylvain Rabot <[email protected]> Signed-off-by: Ben Ridley <[email protected]> Signed-off-by: Ben Ridley <[email protected]> Co-authored-by: Sylvain Rabot <[email protected]> Signed-off-by: Yijie Qin <[email protected]>
This PR addresses #2771 by adding a
time_zone
field to time interval definitions, which accepts an IANA time zone name (like 'Australia/Sydney') and if supplied means that the time intervals are defined inside that time zone.On Windows machines the only time zones that are accepted are 'Local' and 'UTC', but I think that is probably enough for 99% of users who just want to use their local server time zone. I have noted this in the documentation, and also added some more info to the error message when a timezone fails to load on Windows mentioning that the user may need to supply their own time zone database.
I think it's also probably worth changing the default to be the server's local time rather than UTC as this would probably be unexpected for most users, but it is breaking behavior so I'm not sure how we should proceed with that but open to the ideas.
Feedback on the approach (particularly dealing with Windows) is more than welcome. Cheers!