Moved warnings into apiclient

Signed-off-by: Joe Elliott <number101010@gmail.com>
This commit is contained in:
Joe Elliott 2019-12-11 10:04:00 -05:00
parent bd858421cd
commit 88792b1169
3 changed files with 50 additions and 51 deletions

View File

@ -25,8 +25,6 @@ import (
"time"
)
type Warnings []string
// DefaultRoundTripper is used if no RoundTripper is set in Config.
var DefaultRoundTripper http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,

View File

@ -230,15 +230,15 @@ type API interface {
// Flags returns the flag values that Prometheus was launched with.
Flags(ctx context.Context) (FlagsResult, error)
// LabelNames returns all the unique label names present in the block in sorted order.
LabelNames(ctx context.Context) ([]string, api.Warnings, error)
LabelNames(ctx context.Context) ([]string, Warnings, error)
// LabelValues performs a query for the values of the given label.
LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error)
LabelValues(ctx context.Context, label string) (model.LabelValues, Warnings, error)
// Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error)
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
// QueryRange performs a query for the given range.
QueryRange(ctx context.Context, query string, r Range) (model.Value, api.Warnings, error)
QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error)
// Series finds series by label matchers.
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error)
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error)
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
// under the TSDB's data directory and returns the directory as response.
Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
@ -630,7 +630,7 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
return res, json.Unmarshal(body, &res)
}
func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error) {
func (h *httpAPI) LabelNames(ctx context.Context) ([]string, Warnings, error) {
u := h.client.URL(epLabels, nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
@ -644,7 +644,7 @@ func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error
return labelNames, w, json.Unmarshal(body, &labelNames)
}
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error) {
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, Warnings, error) {
u := h.client.URL(epLabelValues, map[string]string{"name": label})
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
@ -658,7 +658,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
return labelValues, w, json.Unmarshal(body, &labelValues)
}
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error) {
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error) {
u := h.client.URL(epQuery, nil)
q := u.Query()
@ -676,7 +676,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
}
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.Value, api.Warnings, error) {
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error) {
u := h.client.URL(epQueryRange, nil)
q := u.Query()
@ -695,7 +695,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
}
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error) {
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error) {
u := h.client.URL(epSeries, nil)
q := u.Query()
@ -802,12 +802,15 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
return res, json.Unmarshal(body, &res)
}
// Warnings is an array of non critical errors
type Warnings []string
// apiClient wraps a regular client and processes successful API responses.
// Successful also includes responses that errored at the API level.
type apiClient interface {
URL(ep string, args map[string]string) *url.URL
Do(context.Context, *http.Request) (*http.Response, []byte, api.Warnings, error)
DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error)
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error)
}
type apiClientImpl struct {
@ -841,7 +844,7 @@ func (h *apiClientImpl) URL(ep string, args map[string]string) *url.URL {
return h.client.URL(ep, args)
}
func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
resp, body, err := h.client.Do(ctx, req)
if err != nil {
return resp, body, nil, err
@ -888,7 +891,7 @@ func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Respon
}
// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error) {
func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
if err != nil {
return nil, nil, nil, err

View File

@ -30,12 +30,10 @@ import (
json "github.com/json-iterator/go"
"github.com/prometheus/common/model"
"github.com/prometheus/client_golang/api"
)
type apiTest struct {
do func() (interface{}, api.Warnings, error)
do func() (interface{}, Warnings, error)
inWarnings []string
inErr error
inStatusCode int
@ -45,7 +43,7 @@ type apiTest struct {
reqParam url.Values
reqMethod string
res interface{}
warnings api.Warnings
warnings Warnings
err error
}
@ -66,7 +64,7 @@ func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
return u
}
func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
test := c.curTest
@ -94,7 +92,7 @@ func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Respon
return resp, b, test.inWarnings, test.inErr
}
func (c *apiTestClient) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error) {
func (c *apiTestClient) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
if err != nil {
return nil, nil, nil, err
@ -113,92 +111,92 @@ func TestAPIs(t *testing.T) {
client: tc,
}
doAlertManagers := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doAlertManagers := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.AlertManagers(context.Background())
return v, nil, err
}
}
doCleanTombstones := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doCleanTombstones := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return nil, nil, promAPI.CleanTombstones(context.Background())
}
}
doConfig := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doConfig := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Config(context.Background())
return v, nil, err
}
}
doDeleteSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doDeleteSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return nil, nil, promAPI.DeleteSeries(context.Background(), []string{matcher}, startTime, endTime)
}
}
doFlags := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doFlags := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Flags(context.Background())
return v, nil, err
}
}
doLabelNames := func(label string) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doLabelNames := func(label string) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.LabelNames(context.Background())
}
}
doLabelValues := func(label string) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doLabelValues := func(label string) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.LabelValues(context.Background(), label)
}
}
doQuery := func(q string, ts time.Time) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doQuery := func(q string, ts time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.Query(context.Background(), q, ts)
}
}
doQueryRange := func(q string, rng Range) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doQueryRange := func(q string, rng Range) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.QueryRange(context.Background(), q, rng)
}
}
doSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.Series(context.Background(), []string{matcher}, startTime, endTime)
}
}
doSnapshot := func(skipHead bool) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doSnapshot := func(skipHead bool) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Snapshot(context.Background(), skipHead)
return v, nil, err
}
}
doRules := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doRules := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Rules(context.Background())
return v, nil, err
}
}
doTargets := func() func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doTargets := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Targets(context.Background())
return v, nil, err
}
}
doTargetsMetadata := func(matchTarget string, metric string, limit string) func() (interface{}, api.Warnings, error) {
return func() (interface{}, api.Warnings, error) {
doTargetsMetadata := func(matchTarget string, metric string, limit string) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.TargetsMetadata(context.Background(), matchTarget, metric, limit)
return v, nil, err
}
@ -911,7 +909,7 @@ type apiClientTest struct {
response interface{}
expectedBody string
expectedErr *Error
expectedWarnings api.Warnings
expectedWarnings Warnings
}
func (c *testClient) URL(ep string, args map[string]string) *url.URL {