fix(observability-proxy): round the metrics step up to whole milliseconds

vmselect reads step and timeout in whole milliseconds and drops the
remainder, so a step of 1.9ms passed the points check as 1.9ms and
then ran as 1ms, close to twice the points the limit allows. The
duration is now rounded up before the limits are checked, so they see
the value vmselect will use.

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 5630de4ab1
commit 1ac7fd29bf
2 files changed
+14 -2

No files matched your search

+4 -2
View File
@@ -242,13 +242,15 @@ func parseStep(v string, lim MetricsLimits) (time.Duration, *HTTPError) {
}
// parseMetricsDuration takes Prometheus float seconds or a duration such as
// 1m30s. vmselect works in milliseconds and refuses anything shorter.
// 1m30s. vmselect works in whole milliseconds and refuses anything shorter;
// the value is rounded up to the millisecond so the limits are checked
// against what vmselect will use, not against a fraction it drops.
func parseMetricsDuration(key, v string) (time.Duration, *HTTPError) {
d, err := parseDuration(v)
if err != nil || d < time.Millisecond {
return 0, errorf(http.StatusBadRequest, "invalid %s %q", key, v)
}
return d, nil
return (d + time.Millisecond - 1).Truncate(time.Millisecond), nil
}
// parseMetricsTime takes the two forms the Prometheus API defines: float
@@ -155,6 +155,7 @@ func TestBuildMetricsParamsLimits(t *testing.T) {
{"range above the limit", "api/v1/query_range", rng(ago(31*day), ago(0), "1h"), http.StatusUnprocessableEntity},
{"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},
{"step above the range limit", "api/v1/query_range", rng(ago(time.Hour), ago(0), "31d"), http.StatusUnprocessableEntity},
{"zero step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "0"), http.StatusBadRequest},
{"sub-millisecond step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "0.0001"), http.StatusBadRequest},
@@ -198,6 +199,15 @@ func TestBuildMetricsParamsCanonicalForms(t *testing.T) {
t.Errorf("params = %v\nwant %v", req.params, want)
}
// vmselect drops a sub-millisecond remainder; the points check has to
// see the step it will use.
for in, want := range map[string]string{"60.0001": "60.001", "0.0019": "0.002"} {
req = buildMetrics(t, "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-20 * time.Second))}, "end": {unix(testNow)}, "step": {in}})
if got := req.params.Get("step"); got != want {
t.Errorf("step %s forwarded as %q, want %s", in, got, want)
}
}
for in, want := range map[string]string{"": "30", "10s": "10", "5m": "30", "1.5": "1.5"} {
v := url.Values{"query": {"up"}}
if in != "" {