2020-04-27 21:19:27 +00:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-30 06:46:03 +00:00
|
|
|
"fmt"
|
2020-04-27 21:19:27 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-02 12:45:08 +00:00
|
|
|
"net/url"
|
2021-04-30 06:46:03 +00:00
|
|
|
"reflect"
|
2022-12-05 07:34:54 +00:00
|
|
|
"sort"
|
2021-06-09 09:20:38 +00:00
|
|
|
"strings"
|
2020-04-27 21:19:27 +00:00
|
|
|
"testing"
|
2020-09-21 12:53:49 +00:00
|
|
|
"time"
|
2021-09-14 11:32:06 +00:00
|
|
|
|
2022-03-10 11:09:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2021-09-14 11:32:06 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-04-27 21:19:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
basicAuthName = "foo"
|
|
|
|
basicAuthPass = "bar"
|
2021-09-14 11:32:06 +00:00
|
|
|
baCfg = &promauth.BasicAuthConfig{
|
|
|
|
Username: basicAuthName,
|
2021-11-05 12:41:14 +00:00
|
|
|
Password: promauth.NewSecret(basicAuthPass),
|
2021-09-14 11:32:06 +00:00
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
vmQuery = "vm_rows"
|
|
|
|
queryRender = "constantLine(10)"
|
|
|
|
vlogsQuery = "_time: 5m | stats by (foo) count() total"
|
|
|
|
vlogsRangeQuery = "* | stats by (foo) count() total"
|
2020-04-27 21:19:27 +00:00
|
|
|
)
|
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
func TestVMInstantQuery(t *testing.T) {
|
2020-04-27 21:19:27 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("should not be called")
|
2020-04-27 21:19:27 +00:00
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected POST method got %s", r.Method)
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
if r.URL.Query().Get("query") != vmQuery {
|
|
|
|
t.Fatalf("expected %s in query param, got %s", vmQuery, r.URL.Query().Get("query"))
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2020-09-21 12:53:49 +00:00
|
|
|
timeParam := r.URL.Query().Get("time")
|
|
|
|
if timeParam == "" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 'time' in query param, got nil instead")
|
2020-09-21 12:53:49 +00:00
|
|
|
}
|
2023-07-07 08:39:25 +00:00
|
|
|
if _, err := time.Parse(time.RFC3339, timeParam); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("failed to parse 'time' query param %q: %s", timeParam, err)
|
2020-09-21 12:53:49 +00:00
|
|
|
}
|
2020-04-27 21:19:27 +00:00
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.WriteHeader(500)
|
2023-04-19 08:18:32 +00:00
|
|
|
case 1:
|
2020-04-27 21:19:27 +00:00
|
|
|
w.Write([]byte("[]"))
|
2023-04-19 08:18:32 +00:00
|
|
|
case 2:
|
2020-04-27 21:19:27 +00:00
|
|
|
w.Write([]byte(`{"status":"error", "errorType":"type:", "error":"some error msg"}`))
|
2023-04-19 08:18:32 +00:00
|
|
|
case 3:
|
2020-04-27 21:19:27 +00:00
|
|
|
w.Write([]byte(`{"status":"unknown"}`))
|
2023-04-19 08:18:32 +00:00
|
|
|
case 4:
|
2020-04-27 21:19:27 +00:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix"}}`))
|
2023-04-19 08:18:32 +00:00
|
|
|
case 5:
|
2022-12-05 07:34:54 +00:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"vm_rows","foo":"bar"},"value":[1583786142,"13763"]},{"metric":{"__name__":"vm_requests","foo":"baz"},"value":[1583786140,"2000"]}]}}`))
|
2023-04-19 08:18:32 +00:00
|
|
|
case 6:
|
2022-05-18 07:50:46 +00:00
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]}}`))
|
2023-05-08 07:36:39 +00:00
|
|
|
case 7:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]},"stats":{"seriesFetched": "42"}}`))
|
|
|
|
}
|
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
mux.HandleFunc("/render", func(w http.ResponseWriter, _ *http.Request) {
|
2023-05-08 07:36:39 +00:00
|
|
|
c++
|
|
|
|
switch c {
|
|
|
|
case 8:
|
|
|
|
w.Write([]byte(`[{"target":"constantLine(10)","tags":{"name":"constantLine(10)"},"datapoints":[[10,1611758343],[10,1611758373],[10,1611758403]]}]`))
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
|
|
|
})
|
2024-10-29 15:30:39 +00:00
|
|
|
mux.HandleFunc("/select/logsql/stats_query", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
t.Fatalf("expected POST method got %s", r.Method)
|
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
|
|
|
t.Fatalf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
|
|
|
}
|
|
|
|
if r.URL.Query().Get("query") != vlogsQuery {
|
|
|
|
t.Fatalf("expected %s in query param, got %s", vlogsQuery, r.URL.Query().Get("query"))
|
|
|
|
}
|
|
|
|
timeParam := r.URL.Query().Get("time")
|
|
|
|
if timeParam == "" {
|
|
|
|
t.Fatalf("expected 'time' in query param, got nil instead")
|
|
|
|
}
|
|
|
|
if _, err := time.Parse(time.RFC3339, timeParam); err != nil {
|
|
|
|
t.Fatalf("failed to parse 'time' query param %q: %s", timeParam, err)
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 9:
|
|
|
|
w.Write([]byte("[]"))
|
|
|
|
case 10:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"total","foo":"bar"},"value":[1583786142,"13763"]},{"metric":{"__name__":"total","foo":"baz"},"value":[1583786140,"2000"]}]}}`))
|
|
|
|
}
|
|
|
|
})
|
2020-04-27 21:19:27 +00:00
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2022-06-22 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
s := NewPrometheusClient(srv.URL, authCfg, false, srv.Client())
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2022-07-22 08:44:55 +00:00
|
|
|
p := datasourcePrometheus
|
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(p), EvaluationInterval: 15 * time.Second})
|
2022-03-29 13:09:07 +00:00
|
|
|
ts := time.Now()
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
expErr := func(query, err string) {
|
2023-04-19 08:18:32 +00:00
|
|
|
_, _, gotErr := pq.Query(ctx, query, ts)
|
|
|
|
if gotErr == nil {
|
2022-05-18 07:50:46 +00:00
|
|
|
t.Fatalf("expected %q got nil", err)
|
|
|
|
}
|
2023-04-19 08:18:32 +00:00
|
|
|
if !strings.Contains(gotErr.Error(), err) {
|
|
|
|
t.Fatalf("expected err %q; got %q", err, gotErr)
|
|
|
|
}
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2022-05-18 07:50:46 +00:00
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
expErr(vmQuery, "500") // 0
|
|
|
|
expErr(vmQuery, "error parsing response") // 1
|
|
|
|
expErr(vmQuery, "response error") // 2
|
|
|
|
expErr(vmQuery, "unknown status") // 3
|
|
|
|
expErr(vmQuery, "unexpected end of JSON input") // 4
|
2022-05-18 07:50:46 +00:00
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
res, _, err := pq.Query(ctx, vmQuery, ts) // 5 - vector
|
2020-04-27 21:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
if len(res.Data) != 2 {
|
|
|
|
t.Fatalf("expected 2 metrics got %d in %+v", len(res.Data), res.Data)
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2021-06-11 08:22:05 +00:00
|
|
|
expected := []Metric{
|
|
|
|
{
|
2022-12-05 07:34:54 +00:00
|
|
|
Labels: []Label{{Value: "vm_rows", Name: "__name__"}, {Value: "bar", Name: "foo"}},
|
2021-06-11 08:22:05 +00:00
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
},
|
|
|
|
{
|
2022-12-05 07:34:54 +00:00
|
|
|
Labels: []Label{{Value: "vm_requests", Name: "__name__"}, {Value: "baz", Name: "foo"}},
|
2021-06-11 08:22:05 +00:00
|
|
|
Timestamps: []int64{1583786140},
|
|
|
|
Values: []float64{2000},
|
|
|
|
},
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
metricsEqual(t, res.Data, expected)
|
2021-04-28 20:41:15 +00:00
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
res, req, err := pq.Query(ctx, vmQuery, ts) // 6 - scalar
|
2022-05-18 07:50:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2022-09-15 10:40:22 +00:00
|
|
|
if req == nil {
|
|
|
|
t.Fatalf("expected request to be non-nil")
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(res.Data), res.Data)
|
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res.Data, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", res.Data, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.SeriesFetched != nil {
|
|
|
|
t.Fatalf("expected `seriesFetched` field to be nil when it is missing in datasource response; got %v instead",
|
|
|
|
res.SeriesFetched)
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
res, _, err = pq.Query(ctx, vmQuery, ts) // 7 - scalar with stats
|
2023-05-08 07:36:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(res.Data), res)
|
2022-05-18 07:50:46 +00:00
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{1},
|
|
|
|
},
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
if !reflect.DeepEqual(res.Data, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", res.Data, expected)
|
|
|
|
}
|
|
|
|
if *res.SeriesFetched != 42 {
|
|
|
|
t.Fatalf("expected `seriesFetched` field to be 42; got %d instead",
|
|
|
|
*res.SeriesFetched)
|
2022-05-18 07:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
// test graphite
|
2022-07-22 08:44:55 +00:00
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceGraphite)})
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2023-05-08 07:36:39 +00:00
|
|
|
res, _, err = gq.Query(ctx, queryRender, ts) // 8 - graphite
|
2021-02-01 13:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
if len(res.Data) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(res.Data), res.Data)
|
2021-02-01 13:02:44 +00:00
|
|
|
}
|
2022-12-05 07:34:54 +00:00
|
|
|
exp := []Metric{
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "constantLine(10)", Name: "name"}},
|
|
|
|
Timestamps: []int64{1611758403},
|
|
|
|
Values: []float64{10},
|
|
|
|
},
|
2021-02-01 13:02:44 +00:00
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
metricsEqual(t, res.Data, exp)
|
2024-10-29 15:30:39 +00:00
|
|
|
|
|
|
|
// test victorialogs
|
|
|
|
vlogs := datasourceVLogs
|
|
|
|
pq = s.BuildWithParams(QuerierParams{DataSourceType: string(vlogs), EvaluationInterval: 15 * time.Second})
|
|
|
|
|
|
|
|
expErr(vlogsQuery, "error parsing response") // 9
|
|
|
|
|
|
|
|
res, _, err = pq.Query(ctx, vlogsQuery, ts) // 10
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
if len(res.Data) != 2 {
|
|
|
|
t.Fatalf("expected 2 metrics got %d in %+v", len(res.Data), res.Data)
|
|
|
|
}
|
|
|
|
expected = []Metric{
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "total", Name: "stats_result"}, {Value: "bar", Name: "foo"}},
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Labels: []Label{{Value: "total", Name: "stats_result"}, {Value: "baz", Name: "foo"}},
|
|
|
|
Timestamps: []int64{1583786140},
|
|
|
|
Values: []float64{2000},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
metricsEqual(t, res.Data, expected)
|
2023-04-19 08:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMInstantQueryWithRetry(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("should not be called")
|
2023-04-19 08:18:32 +00:00
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
2024-10-29 15:30:39 +00:00
|
|
|
if r.URL.Query().Get("query") != vmQuery {
|
|
|
|
t.Fatalf("expected %s in query param, got %s", vmQuery, r.URL.Query().Get("query"))
|
2023-04-19 08:18:32 +00:00
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "1"]}}`))
|
|
|
|
case 1:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 2:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"scalar","result":[1583786142, "2"]}}`))
|
|
|
|
case 3:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
case 4:
|
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
s := NewPrometheusClient(srv.URL, nil, false, srv.Client())
|
2023-04-19 08:18:32 +00:00
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourcePrometheus)})
|
|
|
|
|
|
|
|
expErr := func(err string) {
|
2024-10-29 15:30:39 +00:00
|
|
|
_, _, gotErr := pq.Query(ctx, vmQuery, time.Now())
|
2023-04-19 08:18:32 +00:00
|
|
|
if gotErr == nil {
|
|
|
|
t.Fatalf("expected %q got nil", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(gotErr.Error(), err) {
|
|
|
|
t.Fatalf("expected err %q; got %q", err, gotErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expValue := func(v float64) {
|
2024-10-29 15:30:39 +00:00
|
|
|
res, _, err := pq.Query(ctx, vmQuery, time.Now())
|
2023-04-19 08:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
m := res.Data
|
2023-04-19 08:18:32 +00:00
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metrics got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected := []Metric{
|
|
|
|
{
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{v},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m, expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expValue(1) // 0
|
|
|
|
expValue(2) // 1 - fail, 2 - retry
|
|
|
|
expErr("EOF") // 3, 4 - retries
|
2022-12-05 07:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func metricsEqual(t *testing.T, gotM, expectedM []Metric) {
|
|
|
|
for i, exp := range expectedM {
|
|
|
|
got := gotM[i]
|
|
|
|
gotTS, expTS := got.Timestamps, exp.Timestamps
|
|
|
|
if !reflect.DeepEqual(gotTS, expTS) {
|
|
|
|
t.Fatalf("unexpected timestamps %+v want %+v", gotTS, expTS)
|
|
|
|
}
|
|
|
|
gotV, expV := got.Values, exp.Values
|
|
|
|
if !reflect.DeepEqual(gotV, expV) {
|
|
|
|
t.Fatalf("unexpected values %+v want %+v", gotV, expV)
|
|
|
|
}
|
|
|
|
sort.Slice(got.Labels, func(i, j int) bool {
|
|
|
|
return got.Labels[i].Name < got.Labels[j].Name
|
|
|
|
})
|
|
|
|
sort.Slice(exp.Labels, func(i, j int) bool {
|
|
|
|
return exp.Labels[i].Name < exp.Labels[j].Name
|
|
|
|
})
|
|
|
|
if !reflect.DeepEqual(exp.Labels, got.Labels) {
|
|
|
|
t.Fatalf("unexpected labels %+v want %+v", got.Labels, exp.Labels)
|
|
|
|
}
|
2021-02-01 13:02:44 +00:00
|
|
|
}
|
2020-04-27 21:19:27 +00:00
|
|
|
}
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
func TestVMRangeQuery(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("should not be called")
|
2021-06-09 09:20:38 +00:00
|
|
|
})
|
|
|
|
c := -1
|
|
|
|
mux.HandleFunc("/api/v1/query_range", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected POST method got %s", r.Method)
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
if r.URL.Query().Get("query") != vmQuery {
|
|
|
|
t.Fatalf("expected %s in query param, got %s", vmQuery, r.URL.Query().Get("query"))
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
startTS := r.URL.Query().Get("start")
|
|
|
|
if startTS == "" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 'start' in query param, got nil instead")
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
2023-07-07 08:39:25 +00:00
|
|
|
if _, err := time.Parse(time.RFC3339, startTS); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("failed to parse 'start' query param: %s", err)
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
endTS := r.URL.Query().Get("end")
|
|
|
|
if endTS == "" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 'end' in query param, got nil instead")
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
2023-07-07 08:39:25 +00:00
|
|
|
if _, err := time.Parse(time.RFC3339, endTS); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("failed to parse 'end' query param: %s", err)
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
2022-12-01 12:57:53 +00:00
|
|
|
step := r.URL.Query().Get("step")
|
|
|
|
if step != "15s" {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected 'step' query param to be 15s; got %q instead", step)
|
2022-12-01 12:57:53 +00:00
|
|
|
}
|
2021-06-09 09:20:38 +00:00
|
|
|
switch c {
|
|
|
|
case 0:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"vm_rows"},"values":[[1583786142,"13763"]]}]}}`))
|
|
|
|
}
|
|
|
|
})
|
2024-10-29 15:30:39 +00:00
|
|
|
mux.HandleFunc("/select/logsql/stats_query_range", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c++
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
t.Fatalf("expected POST method got %s", r.Method)
|
|
|
|
}
|
|
|
|
if name, pass, _ := r.BasicAuth(); name != basicAuthName || pass != basicAuthPass {
|
|
|
|
t.Fatalf("expected %s:%s as basic auth got %s:%s", basicAuthName, basicAuthPass, name, pass)
|
|
|
|
}
|
|
|
|
if r.URL.Query().Get("query") != vlogsRangeQuery {
|
|
|
|
t.Fatalf("expected %s in query param, got %s", vmQuery, r.URL.Query().Get("query"))
|
|
|
|
}
|
|
|
|
startTS := r.URL.Query().Get("start")
|
|
|
|
if startTS == "" {
|
|
|
|
t.Fatalf("expected 'start' in query param, got nil instead")
|
|
|
|
}
|
|
|
|
if _, err := time.Parse(time.RFC3339, startTS); err != nil {
|
|
|
|
t.Fatalf("failed to parse 'start' query param: %s", err)
|
|
|
|
}
|
|
|
|
endTS := r.URL.Query().Get("end")
|
|
|
|
if endTS == "" {
|
|
|
|
t.Fatalf("expected 'end' in query param, got nil instead")
|
|
|
|
}
|
|
|
|
if _, err := time.Parse(time.RFC3339, endTS); err != nil {
|
|
|
|
t.Fatalf("failed to parse 'end' query param: %s", err)
|
|
|
|
}
|
|
|
|
step := r.URL.Query().Get("step")
|
|
|
|
if step != "60s" {
|
|
|
|
t.Fatalf("expected 'step' query param to be 60s; got %q instead", step)
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 1:
|
|
|
|
w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"total"},"values":[[1583786142,"10"]]}]}}`))
|
|
|
|
}
|
|
|
|
})
|
2021-06-09 09:20:38 +00:00
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
|
|
|
|
2022-06-22 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected: %s", err)
|
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
s := NewPrometheusClient(srv.URL, authCfg, false, srv.Client())
|
2021-06-09 09:20:38 +00:00
|
|
|
|
2022-07-22 08:44:55 +00:00
|
|
|
pq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourcePrometheus), EvaluationInterval: 15 * time.Second})
|
2021-06-09 09:20:38 +00:00
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
_, err = pq.QueryRange(ctx, vmQuery, time.Now(), time.Time{})
|
2021-06-09 09:20:38 +00:00
|
|
|
expectError(t, err, "is missing")
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
_, err = pq.QueryRange(ctx, vmQuery, time.Time{}, time.Now())
|
2021-06-09 09:20:38 +00:00
|
|
|
expectError(t, err, "is missing")
|
|
|
|
|
|
|
|
start, end := time.Now().Add(-time.Minute), time.Now()
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
res, err := pq.QueryRange(ctx, vmQuery, start, end)
|
2021-06-09 09:20:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
2023-05-08 07:36:39 +00:00
|
|
|
m := res.Data
|
2021-06-09 09:20:38 +00:00
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected := Metric{
|
|
|
|
Labels: []Label{{Value: "vm_rows", Name: "__name__"}},
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{13763},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m[0], expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m[0], expected)
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
// test unsupported graphite
|
2022-07-22 08:44:55 +00:00
|
|
|
gq := s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceGraphite)})
|
2021-06-09 09:20:38 +00:00
|
|
|
|
|
|
|
_, err = gq.QueryRange(ctx, queryRender, start, end)
|
|
|
|
expectError(t, err, "is not supported")
|
2024-10-29 15:30:39 +00:00
|
|
|
|
|
|
|
// unsupported logsql
|
|
|
|
gq = s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceVLogs), EvaluationInterval: 60 * time.Second})
|
|
|
|
|
|
|
|
res, err = gq.QueryRange(ctx, vlogsRangeQuery, start, end)
|
|
|
|
expectError(t, err, "is not supported")
|
|
|
|
|
|
|
|
// supported logsql
|
|
|
|
gq = s.BuildWithParams(QuerierParams{DataSourceType: string(datasourceVLogs), EvaluationInterval: 60 * time.Second, ApplyIntervalAsTimeFilter: true})
|
|
|
|
res, err = gq.QueryRange(ctx, vlogsRangeQuery, start, end)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected %s", err)
|
|
|
|
}
|
|
|
|
m = res.Data
|
|
|
|
if len(m) != 1 {
|
|
|
|
t.Fatalf("expected 1 metric got %d in %+v", len(m), m)
|
|
|
|
}
|
|
|
|
expected = Metric{
|
|
|
|
Labels: []Label{{Value: "total", Name: "stats_result"}},
|
|
|
|
Timestamps: []int64{1583786142},
|
|
|
|
Values: []float64{10},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m[0], expected) {
|
|
|
|
t.Fatalf("unexpected metric %+v want %+v", m[0], expected)
|
|
|
|
}
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestParams(t *testing.T) {
|
2024-07-12 19:57:56 +00:00
|
|
|
query := "up"
|
2024-10-29 15:30:39 +00:00
|
|
|
vlogsQuery := "_time: 5m | stats count() total"
|
2024-07-12 19:57:56 +00:00
|
|
|
timestamp := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC)
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
f := func(isQueryRange bool, c *Client, checkFn func(t *testing.T, r *http.Request)) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
req, err := c.newRequest(ctx)
|
2024-07-12 19:57:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in newRequest: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:30:39 +00:00
|
|
|
switch c.dataSourceType {
|
|
|
|
case datasourcePrometheus:
|
2024-07-12 19:57:56 +00:00
|
|
|
if isQueryRange {
|
2024-10-29 15:30:39 +00:00
|
|
|
c.setPrometheusRangeReqParams(req, query, timestamp, timestamp)
|
2024-07-12 19:57:56 +00:00
|
|
|
} else {
|
2024-10-29 15:30:39 +00:00
|
|
|
c.setPrometheusInstantReqParams(req, query, timestamp)
|
2024-07-12 19:57:56 +00:00
|
|
|
}
|
|
|
|
case datasourceGraphite:
|
2024-10-29 15:30:39 +00:00
|
|
|
c.setGraphiteReqParams(req, query)
|
|
|
|
case datasourceVLogs:
|
|
|
|
if isQueryRange {
|
|
|
|
c.setVLogsRangeReqParams(req, vlogsQuery, timestamp, timestamp)
|
|
|
|
} else {
|
|
|
|
c.setVLogsInstantReqParams(req, vlogsQuery, timestamp)
|
|
|
|
}
|
2024-07-12 19:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkFn(t, req)
|
|
|
|
}
|
|
|
|
|
2022-06-22 17:38:43 +00:00
|
|
|
authCfg, err := baCfg.NewConfig(".")
|
2021-09-14 11:32:06 +00:00
|
|
|
if err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2021-09-14 11:32:06 +00:00
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
storage := Client{
|
2023-06-01 07:44:11 +00:00
|
|
|
extraParams: url.Values{"round_digits": {"10"}},
|
|
|
|
}
|
2024-07-12 19:57:56 +00:00
|
|
|
|
|
|
|
// prometheus path
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, "/api/v1/query", r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// prometheus prefix
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
appendTypePrefix: true,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, "/prometheus/api/v1/query", r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// prometheus range path
|
2024-10-29 15:30:39 +00:00
|
|
|
f(true, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, "/api/v1/query_range", r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// prometheus range prefix
|
2024-10-29 15:30:39 +00:00
|
|
|
f(true, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
appendTypePrefix: true,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, "/prometheus/api/v1/query_range", r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// graphite path
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourceGraphite,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePath, r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// graphite prefix
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourceGraphite,
|
|
|
|
appendTypePrefix: true,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
checkEqualString(t, graphitePrefix+graphitePath, r.URL.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
// default params
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{dataSourceType: datasourcePrometheus}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {query}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
f(false, &Client{dataSourceType: datasourcePrometheus, applyIntervalAsTimeFilter: true}, func(t *testing.T, r *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
exp := url.Values{"query": {query}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// default range params
|
2024-10-29 15:30:39 +00:00
|
|
|
f(true, &Client{dataSourceType: datasourcePrometheus}, func(t *testing.T, r *http.Request) {
|
2024-07-12 19:57:56 +00:00
|
|
|
ts := timestamp.Format(time.RFC3339)
|
|
|
|
exp := url.Values{"query": {query}, "start": {ts}, "end": {ts}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// basic auth
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
authCfg: authCfg,
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
})
|
|
|
|
|
|
|
|
// basic auth range
|
2024-10-29 15:30:39 +00:00
|
|
|
f(true, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
authCfg: authCfg,
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
})
|
|
|
|
|
|
|
|
// evaluation interval
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
2024-07-12 19:57:56 +00:00
|
|
|
evaluationInterval: 15 * time.Second,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
evalInterval := 15 * time.Second
|
|
|
|
exp := url.Values{"query": {query}, "step": {evalInterval.String()}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// step override
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
queryStep: time.Minute,
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{
|
|
|
|
"query": {query},
|
|
|
|
"step": {fmt.Sprintf("%ds", int(time.Minute.Seconds()))},
|
|
|
|
"time": {timestamp.Format(time.RFC3339)},
|
|
|
|
}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// step to seconds
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
2024-07-12 19:57:56 +00:00
|
|
|
evaluationInterval: 3 * time.Hour,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
evalInterval := 3 * time.Hour
|
|
|
|
exp := url.Values{"query": {query}, "step": {fmt.Sprintf("%ds", int(evalInterval.Seconds()))}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// prometheus extra params
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
|
|
|
extraParams: url.Values{"round_digits": {"10"}},
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"10"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// prometheus extra params range
|
2024-10-29 15:30:39 +00:00
|
|
|
f(true, &Client{
|
|
|
|
dataSourceType: datasourcePrometheus,
|
2024-07-12 19:57:56 +00:00
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2021-08-31 11:57:47 +00:00
|
|
|
},
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{
|
|
|
|
"query": {query},
|
|
|
|
"end": {timestamp.Format(time.RFC3339)},
|
|
|
|
"start": {timestamp.Format(time.RFC3339)},
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
|
|
|
}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// custom params overrides the original params
|
|
|
|
f(false, storage.Clone().ApplyParams(QuerierParams{
|
2024-10-29 15:30:39 +00:00
|
|
|
DataSourceType: string(datasourcePrometheus),
|
|
|
|
QueryParams: url.Values{"round_digits": {"2"}},
|
2024-07-12 19:57:56 +00:00
|
|
|
}), func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"2"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// allow duplicates in query params
|
|
|
|
f(false, storage.Clone().ApplyParams(QuerierParams{
|
2024-10-29 15:30:39 +00:00
|
|
|
DataSourceType: string(datasourcePrometheus),
|
|
|
|
QueryParams: url.Values{"extra_labels": {"env=dev", "foo=bar"}},
|
2024-07-12 19:57:56 +00:00
|
|
|
}), func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {query}, "round_digits": {"10"}, "extra_labels": {"env=dev", "foo=bar"}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
// graphite extra params
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourceGraphite,
|
|
|
|
extraParams: url.Values{
|
|
|
|
"nocache": {"1"},
|
|
|
|
"max_lookback": {"1h"},
|
2023-07-21 10:28:10 +00:00
|
|
|
},
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("format=json&from=-5min&max_lookback=1h&nocache=1&target=%s&until=now", query)
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
})
|
2021-04-30 06:46:03 +00:00
|
|
|
|
2024-07-12 19:57:56 +00:00
|
|
|
// graphite extra params allows to override from
|
2024-10-29 15:30:39 +00:00
|
|
|
f(false, &Client{
|
2024-07-12 19:57:56 +00:00
|
|
|
dataSourceType: datasourceGraphite,
|
|
|
|
extraParams: url.Values{
|
|
|
|
"from": {"-10m"},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := fmt.Sprintf("format=json&from=-10m&target=%s&until=now", query)
|
|
|
|
checkEqualString(t, exp, r.URL.RawQuery)
|
|
|
|
})
|
2024-10-29 15:30:39 +00:00
|
|
|
|
|
|
|
// test vlogs
|
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourceVLogs,
|
|
|
|
evaluationInterval: time.Minute,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
exp := url.Values{"query": {vlogsQuery}, "time": {timestamp.Format(time.RFC3339)}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
f(false, &Client{
|
|
|
|
dataSourceType: datasourceVLogs,
|
|
|
|
evaluationInterval: time.Minute,
|
|
|
|
applyIntervalAsTimeFilter: true,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
ts := timestamp.Format(time.RFC3339)
|
|
|
|
exp := url.Values{"query": {vlogsQuery}, "time": {ts}, "start": {timestamp.Add(-time.Minute).Format(time.RFC3339)}, "end": {ts}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
|
|
|
|
|
|
|
f(true, &Client{
|
|
|
|
dataSourceType: datasourceVLogs,
|
|
|
|
evaluationInterval: time.Minute,
|
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
ts := timestamp.Format(time.RFC3339)
|
|
|
|
exp := url.Values{"query": {vlogsQuery}, "start": {ts}, "end": {ts}, "step": {"60s"}}
|
|
|
|
checkEqualString(t, exp.Encode(), r.URL.RawQuery)
|
|
|
|
})
|
2021-04-30 06:46:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 13:59:55 +00:00
|
|
|
func TestHeaders(t *testing.T) {
|
2024-10-29 15:30:39 +00:00
|
|
|
f := func(vmFn func() *Client, checkFn func(t *testing.T, r *http.Request)) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
vm := vmFn()
|
|
|
|
req, err := vm.newQueryRequest(ctx, "foo", time.Now())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error in newQueryRequest: %s", err)
|
|
|
|
}
|
|
|
|
checkFn(t, req)
|
2022-03-10 11:09:12 +00:00
|
|
|
}
|
2024-07-12 19:57:56 +00:00
|
|
|
|
|
|
|
// basic auth
|
2024-10-29 15:30:39 +00:00
|
|
|
f(func() *Client {
|
2024-07-12 19:57:56 +00:00
|
|
|
cfg, err := utils.AuthConfig(utils.WithBasicAuth("foo", "bar", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error get auth config: %s", err)
|
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
return NewPrometheusClient("", cfg, false, nil)
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "foo", u)
|
|
|
|
checkEqualString(t, "bar", p)
|
|
|
|
})
|
|
|
|
|
|
|
|
// bearer auth
|
2024-10-29 15:30:39 +00:00
|
|
|
f(func() *Client {
|
2024-07-12 19:57:56 +00:00
|
|
|
cfg, err := utils.AuthConfig(utils.WithBearer("foo", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error get auth config: %s", err)
|
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
return NewPrometheusClient("", cfg, false, nil)
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
reqToken := r.Header.Get("Authorization")
|
|
|
|
splitToken := strings.Split(reqToken, "Bearer ")
|
|
|
|
if len(splitToken) != 2 {
|
|
|
|
t.Fatalf("expected two items got %d", len(splitToken))
|
|
|
|
}
|
|
|
|
token := splitToken[1]
|
|
|
|
checkEqualString(t, "foo", token)
|
|
|
|
})
|
|
|
|
|
|
|
|
// custom extraHeaders
|
2024-10-29 15:30:39 +00:00
|
|
|
f(func() *Client {
|
|
|
|
c := NewPrometheusClient("", nil, false, nil)
|
|
|
|
c.extraHeaders = []keyValue{
|
2024-07-12 19:57:56 +00:00
|
|
|
{key: "Foo", value: "bar"},
|
|
|
|
{key: "Baz", value: "qux"},
|
2024-10-29 15:30:39 +00:00
|
|
|
}
|
|
|
|
return c
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
h1 := r.Header.Get("Foo")
|
|
|
|
checkEqualString(t, "bar", h1)
|
|
|
|
h2 := r.Header.Get("Baz")
|
|
|
|
checkEqualString(t, "qux", h2)
|
|
|
|
})
|
|
|
|
|
|
|
|
// custom header overrides basic auth
|
2024-10-29 15:30:39 +00:00
|
|
|
f(func() *Client {
|
2024-07-12 19:57:56 +00:00
|
|
|
cfg, err := utils.AuthConfig(utils.WithBasicAuth("foo", "bar", ""))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error get auth config: %s", err)
|
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
c := NewPrometheusClient("", cfg, false, nil)
|
|
|
|
c.extraHeaders = []keyValue{
|
|
|
|
{key: "Authorization", value: "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="},
|
2024-07-12 19:57:56 +00:00
|
|
|
}
|
2024-10-29 15:30:39 +00:00
|
|
|
return c
|
2024-07-12 19:57:56 +00:00
|
|
|
}, func(t *testing.T, r *http.Request) {
|
|
|
|
u, p, _ := r.BasicAuth()
|
|
|
|
checkEqualString(t, "Aladdin", u)
|
|
|
|
checkEqualString(t, "open sesame", p)
|
|
|
|
})
|
2022-03-10 11:09:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 06:46:03 +00:00
|
|
|
func checkEqualString(t *testing.T, exp, got string) {
|
|
|
|
t.Helper()
|
2024-07-12 19:57:56 +00:00
|
|
|
|
2021-04-30 06:46:03 +00:00
|
|
|
if got != exp {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected to get: \n%q; \ngot: \n%q", exp, got)
|
2021-04-30 06:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 09:20:38 +00:00
|
|
|
|
|
|
|
func expectError(t *testing.T, err error, exp string) {
|
|
|
|
t.Helper()
|
2024-07-12 19:57:56 +00:00
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
if err == nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected non-nil error")
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), exp) {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected error %q to contain %q", err, exp)
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
}
|