mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
fix(observability-proxy): refuse metrics durations vmselect would refuse
Rounding a duration up to the millisecond overflows for values within a millisecond of the largest one, which turned such a step or timeout negative and let it past the range, points and timeout limits. vmselect refuses durations above 100 years itself, so the proxy now answers 400 for them before rounding. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
81cd278f63
commit
6d6f05a1c7
2 files changed
+8
-1
No files matched your search
@@ -241,13 +241,18 @@ func parseStep(v string, lim MetricsLimits) (time.Duration, *HTTPError) {
|
||||
return step, nil
|
||||
}
|
||||
|
||||
// maxMetricsDuration is vmselect's own bound on step and timeout
|
||||
// (maxDurationMsecs in lib/httputil); it also keeps the rounding below
|
||||
// from overflowing.
|
||||
const maxMetricsDuration = 100 * 365 * 24 * time.Hour
|
||||
|
||||
// parseMetricsDuration takes Prometheus float seconds or a duration such as
|
||||
// 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 {
|
||||
if err != nil || d < time.Millisecond || d > maxMetricsDuration {
|
||||
return 0, errorf(http.StatusBadRequest, "invalid %s %q", key, v)
|
||||
}
|
||||
return (d + time.Millisecond - 1).Truncate(time.Millisecond), nil
|
||||
|
||||
@@ -156,6 +156,8 @@ 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},
|
||||
{"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},
|
||||
{"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},
|
||||
|
||||
Reference in new issue
Block a user