A Prometheus check runs an instant query against the query API of Prometheus or a compatible server and requires every series, or at least one, to meet the condition. No data fails unless allowed. Failures name the series, its value and the expectation; query errors are passed on. Refs #25 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
83 lines
3.3 KiB
Go
83 lines
3.3 KiB
Go
package check
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPrometheus(t *testing.T) {
|
|
results := map[string]string{
|
|
`up{job="api"}`: `{"status":"success","data":{"resultType":"vector","result":[
|
|
{"metric":{"__name__":"up","job":"api","instance":"a:9100"},"value":[1700000000,"1"]},
|
|
{"metric":{"__name__":"up","job":"api","instance":"b:9100"},"value":[1700000000,"0"]}]}}`,
|
|
`scalar(1)`: `{"status":"success","data":{"resultType":"scalar","result":[1700000000,"1"]}}`,
|
|
`absent_metric`: `{"status":"success","data":{"resultType":"vector","result":[]}}`,
|
|
`rate(x[5m])[1h:]`: `{"status":"success","data":{"resultType":"matrix","result":[]}}`,
|
|
`bad(`: `{"status":"error","errorType":"bad_data","error":"parse error"}`,
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/prom/api/v1/query" || r.Header.Get("Authorization") != "Basic dTpw" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
body, ok := results[r.URL.Query().Get("query")]
|
|
if !ok {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(body))
|
|
}))
|
|
defer srv.Close()
|
|
base := srv.URL + "/prom/"
|
|
run := func(cfg map[string]any) Outcome {
|
|
t.Helper()
|
|
cfg["headers"] = map[string]string{"Authorization": "Basic dTpw"}
|
|
raw, _ := json.Marshal(cfg)
|
|
c, err := Validate(Prometheus, base, raw)
|
|
if err != nil {
|
|
t.Fatalf("Validate(%s) = %v", raw, err)
|
|
}
|
|
return Run(context.Background(), Spec{Type: Prometheus, Target: base, Timeout: 2 * time.Second, Config: c, AllowPrivate: true})
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
cfg map[string]any
|
|
ok bool
|
|
msg string
|
|
}{
|
|
{"all series up fails on one down", map[string]any{"query": `up{job="api"}`, "op": "==", "threshold": 1}, false,
|
|
`up{instance="b:9100",job="api"} = 0, expected == 1`},
|
|
{"any series up", map[string]any{"query": `up{job="api"}`, "op": "==", "threshold": 1, "mode": "any"}, true, ""},
|
|
{"none above 1 lists count", map[string]any{"query": `up{job="api"}`, "op": ">", "threshold": 1}, false, "(2 of 2 series fail)"},
|
|
{"scalar", map[string]any{"query": `scalar(1)`, "op": ">=", "threshold": 1}, true, ""},
|
|
{"no data fails", map[string]any{"query": `absent_metric`}, false, "query returned no data"},
|
|
{"no data may be fine", map[string]any{"query": `absent_metric`, "empty_ok": true}, true, ""},
|
|
{"range vectors are refused", map[string]any{"query": `rate(x[5m])[1h:]`}, false, "unsupported result type"},
|
|
{"query errors are reported", map[string]any{"query": `bad(`}, false, "query failed: bad_data: parse error"},
|
|
} {
|
|
o := run(tc.cfg)
|
|
if o.OK != tc.ok || !strings.Contains(o.Error, tc.msg) {
|
|
t.Errorf("%s: %+v, want ok=%t with %q", tc.name, o, tc.ok, tc.msg)
|
|
}
|
|
}
|
|
|
|
for cfg, code := range map[string]string{
|
|
`{}`: "invalid_query",
|
|
`{"query":"up","op":"~"}`: "invalid_assertion_op",
|
|
`{"query":"up","mode":"most"}`: "invalid_config",
|
|
`{"query":"up","headers":{"Host":"x"}}`: "invalid_header",
|
|
} {
|
|
_, err := Validate(Prometheus, base, json.RawMessage(cfg))
|
|
var inv ErrInvalid
|
|
if !errors.As(err, &inv) || inv.Code != code {
|
|
t.Errorf("Validate(%s) = %v, want %s", cfg, err, code)
|
|
}
|
|
}
|
|
}
|