Skip to content

Commit c52580d

Browse files
committed
Change all time formatting to UTC and off of time.RFC3339Nano
Prometheus has issues parsing RFC3339Nano timestamps if the year has more than 4 digits, in addition it is the second-pass parse attempt. Since this is a client library and the interface is a `time.Time` it makes sense that we pick the clearest simplest format-- so I propose we use the `model.Time` representation of time in our communications to prometheus. This (1) removes the issues with timezones in those queries going downstream and (2) completely works around this #614 issue as the parsing mechanism in prometheus can handle those times in this format. Related to #614 Signed-off-by: Thomas Jackson <[email protected]>
1 parent a6c6979 commit c52580d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

api/prometheus/v1/api.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime
594594
q.Add("match[]", m)
595595
}
596596

597-
q.Set("start", startTime.Format(time.RFC3339Nano))
598-
q.Set("end", endTime.Format(time.RFC3339Nano))
597+
q.Set("start", formatTime(startTime))
598+
q.Set("end", formatTime(endTime))
599599

600600
u.RawQuery = q.Encode()
601601

@@ -659,7 +659,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
659659

660660
q.Set("query", query)
661661
if !ts.IsZero() {
662-
q.Set("time", ts.Format(time.RFC3339Nano))
662+
q.Set("time", formatTime(ts))
663663
}
664664

665665
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
@@ -675,16 +675,10 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
675675
u := h.client.URL(epQueryRange, nil)
676676
q := u.Query()
677677

678-
var (
679-
start = r.Start.Format(time.RFC3339Nano)
680-
end = r.End.Format(time.RFC3339Nano)
681-
step = strconv.FormatFloat(r.Step.Seconds(), 'f', 3, 64)
682-
)
683-
684678
q.Set("query", query)
685-
q.Set("start", start)
686-
q.Set("end", end)
687-
q.Set("step", step)
679+
q.Set("start", formatTime(r.Start))
680+
q.Set("end", formatTime(r.End))
681+
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', 3, 64))
688682

689683
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
690684
if err != nil {
@@ -704,8 +698,8 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.T
704698
q.Add("match[]", m)
705699
}
706700

707-
q.Set("start", startTime.Format(time.RFC3339Nano))
708-
q.Set("end", endTime.Format(time.RFC3339Nano))
701+
q.Set("start", formatTime(startTime))
702+
q.Set("end", formatTime(endTime))
709703

710704
u.RawQuery = q.Encode()
711705

@@ -877,3 +871,7 @@ func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, [
877871
return resp, []byte(result.Data), warnings, err
878872

879873
}
874+
875+
func formatTime(t time.Time) string {
876+
return strconv.FormatFloat(float64(t.UnixNano())/1e9, 'f', -1, 64)
877+
}

0 commit comments

Comments
 (0)