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 timezone support to time intervals. #2782

Merged
merged 14 commits into from Sep 22, 2022

Conversation

benridley
Copy link
Contributor

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!

@benridley benridley force-pushed the feature_timeinterval_timezone branch 3 times, most recently from 9ac5bea to 6801a4c Compare December 8, 2021 08:43
@benridley
Copy link
Contributor Author

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...

@philipgough
Copy link
Contributor

@benridley looks like this was addressed in #2785 so a rebase on top of main should resolve this for you

@benridley
Copy link
Contributor Author

Thanks @philipgough, I've just rebased so we'll see how it goes.

timeinterval/timeinterval.go Outdated Show resolved Hide resolved
timeinterval/timeinterval_test.go Show resolved Hide resolved
@@ -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
Copy link
Member

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?

Comment on lines 184 to 191
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
Copy link
Member

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.

Suggested change
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

Copy link
Contributor Author

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?

Copy link
Member

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.

@beorn7
Copy link
Member

beorn7 commented Jan 29, 2022

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).

@benridley
Copy link
Contributor Author

Sure @beorn7, I think that's a good suggestion and keeps the terminology consistent with Go's stdlib. Happy to add this.

@simonpasquier
Copy link
Member

Great feedback from @beorn7 (as usual) on location vs. time zone, +1 for me.

@piotr1212
Copy link

Hey, wondering if this is still being worked on as we'd need this feature. Also assuming this works well with daylight saving?

@benridley
Copy link
Contributor Author

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.

benridley and others added 12 commits March 14, 2022 19:17
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Makes it clear that the default is UTC, but others are supported.

Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
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 <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Co-authored-by: Sylvain Rabot <sylvain@abstraction.fr>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
@benridley benridley force-pushed the feature_timeinterval_timezone branch from 3b0836c to 07ba642 Compare March 14, 2022 08:17
Signed-off-by: Ben Ridley <benridley29@gmail.com>
@benridley
Copy link
Contributor Author

benridley commented Mar 14, 2022

I've just rebased onto main, and also added in the following:

  • Renamed Timezone/time_zone to Location/location as its a more accurate description and consistent with Go's stdlib (thanks @beorn7)
  • Added in @simonpasquier's suggestion to be more specific with the Windows error and check.
  • Fixed up the docs to use the 'location' keyword, and also added some more details about what it does.

@sylr
Copy link
Contributor

sylr commented Sep 10, 2022

@simonpasquier could we go ahead and merge this please ?

notify/notify_test.go Outdated Show resolved Hide resolved
notify/notify_test.go Outdated Show resolved Hide resolved
timeinterval/timeinterval.go Outdated Show resolved Hide resolved
timeinterval/timeinterval_test.go Outdated Show resolved Hide resolved
Co-authored-by: Sylvain Rabot <sylvain@abstraction.fr>
Signed-off-by: Ben Ridley <benridley29@gmail.com>
@benridley
Copy link
Contributor Author

Thanks for those test corrections @sylr, I've just merged them.

Copy link
Member

@simonpasquier simonpasquier left a 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.

@simonpasquier simonpasquier merged commit 33a0e77 into prometheus:main Sep 22, 2022
qinxx108 pushed a commit to qinxx108/alertmanager that referenced this pull request Dec 13, 2022
* Add explicit UTC to time interval tests

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Add timezone support to time intervals

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Update time interval documentation with time zone info

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Refactor notification tests to test timezone support

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Make use of Local more clear

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Fix documentation about timezone support.

Makes it clear that the default is UTC, but others are supported.

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Remove commented/unused function

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* 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 <benridley29@gmail.com>

* Add a few more timezone test cases

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Remove unnecessary if/else branch

Co-authored-by: Sylvain Rabot <sylvain@abstraction.fr>
Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Rename timezone to location for consistency with Go stdlib

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Make Windows timezone error more specific

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Update docs to use 'location'

Signed-off-by: Ben Ridley <benridley29@gmail.com>

* Apply suggestions from code review

Co-authored-by: Sylvain Rabot <sylvain@abstraction.fr>
Signed-off-by: Ben Ridley <benridley29@gmail.com>

Signed-off-by: Ben Ridley <benridley29@gmail.com>
Co-authored-by: Sylvain Rabot <sylvain@abstraction.fr>
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
@simonpasquier simonpasquier mentioned this pull request Dec 16, 2022
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

6 participants