From fef150f1b5e48652ec6779e2129ae9a0cf13db8a Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Mon, 8 Jul 2019 02:43:59 -0700 Subject: [PATCH] Add tests to ensure we can marshal and unmarshal our min/max times (#5734) * Add tests to ensure we can marshal and unmarshal our min/max times Related to https://github.com/prometheus/client_golang/issues/614 Instead of implementing all the time parsing, we can special-case handle these 2 times. This means if times in this format show up that time.Parse can't handle they will still error, but we can marshal/parse our own min/max time Signed-off-by: Thomas Jackson --- web/api/v1/api.go | 18 ++++++++++++++++-- web/api/v1/api_test.go | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index d4523eac49..8b21c313a4 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -439,8 +439,11 @@ func (api *API) labelValues(r *http.Request) apiFuncResult { } var ( - minTime = time.Unix(math.MinInt64/1000+62135596801, 0) - maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999) + minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC() + maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC() + + minTimeFormatted = minTime.Format(time.RFC3339Nano) + maxTimeFormatted = maxTime.Format(time.RFC3339Nano) ) func (api *API) series(r *http.Request) apiFuncResult { @@ -1162,6 +1165,17 @@ func parseTime(s string) (time.Time, error) { if t, err := time.Parse(time.RFC3339Nano, s); err == nil { return t, nil } + + // Stdlib's time parser can only handle 4 digit years. As a workaround until + // that is fixed we want to at least support our own boundary times. + // Context: https://github.com/prometheus/client_golang/issues/614 + // Upstream issue: https://github.com/golang/go/issues/20555 + switch s { + case minTimeFormatted: + return minTime, nil + case maxTimeFormatted: + return maxTime, nil + } return time.Time{}, errors.Errorf("cannot parse %q to a valid timestamp", s) } diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 11d2802b1a..a53f5fed2d 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -1331,6 +1331,14 @@ func TestParseTime(t *testing.T) { input: "1543578564.705", result: time.Unix(1543578564, 705*1e6), }, + { + input: minTime.Format(time.RFC3339Nano), + result: minTime, + }, + { + input: maxTime.Format(time.RFC3339Nano), + result: maxTime, + }, } for _, test := range tests {