fix(observability-proxy): check metrics points on millisecond bounds

The points limit was checked on the parsed bounds, but vmselect gets
both cut to the millisecond. A start cut back by more than the end
widened the forwarded range, so a 1ms step admitted at 11000 points
reached vmselect as 11001.

Times are now cut to the millisecond when they are parsed, so the
range and points checks and the forwarded request see one window.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-10 03:56:18 +07:00
1 parent 0d3a0487e4
commit 6f5b0b666e
2 files changed
+7 -3

No files matched your search

+4 -3
View File
@@ -264,7 +264,8 @@ func parseMetricsDuration(key, v string) (time.Duration, *HTTPError) {
// parseMetricsTime takes the two forms the Prometheus API defines: float
// unix seconds and RFC3339. vmselect would also guess milliseconds from the
// digit count and accept relative times; a guessed unit silently moves the
// window, so those are refused.
// window, so those are refused. The result is cut to the millisecond, as
// formatTime sends it, so the limits see the window vmselect will use.
func parseMetricsTime(key, v string) (time.Time, *HTTPError) {
if sec, frac, ok := strings.Cut(v, "."); isDigits(sec) && (!ok || isDigits(frac)) {
if len(sec) > 10 {
@@ -275,13 +276,13 @@ func parseMetricsTime(key, v string) (time.Time, *HTTPError) {
if ok {
nanos, _ = strconv.ParseInt((frac + "000000000")[:9], 10, 64)
}
return time.Unix(secs, nanos).UTC(), nil
return time.UnixMilli(time.Unix(secs, nanos).UnixMilli()).UTC(), nil
}
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil || t.Before(time.Unix(0, 0)) {
return time.Time{}, errorf(http.StatusBadRequest, "invalid %s %q", key, v)
}
return t.UTC(), nil
return time.UnixMilli(t.UnixMilli()).UTC(), nil
}
func formatTime(t time.Time) string {
@@ -156,6 +156,9 @@ func TestBuildMetricsParamsLimits(t *testing.T) {
{"points at the limit", "api/v1/query_range", rng(ago(10999*time.Minute), ago(0), "1m"), http.StatusOK},
{"points above the limit", "api/v1/query_range", rng(ago(11000*time.Minute), ago(0), "1m"), http.StatusUnprocessableEntity},
{"points above the limit after rounding the step up", "api/v1/query_range", rng(ago(22*time.Second), ago(0), "0.0019"), http.StatusUnprocessableEntity},
// 10.9999s admits 11000 points, but vmselect gets both bounds cut to
// the millisecond: 11.000s, 11001 points.
{"points above the limit once the bounds are cut to milliseconds", "api/v1/query_range", rng("2026-11-04T11:59:49.0009Z", "2026-11-04T12:00:00.0008Z", "0.001"), http.StatusUnprocessableEntity},
{"overflowing step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "2562047h47m16.854775807s"), http.StatusBadRequest},
{"overflowing timeout", "api/v1/query", url.Values{"query": {"up"}, "timeout": {"2562047h47m16.854775807s"}}, http.StatusBadRequest},
{"step above the range limit", "api/v1/query_range", rng(ago(time.Hour), ago(0), "31d"), http.StatusUnprocessableEntity},