mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 20:23:39 +09:00
Resolve the overlap with the per-application roots: the namespace root keeps its platform exporter branch, the application root still replaces the whole scope with its own selectors, and the label APIs mark the repeated scope so that replacement reaches match[] as well. Assisted-by: LLM Signed-off-by: Maxim Belyy <maximbel2003@gmail.com>
575 lines
26 KiB
Go
575 lines
26 KiB
Go
/*
|
|
Copyright 2026 The Cozystack Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package observabilityproxy
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func testMetricsLimits() MetricsLimits {
|
|
return MetricsLimits{MaxRange: 30 * 24 * time.Hour, MaxPoints: 11000, MaxTimeout: 30 * time.Second}
|
|
}
|
|
|
|
// wantMetricsScope is the namespace root's scope under the default
|
|
// platform prefix: the namespace's own series, then the series a platform
|
|
// exporter attributes to it.
|
|
var wantMetricsScope = []string{`{namespace="tenant-a"}`, `{namespace=~"cozy-.*",exported_namespace="tenant-a"}`}
|
|
|
|
func testScope() []string {
|
|
return MetricsConfig{PlatformNamespacePrefix: "cozy-"}.namespaceScope("tenant-a")
|
|
}
|
|
|
|
func unix(t time.Time) string { return strconv.FormatInt(t.Unix(), 10) }
|
|
|
|
func buildMetrics(t *testing.T, endpoint string, in url.Values) metricsRequest {
|
|
t.Helper()
|
|
req, err := BuildMetricsParams(endpoint, in, testScope(), testNow, testMetricsLimits())
|
|
if err != nil {
|
|
t.Fatalf("BuildMetricsParams(%s, %v): %v", endpoint, in, err)
|
|
}
|
|
return req
|
|
}
|
|
|
|
func metricsStatus(endpoint string, in url.Values) int {
|
|
_, err := BuildMetricsParams(endpoint, in, testScope(), testNow, testMetricsLimits())
|
|
if err == nil {
|
|
return http.StatusOK
|
|
}
|
|
return err.Code
|
|
}
|
|
|
|
func TestBuildMetricsParamsAllowlist(t *testing.T) {
|
|
hostile := url.Values{
|
|
"extra_filters": {`{namespace=~".+"}`},
|
|
"extra_filters[]": {`{namespace="tenant-b"}`, `{namespace=~".*"}`},
|
|
"extra_label": {"namespace=tenant-b"},
|
|
"match": {`{namespace="tenant-b"}`},
|
|
"nocache": {"1"},
|
|
"limit": {"1"},
|
|
"AccountID": {"7"},
|
|
"latency_offset": {"1h"},
|
|
"round_digits": {"2"},
|
|
}
|
|
cases := map[string]url.Values{
|
|
"api/v1/query": {"query": {"up"}, "time": {unix(testNow)}, "step": {"30s"}},
|
|
"api/v1/query_range": {"query": {"up"}, "start": {unix(testNow.Add(-time.Hour))}, "end": {unix(testNow)}, "step": {"60"}},
|
|
"api/v1/series": {"match[]": {"up"}},
|
|
"api/v1/labels": {"match[]": {"up"}},
|
|
"api/v1/label/pod/values": {"match[]": {"up"}},
|
|
}
|
|
allowed := map[string]bool{"query": true, "start": true, "end": true, "step": true, "time": true, "match[]": true, "timeout": true, "extra_filters[]": true}
|
|
for endpoint, in := range cases {
|
|
t.Run(endpoint, func(t *testing.T) {
|
|
for k, v := range hostile {
|
|
in[k] = v
|
|
}
|
|
req := buildMetrics(t, endpoint, in)
|
|
if req.path != endpoint {
|
|
t.Errorf("path = %q", req.path)
|
|
}
|
|
for k := range req.params {
|
|
if !allowed[k] {
|
|
t.Errorf("parameter %q forwarded: %v", k, req.params)
|
|
}
|
|
}
|
|
if got := req.params["extra_filters[]"]; !slices.Equal(got, wantMetricsScope) {
|
|
t.Errorf("extra_filters[] = %q, want exactly %q", got, wantMetricsScope)
|
|
}
|
|
for _, m := range req.params["match[]"] {
|
|
if strings.Contains(m, "tenant-b") {
|
|
t.Errorf("client match without brackets forwarded as match[]: %q", m)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNamespaceSelectorEscapes(t *testing.T) {
|
|
for _, ns := range []string{"tenant-a", `a"} or {namespace=~".+`, `back\slash`, "new\nline"} {
|
|
sel := namespaceSelector(ns)
|
|
lit, ok := strings.CutPrefix(sel, "{namespace=")
|
|
lit, ok2 := strings.CutSuffix(lit, "}")
|
|
if !ok || !ok2 {
|
|
t.Fatalf("selector %q has the wrong shape", sel)
|
|
}
|
|
// MetricsQL reads a double-quoted label value with strconv.Unquote
|
|
// (metricsql extractStringValue).
|
|
got, err := strconv.Unquote(lit)
|
|
if err != nil || got != ns {
|
|
t.Errorf("selector %q decodes to %q, %v; want %q", sel, got, err, ns)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildMetricsParamsLabelEndpointsAlwaysMatch(t *testing.T) {
|
|
for _, endpoint := range []string{"api/v1/labels", "api/v1/label/__name__/values"} {
|
|
req := buildMetrics(t, endpoint, url.Values{})
|
|
if got := req.params["match[]"]; !slices.Equal(got, wantMetricsScope) {
|
|
t.Errorf("%s without match[] sends %q, want the namespace scope %q", endpoint, got, wantMetricsScope)
|
|
}
|
|
req = buildMetrics(t, endpoint, url.Values{"match[]": {"up", `{job="x"}`}})
|
|
if got := req.params["match[]"]; len(got) != 2 || got[0] != "up" || got[1] != `{job="x"}` {
|
|
t.Errorf("%s client match[] = %q, want them kept", endpoint, got)
|
|
}
|
|
}
|
|
if got := metricsStatus("api/v1/series", url.Values{}); got != http.StatusBadRequest {
|
|
t.Errorf("series without match[] = %d, want 400", got)
|
|
}
|
|
if got := metricsStatus("api/v1/series", url.Values{"match[]": {""}}); got != http.StatusBadRequest {
|
|
t.Errorf("empty match[] = %d, want 400", got)
|
|
}
|
|
many := url.Values{}
|
|
for i := range maxMetricsMatchers + 1 {
|
|
many.Add("match[]", "up"+strconv.Itoa(i))
|
|
}
|
|
if got := metricsStatus("api/v1/labels", many); got != http.StatusUnprocessableEntity {
|
|
t.Errorf("%d match[] = %d, want 422", maxMetricsMatchers+1, got)
|
|
}
|
|
}
|
|
|
|
func TestBuildMetricsParamsLimits(t *testing.T) {
|
|
ago := func(d time.Duration) string { return unix(testNow.Add(-d)) }
|
|
day := 24 * time.Hour
|
|
rng := func(start, end, step string) url.Values {
|
|
return url.Values{"query": {"up"}, "start": {start}, "end": {end}, "step": {step}}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
endpoint string
|
|
in url.Values
|
|
want int
|
|
}{
|
|
{"range at the limit", "api/v1/query_range", rng(ago(30*day), ago(0), "1h"), http.StatusOK},
|
|
{"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},
|
|
// 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},
|
|
{"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},
|
|
{"missing step", "api/v1/query_range", url.Values{"query": {"up"}, "start": {ago(time.Hour)}, "end": {ago(0)}}, http.StatusBadRequest},
|
|
{"missing start", "api/v1/query_range", url.Values{"query": {"up"}, "end": {ago(0)}, "step": {"1m"}}, http.StatusBadRequest},
|
|
{"missing query", "api/v1/query_range", url.Values{"start": {ago(time.Hour)}, "end": {ago(0)}, "step": {"1m"}}, http.StatusBadRequest},
|
|
{"inverted range", "api/v1/query_range", rng(ago(0), ago(time.Hour), "1m"), http.StatusBadRequest},
|
|
{"millisecond timestamp", "api/v1/query_range", rng(ago(time.Hour)+"000", ago(0)+"000", "1m"), http.StatusBadRequest},
|
|
{"relative time", "api/v1/query_range", rng("-1h", "now", "1m"), http.StatusBadRequest},
|
|
{"rfc3339", "api/v1/query_range", rng("2026-11-04T11:00:00Z", "2026-11-04T12:00:00.5Z", "15s"), http.StatusOK},
|
|
{"instant without time", "api/v1/query", url.Values{"query": {"up"}}, http.StatusOK},
|
|
{"instant step above the range limit", "api/v1/query", url.Values{"query": {"up"}, "step": {"31d"}}, http.StatusUnprocessableEntity},
|
|
{"instant bad time", "api/v1/query", url.Values{"query": {"up"}, "time": {"yesterday"}}, http.StatusBadRequest},
|
|
{"negative timeout", "api/v1/query", url.Values{"query": {"up"}, "timeout": {"-1s"}}, http.StatusBadRequest},
|
|
{"series range above the limit", "api/v1/series", url.Values{"match[]": {"up"}, "start": {ago(31 * day)}}, http.StatusUnprocessableEntity},
|
|
{"labels inverted range", "api/v1/labels", url.Values{"start": {ago(0)}, "end": {ago(time.Hour)}}, http.StatusBadRequest},
|
|
{"invalid label name", "api/v1/label/a-b/values", url.Values{}, http.StatusBadRequest},
|
|
{"unknown endpoint", "api/v1/status/tsdb", url.Values{}, http.StatusNotFound},
|
|
{"export", "api/v1/export", url.Values{"match[]": {"up"}}, http.StatusNotFound},
|
|
{"label without values", "api/v1/label/pod", url.Values{}, http.StatusNotFound},
|
|
{"root", "", url.Values{}, http.StatusNotFound},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := metricsStatus(tt.endpoint, tt.in); got != tt.want {
|
|
t.Fatalf("status = %d, want %d", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildMetricsParamsCanonicalForms(t *testing.T) {
|
|
req := buildMetrics(t, "api/v1/query_range", url.Values{
|
|
"query": {"up"}, "start": {"2026-11-04T11:00:00.25Z"}, "end": {unix(testNow)}, "step": {"1m30s"},
|
|
})
|
|
want := url.Values{
|
|
"query": {"up"}, "start": {"2026-11-04T11:00:00.250Z"}, "end": {"2026-11-04T12:00:00.000Z"}, "step": {"90"},
|
|
"timeout": {"30"}, "extra_filters[]": wantMetricsScope,
|
|
}
|
|
if req.params.Encode() != want.Encode() {
|
|
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 != "" {
|
|
v.Set("timeout", in)
|
|
}
|
|
if got := buildMetrics(t, "api/v1/query", v).params.Get("timeout"); got != want {
|
|
t.Errorf("timeout %q forwarded as %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
|
|
req = buildMetrics(t, "api/v1/labels", url.Values{})
|
|
if req.params.Get("end") != "2026-11-04T12:00:00.000Z" || req.params.Get("start") != "2026-11-04T11:55:00.000Z" {
|
|
t.Errorf("labels window = %s..%s, want the last 5m", req.params.Get("start"), req.params.Get("end"))
|
|
}
|
|
if q := buildMetrics(t, "api/v1/query", url.Values{"query": {"up"}}); q.params.Get("time") != "2026-11-04T12:00:00.000Z" || !q.start.Equal(testNow.Add(-15*time.Minute-2*latencyOffset)) {
|
|
t.Errorf("instant query without time: params %v, start %v", q.params, q.start)
|
|
}
|
|
}
|
|
|
|
func TestMetricsStoreByRange(t *testing.T) {
|
|
cfg := MetricsConfig{ShortTermStore: "shortterm", LongTermStore: "longterm", ShortTermRetention: 72 * time.Hour}
|
|
tests := []struct {
|
|
name string
|
|
endpoint string
|
|
in url.Values
|
|
want string
|
|
}{
|
|
{"instant now", "api/v1/query", url.Values{"query": {"up"}}, "shortterm"},
|
|
{"instant within retention", "api/v1/query", url.Values{"query": {"up"}, "time": {unix(testNow.Add(-71 * time.Hour))}}, "shortterm"},
|
|
{"instant past retention", "api/v1/query", url.Values{"query": {"up"}, "time": {unix(testNow.Add(-73 * time.Hour))}}, "longterm"},
|
|
{"live graph", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-6 * time.Hour))}, "end": {unix(testNow)}, "step": {"60"}}, "shortterm"},
|
|
{"range fits with lookback", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-71 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "shortterm"},
|
|
{"range lookback past retention", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-72 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "longterm"},
|
|
{"range past retention", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-7 * 24 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "longterm"},
|
|
{"old window ending long ago", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-5 * 24 * time.Hour))}, "end": {unix(testNow.Add(-4 * 24 * time.Hour))}, "step": {"600"}}, "longterm"},
|
|
{"labels default window", "api/v1/labels", url.Values{}, "shortterm"},
|
|
{"series past retention", "api/v1/series", url.Values{"match[]": {"up"}, "start": {unix(testNow.Add(-4 * 24 * time.Hour))}}, "longterm"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req := buildMetrics(t, tt.endpoint, tt.in)
|
|
if got := cfg.store(req.start, testNow); got != tt.want {
|
|
t.Fatalf("store = %s, want %s", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryLookback(t *testing.T) {
|
|
tests := []struct {
|
|
query string
|
|
step time.Duration
|
|
want time.Duration
|
|
}{
|
|
{`up`, 5 * time.Minute, 15 * time.Minute},
|
|
{`increase(x[7d])`, 5 * time.Minute, 7*24*time.Hour + 35*time.Minute},
|
|
{`rate(x[300])`, time.Minute, 20 * time.Minute},
|
|
{`rate(x[ 1h30m ])`, time.Minute, 105 * time.Minute},
|
|
{`max_over_time(x[1h:5m])`, time.Minute, 115 * time.Minute},
|
|
{`max_over_time(x[1h:])`, time.Minute, 83 * time.Minute},
|
|
{`max_over_time(max_over_time(rate(x[5m])[1h:1m])[1d:1h])`, time.Minute, 42*time.Hour + 35*time.Minute},
|
|
{`rate(rate(x))`, time.Hour, 2*(3*time.Hour+5*time.Minute) + 2*time.Hour + 5*time.Minute},
|
|
{`x offset 10d`, 5 * time.Minute, 10*24*time.Hour + 15*time.Minute},
|
|
{`sum(x OFFSET 2h)`, 5 * time.Minute, 155 * time.Minute},
|
|
{`rate(x[5m] offset 1h)`, time.Minute, 80 * time.Minute},
|
|
{`x offset -1h`, 5 * time.Minute, 15 * time.Minute},
|
|
{`x offset - 1h`, 5 * time.Minute, 15 * time.Minute},
|
|
{`node_offset_seconds + offset_total + with_x`, 5 * time.Minute, 15 * time.Minute},
|
|
{`x{a="[1h]"}`, 5 * time.Minute, 75 * time.Minute},
|
|
{`x{a="]"}[7d]`, 5 * time.Minute, 7*24*time.Hour + 15*time.Minute},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.query, func(t *testing.T) {
|
|
got, ok := queryLookback(tt.query, tt.step)
|
|
if !ok || got != tt.want {
|
|
t.Fatalf("queryLookback = %s, %t; want %s, true", got, ok, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryLookbackUnknown(t *testing.T) {
|
|
for _, q := range []string{
|
|
`x @ 123`,
|
|
`x @ start()`,
|
|
`x{a="a@b"}`,
|
|
`rate(x[5i])`,
|
|
`rate(x[$__interval])`,
|
|
`x[1h-5m]`,
|
|
`x[7D]`,
|
|
`x[1.d]`,
|
|
`x[]`,
|
|
`rate(x[7d)`,
|
|
`x offset`,
|
|
`x offset w`,
|
|
`WITH (w = 7d) rate(x[w])`,
|
|
`x{a="["}`,
|
|
`x{a="[5m"}[7d]`,
|
|
`x{path="/offset/1"}`,
|
|
`x[99999999999y]`,
|
|
`x offset 99999999999y`,
|
|
`x offset 3_000_000`,
|
|
`WITH (f(q) = max_over_time(q[2d:1h])) f(f(x))`,
|
|
`with (s(m) = m offset 2d) s(s(x))`,
|
|
} {
|
|
t.Run(q, func(t *testing.T) {
|
|
if got, ok := queryLookback(q, 5*time.Minute); ok {
|
|
t.Fatalf("queryLookback = %s, true; want unknown", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMetricsStoreByLookback(t *testing.T) {
|
|
cfg := MetricsConfig{ShortTermStore: "shortterm", LongTermStore: "longterm", ShortTermRetention: 72 * time.Hour}
|
|
rangeFrom := func(q string, ago time.Duration, step string) url.Values {
|
|
return url.Values{"query": {q}, "start": {unix(testNow.Add(-ago))}, "end": {unix(testNow)}, "step": {step}}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
endpoint string
|
|
in url.Values
|
|
want string
|
|
}{
|
|
{"instant short window", "api/v1/query", url.Values{"query": {"rate(x[5m])"}}, "shortterm"},
|
|
{"instant 7d window", "api/v1/query", url.Values{"query": {"increase(x[7d])"}}, "longterm"},
|
|
{"instant offset 10d", "api/v1/query", url.Values{"query": {"x offset 10d"}}, "longterm"},
|
|
{"instant negative offset", "api/v1/query", url.Values{"query": {"x offset -1h"}}, "shortterm"},
|
|
{"instant at timestamp", "api/v1/query", url.Values{"query": {"x @ 123"}}, "longterm"},
|
|
{"instant at start", "api/v1/query", url.Values{"query": {"x @ start()"}}, "longterm"},
|
|
{"instant step window", "api/v1/query", url.Values{"query": {"rate(x[5i])"}}, "longterm"},
|
|
{"instant step past retention", "api/v1/query", url.Values{"query": {"up"}, "step": {"259200"}}, "longterm"},
|
|
{"instant latency offset at the edge", "api/v1/query", url.Values{"query": {"x[71h54m59s]"}, "step": {"0.001"}}, "longterm"},
|
|
{"instant subquery past retention", "api/v1/query", url.Values{"query": {"max_over_time(rate(x[5m])[4d:1h])"}}, "longterm"},
|
|
{"instant offset with underscores", "api/v1/query", url.Values{"query": {"x offset 3_000_000"}}, "longterm"},
|
|
{"instant WITH template", "api/v1/query", url.Values{"query": {"WITH (f(q) = max_over_time(q[2d:1h])) f(f(x))"}}, "longterm"},
|
|
{"instant implicit subqueries", "api/v1/query", url.Values{"query": {"rate(rate(rate(rate(rate(rate(x))))))"}, "step": {"43200"}}, "longterm"},
|
|
{"quoted at sign", "api/v1/query", url.Values{"query": {`x{a="a@b"}`}}, "longterm"},
|
|
{"quoted open bracket hides window", "api/v1/query", url.Values{"query": {`rate(x{a="[5m"}[7d])`}}, "longterm"},
|
|
{"quoted close bracket before window", "api/v1/query", url.Values{"query": {`rate(x{a="]"}[7d])`}}, "longterm"},
|
|
{"quoted offset before offset", "api/v1/query", url.Values{"query": {`x{a="offset 1m"} offset 10d`}}, "longterm"},
|
|
{"range short window", "api/v1/query_range", rangeFrom("rate(x[1h])", 6*time.Hour, "60"), "shortterm"},
|
|
{"range 7d window", "api/v1/query_range", rangeFrom("increase(x[7d])", 6*time.Hour, "60"), "longterm"},
|
|
{"range offset past retention", "api/v1/query_range", rangeFrom("x offset 3d", time.Hour, "60"), "longterm"},
|
|
{"range lookback fits", "api/v1/query_range", rangeFrom("rate(x[5m])", 70*time.Hour, "600"), "shortterm"},
|
|
{"range lookback crosses retention", "api/v1/query_range", rangeFrom("rate(x[5m])", 71*time.Hour+50*time.Minute, "600"), "longterm"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req := buildMetrics(t, tt.endpoint, tt.in)
|
|
if got := cfg.store(req.start, testNow); got != tt.want {
|
|
t.Fatalf("store = %s, want %s", got, tt.want)
|
|
}
|
|
if q := req.params.Get("query"); q != tt.in.Get("query") {
|
|
t.Fatalf("query forwarded as %q, want it unchanged", q)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// vmselectMillis repeats how vmselect reads a duration argument
|
|
// (httputil.GetDuration in lib/httputil/duration.go, v1.136.22 and
|
|
// v1.139.0): float seconds, multiplied by 1e3 and truncated.
|
|
func vmselectMillis(t *testing.T, v string) int64 {
|
|
t.Helper()
|
|
secs, err := strconv.ParseFloat(v, 64)
|
|
if err != nil {
|
|
t.Fatalf("vmselect cannot parse %q: %v", v, err)
|
|
}
|
|
return int64(secs * 1e3)
|
|
}
|
|
|
|
// TestFormatSecondsVmselectExact sweeps every step up to 2000s and the
|
|
// large end of the range: vmselect must read back the milliseconds the
|
|
// limits were checked with.
|
|
func TestFormatSecondsVmselectExact(t *testing.T) {
|
|
check := func(ms int64) {
|
|
if got := vmselectMillis(t, formatSeconds(time.Duration(ms)*time.Millisecond)); got != ms {
|
|
t.Fatalf("%dms formatted as %q reaches vmselect as %dms", ms, formatSeconds(time.Duration(ms)*time.Millisecond), got)
|
|
}
|
|
}
|
|
for ms := int64(1); ms <= 2_000_000; ms++ {
|
|
check(ms)
|
|
}
|
|
for ms := maxMetricsDuration.Milliseconds() - 100_000; ms <= maxMetricsDuration.Milliseconds(); ms++ {
|
|
check(ms)
|
|
}
|
|
// A spread of the range in between, on a stride no power of ten divides.
|
|
for ms := int64(2_000_001); ms < maxMetricsDuration.Milliseconds(); ms += 3_153_599_999 {
|
|
check(ms)
|
|
}
|
|
// Whole seconds keep their plain form.
|
|
if got := formatSeconds(90 * time.Second); got != "90" {
|
|
t.Errorf("90s formatted as %q", got)
|
|
}
|
|
}
|
|
|
|
// TestBuildMetricsParamsPointsAsVmselectCounts: a 1.001s step is 1001ms
|
|
// to the proxy; vmselect must count the same 11000 points at the limit.
|
|
func TestBuildMetricsParamsPointsAsVmselectCounts(t *testing.T) {
|
|
end := testNow
|
|
step := 1001 * time.Millisecond
|
|
rng := func(points int64) url.Values {
|
|
start := end.Add(-time.Duration(points-1) * step)
|
|
return url.Values{"query": {"up"}, "start": {start.Format(time.RFC3339Nano)}, "end": {end.Format(time.RFC3339Nano)}, "step": {"1.001"}}
|
|
}
|
|
req := buildMetrics(t, "api/v1/query_range", rng(11000))
|
|
stepMs := vmselectMillis(t, req.params.Get("step"))
|
|
startMs, endMs := vmselectTimeMillis(t, req.params.Get("start")), vmselectTimeMillis(t, req.params.Get("end"))
|
|
if points := (endMs-startMs)/stepMs + 1; stepMs != 1001 || points != 11000 {
|
|
t.Fatalf("vmselect reads step %dms and %d points, want 1001ms and 11000", stepMs, points)
|
|
}
|
|
if got := metricsStatus("api/v1/query_range", rng(11001)); got != http.StatusUnprocessableEntity {
|
|
t.Fatalf("11001 points answered %d, want 422", got)
|
|
}
|
|
}
|
|
|
|
func TestQueryLookahead(t *testing.T) {
|
|
tests := []struct {
|
|
query string
|
|
want time.Duration
|
|
}{
|
|
{`up`, 0},
|
|
{`x offset 1h`, 0},
|
|
{`x offset -1h`, time.Hour},
|
|
{`x OFFSET - 90m`, 90 * time.Minute},
|
|
// Each "(" may be an implicit subquery, which vmselect evaluates
|
|
// up to two steps past its end; here the step is 1m.
|
|
{`rate(x[5m] offset -30m) offset -1h`, 92 * time.Minute},
|
|
{`x offset -1h + y offset 2h`, time.Hour},
|
|
{`node_offset_seconds - 1`, 0},
|
|
// An escape inside a string literal is only part of a label value.
|
|
{`x{a=~"a\\.b"}`, 0},
|
|
{`x{a="\"rollup\\_candlestick\""}`, 0},
|
|
// metricsql lexes a duration as one token, so a sign with spaces,
|
|
// or a plus, is an operator after the offset, not part of it.
|
|
{`x offset 1h+30m`, 0},
|
|
{`x offset 1h - 30m`, 0},
|
|
{`x offset -1h + 30m`, time.Hour},
|
|
{`x offset -1h30m`, 90 * time.Minute},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.query, func(t *testing.T) {
|
|
got, ok := queryLookahead(tt.query, time.Minute)
|
|
if !ok || got != tt.want {
|
|
t.Fatalf("queryLookahead = %s, %v; want %s", got, ok, tt.want)
|
|
}
|
|
})
|
|
}
|
|
for _, q := range []string{
|
|
`x @ 1700000000`,
|
|
// The lookback scan cannot bound any @, so the application root
|
|
// refuses every one; the lookahead does not tell them apart.
|
|
`x @ end()`,
|
|
`x @ start() offset -1h`,
|
|
`x @ (end() - 1h)`,
|
|
`x{a="a@b"}`,
|
|
`x offset -5i`,
|
|
// Compound durations with a negative part (metricsql DurationValue):
|
|
// -1h-30m is -90m and 1h-2h is -1h, neither of which the scan reads.
|
|
`x offset -1h-30m`,
|
|
`x offset - 1h-30m`,
|
|
`x offset 1h-2h`,
|
|
`x offset 1h-30m`,
|
|
// metricsql continues a duration with -1.h or -.5h too, so any minus
|
|
// right after the duration is refused rather than told apart.
|
|
`x offset 5m-1.h`,
|
|
`x offset 5m-.5h`,
|
|
`x offset 1h-1`,
|
|
`x offset 5m-y`,
|
|
// metricsql also reads a trailing $__interval as part of the
|
|
// duration: -1h$__interval is -(1h + step).
|
|
`x offset -1h$__interval`,
|
|
`rate(x[5m]) offset -1h$__interval`,
|
|
// A subquery step the scan cannot read, an unclosed bracket, and a
|
|
// sum past the duration range.
|
|
`rate(x[1h:5i])`,
|
|
`rate(x[1h:$__interval])`,
|
|
`rate(x[1h`,
|
|
`max_over_time(x[1h:100y])`,
|
|
`rate(x[5m] offset 1h-30m)`,
|
|
// metricsql unescapes identifiers (unescapeIdent), so an escaped
|
|
// rollup_candlestick, or any escape outside a string, could hide
|
|
// what the scan looks for.
|
|
`rollup\_candlestick(x + y)`,
|
|
`rollup_candle\stick(x)`,
|
|
`rollup\x5fcandlestick(x)`,
|
|
`x offs\et -1h`,
|
|
// A comment runs to the end of the line, so quotes in it would
|
|
// confuse the string tracking and brackets in it the scans.
|
|
"x # '\n+ rollup\\_candlestick(y) # '",
|
|
"x # \"\n+ rollup\\_candlestick(y) # \"",
|
|
"rate(x[5m]) # comment",
|
|
// metricsql lets a backslash escape the closing quote in backquotes
|
|
// too (scanString), and quoted label names keep such escapes, so
|
|
// a backquoted string must not end at an escaped backquote.
|
|
"container_cpu_usage_seconds_total{`\\``=\"\"} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
|
|
"{`\\`a`} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
|
|
// vmselect evaluates rollup_candlestick one step ahead
|
|
// (evalRollupFuncWithoutAt), whatever the offset.
|
|
`rollup_candlestick(x)`,
|
|
`ROLLUP_CANDLESTICK(x[5m])`,
|
|
`sum(rollup_candlestick(x[1h:5m]))`,
|
|
`x offset -$__interval`,
|
|
`with (f(a) = a offset -1h) f(f(x))`,
|
|
} {
|
|
if got, ok := queryLookahead(q, time.Minute); ok {
|
|
t.Errorf("queryLookahead(%q) = %s, want unknown", q, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestQueryLookaheadSubquery: vmselect evaluates a subquery up to its end
|
|
// plus one subquery step, rounded up to the step (evalRollupFuncWithSubquery),
|
|
// and range_last and the other range_* transforms copy the last of those
|
|
// values back onto earlier points, so a series that starts after the
|
|
// evaluation time can reach the answer.
|
|
func TestQueryLookaheadSubquery(t *testing.T) {
|
|
// One explicit subquery and one "(": two levels of two steps, at the
|
|
// subquery's step, or at the query's step when the subquery has none.
|
|
for q, want := range map[string]time.Duration{
|
|
`max_over_time(x[1h:1h])`: 4 * time.Hour,
|
|
`max_over_time(x[1h:])`: 4 * time.Minute,
|
|
} {
|
|
if got, ok := queryLookahead(q, time.Minute); !ok || got != want {
|
|
t.Errorf("queryLookahead(%q) = %s, %v; want %s", q, got, ok, want)
|
|
}
|
|
}
|
|
for _, q := range []string{`max_over_time(range_last(x)[1h:1h])`, `max_over_time(keep_next_value(x)[1h:1h])`} {
|
|
got, ok := queryLookahead(q, time.Minute)
|
|
if !ok || got < 2*time.Hour {
|
|
t.Errorf("queryLookahead(%q) = %s, %v; want at least two subquery steps", q, got, ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBuildMetricsParamsInstantStartCoversLatencyOffset: vmselect evaluates
|
|
// an instant query whose time is within 30s of its clock at that clock
|
|
// minus 30s, anywhere in (time-60s, time], so the earliest read the store
|
|
// choice and the lookup rely on starts from time-60s.
|
|
func TestBuildMetricsParamsInstantStartCoversLatencyOffset(t *testing.T) {
|
|
at := testNow.Add(29 * time.Second)
|
|
req := buildMetrics(t, "api/v1/query", url.Values{"query": {"x"}, "time": {at.Format(time.RFC3339)}, "step": {"1s"}})
|
|
lookback, ok := queryLookback("x", time.Second)
|
|
if !ok {
|
|
t.Fatal("lookback of x unknown")
|
|
}
|
|
if want := at.Add(-2*latencyOffset - lookback); req.start.After(want) {
|
|
t.Fatalf("start = %s, want at most %s", req.start, want)
|
|
}
|
|
}
|