Small cleanup of API code. (#8258)
* Small cleanup of API code. Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com> * Fixed typo spotted by Julien. Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
This commit is contained in:
parent
dca6954b0a
commit
70c69aaa6c
|
@ -330,6 +330,12 @@ type queryData struct {
|
|||
Stats *stats.QueryStats `json:"stats,omitempty"`
|
||||
}
|
||||
|
||||
func invalidParamError(err error, parameter string) apiFuncResult {
|
||||
return apiFuncResult{nil, &apiError{
|
||||
errorBadData, errors.Wrapf(err, "invalid parameter %q", parameter),
|
||||
}, nil, nil}
|
||||
}
|
||||
|
||||
func (api *API) options(r *http.Request) apiFuncResult {
|
||||
return apiFuncResult{nil, nil, nil, nil}
|
||||
}
|
||||
|
@ -337,15 +343,14 @@ func (api *API) options(r *http.Request) apiFuncResult {
|
|||
func (api *API) query(r *http.Request) (result apiFuncResult) {
|
||||
ts, err := parseTimeParam(r, "time", api.now())
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "time")
|
||||
}
|
||||
ctx := r.Context()
|
||||
if to := r.FormValue("timeout"); to != "" {
|
||||
var cancel context.CancelFunc
|
||||
timeout, err := parseDuration(to)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "invalid parameter 'timeout'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "timeout")
|
||||
}
|
||||
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
|
@ -354,9 +359,9 @@ func (api *API) query(r *http.Request) (result apiFuncResult) {
|
|||
|
||||
qry, err := api.QueryEngine.NewInstantQuery(api.Queryable, r.FormValue("query"), ts)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "invalid parameter 'query'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "query")
|
||||
}
|
||||
|
||||
// From now on, we must only return with a finalizer in the result (to
|
||||
// be called by the caller) or call qry.Close ourselves (which is
|
||||
// required in the case of a panic).
|
||||
|
@ -389,28 +394,23 @@ func (api *API) query(r *http.Request) (result apiFuncResult) {
|
|||
func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
|
||||
start, err := parseTime(r.FormValue("start"))
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "invalid parameter 'start'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "start")
|
||||
}
|
||||
end, err := parseTime(r.FormValue("end"))
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "invalid parameter 'end'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "end")
|
||||
}
|
||||
if end.Before(start) {
|
||||
err := errors.New("end timestamp must not be before start time")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(errors.New("end timestamp must not be before start time"), "end")
|
||||
}
|
||||
|
||||
step, err := parseDuration(r.FormValue("step"))
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "invalid parameter 'step'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "step")
|
||||
}
|
||||
|
||||
if step <= 0 {
|
||||
err := errors.New("zero or negative query resolution step widths are not accepted. Try a positive integer")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(errors.New("zero or negative query resolution step widths are not accepted. Try a positive integer"), "step")
|
||||
}
|
||||
|
||||
// For safety, limit the number of returned points per timeseries.
|
||||
|
@ -425,8 +425,7 @@ func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
|
|||
var cancel context.CancelFunc
|
||||
timeout, err := parseDuration(to)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "invalid parameter 'timeout'")
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "timeout")
|
||||
}
|
||||
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
|
@ -486,11 +485,11 @@ func returnAPIError(err error) *apiError {
|
|||
func (api *API) labelNames(r *http.Request) apiFuncResult {
|
||||
start, err := parseTimeParam(r, "start", minTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrap(err, "invalid parameter 'start'")}, nil, nil}
|
||||
return invalidParamError(err, "start")
|
||||
}
|
||||
end, err := parseTimeParam(r, "end", maxTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrap(err, "invalid parameter 'end'")}, nil, nil}
|
||||
return invalidParamError(err, "end")
|
||||
}
|
||||
|
||||
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
||||
|
@ -560,11 +559,11 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) {
|
|||
|
||||
start, err := parseTimeParam(r, "start", minTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrap(err, "invalid parameter 'start'")}, nil, nil}
|
||||
return invalidParamError(err, "start")
|
||||
}
|
||||
end, err := parseTimeParam(r, "end", maxTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrap(err, "invalid parameter 'end'")}, nil, nil}
|
||||
return invalidParamError(err, "end")
|
||||
}
|
||||
|
||||
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
||||
|
@ -656,16 +655,16 @@ func (api *API) series(r *http.Request) (result apiFuncResult) {
|
|||
|
||||
start, err := parseTimeParam(r, "start", minTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "start")
|
||||
}
|
||||
end, err := parseTimeParam(r, "end", maxTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "end")
|
||||
}
|
||||
|
||||
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "match[]")
|
||||
}
|
||||
|
||||
q, err := api.Queryable.Querier(r.Context(), timestamp.FromTime(start), timestamp.FromTime(end))
|
||||
|
@ -711,7 +710,7 @@ func (api *API) series(r *http.Request) (result apiFuncResult) {
|
|||
return apiFuncResult{metrics, nil, warnings, closer}
|
||||
}
|
||||
|
||||
func (api *API) dropSeries(r *http.Request) apiFuncResult {
|
||||
func (api *API) dropSeries(_ *http.Request) apiFuncResult {
|
||||
return apiFuncResult{nil, &apiError{errorInternal, errors.New("not implemented")}, nil, nil}
|
||||
}
|
||||
|
||||
|
@ -889,20 +888,16 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
|
|||
}
|
||||
|
||||
matchTarget := r.FormValue("match_target")
|
||||
|
||||
var matchers []*labels.Matcher
|
||||
|
||||
var err error
|
||||
|
||||
if matchTarget != "" {
|
||||
matchers, err = parser.ParseMetricSelector(matchTarget)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "match_target")
|
||||
}
|
||||
}
|
||||
|
||||
metric := r.FormValue("metric")
|
||||
|
||||
res := []metricMetadata{}
|
||||
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
||||
for _, t := range tt {
|
||||
|
@ -1036,7 +1031,6 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
|
|||
}
|
||||
|
||||
metric := r.FormValue("metric")
|
||||
|
||||
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
||||
for _, t := range tt {
|
||||
|
||||
|
@ -1069,7 +1063,6 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
|
|||
|
||||
// Put the elements from the pseudo-set into a slice for marshaling.
|
||||
res := map[string][]metadata{}
|
||||
|
||||
for name, set := range metrics {
|
||||
if limit >= 0 && len(res) >= limit {
|
||||
break
|
||||
|
@ -1137,15 +1130,14 @@ type recordingRule struct {
|
|||
func (api *API) rules(r *http.Request) apiFuncResult {
|
||||
ruleGroups := api.rulesRetriever(r.Context()).RuleGroups()
|
||||
res := &RuleDiscovery{RuleGroups: make([]*RuleGroup, len(ruleGroups))}
|
||||
typeParam := strings.ToLower(r.URL.Query().Get("type"))
|
||||
typ := strings.ToLower(r.URL.Query().Get("type"))
|
||||
|
||||
if typeParam != "" && typeParam != "alert" && typeParam != "record" {
|
||||
err := errors.Errorf("invalid query parameter type='%v'", typeParam)
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
if typ != "" && typ != "alert" && typ != "record" {
|
||||
return invalidParamError(errors.Errorf("not supported value %q", typ), "type")
|
||||
}
|
||||
|
||||
returnAlerts := typeParam == "" || typeParam == "alert"
|
||||
returnRecording := typeParam == "" || typeParam == "record"
|
||||
returnAlerts := typ == "" || typ == "alert"
|
||||
returnRecording := typ == "" || typ == "record"
|
||||
|
||||
for i, grp := range ruleGroups {
|
||||
apiRuleGroup := &RuleGroup{
|
||||
|
@ -1213,7 +1205,7 @@ type prometheusConfig struct {
|
|||
YAML string `json:"yaml"`
|
||||
}
|
||||
|
||||
func (api *API) serveRuntimeInfo(r *http.Request) apiFuncResult {
|
||||
func (api *API) serveRuntimeInfo(_ *http.Request) apiFuncResult {
|
||||
status, err := api.runtimeInfo()
|
||||
if err != nil {
|
||||
return apiFuncResult{status, &apiError{errorInternal, err}, nil, nil}
|
||||
|
@ -1221,18 +1213,18 @@ func (api *API) serveRuntimeInfo(r *http.Request) apiFuncResult {
|
|||
return apiFuncResult{status, nil, nil, nil}
|
||||
}
|
||||
|
||||
func (api *API) serveBuildInfo(r *http.Request) apiFuncResult {
|
||||
func (api *API) serveBuildInfo(_ *http.Request) apiFuncResult {
|
||||
return apiFuncResult{api.buildInfo, nil, nil, nil}
|
||||
}
|
||||
|
||||
func (api *API) serveConfig(r *http.Request) apiFuncResult {
|
||||
func (api *API) serveConfig(_ *http.Request) apiFuncResult {
|
||||
cfg := &prometheusConfig{
|
||||
YAML: api.config().String(),
|
||||
}
|
||||
return apiFuncResult{cfg, nil, nil, nil}
|
||||
}
|
||||
|
||||
func (api *API) serveFlags(r *http.Request) apiFuncResult {
|
||||
func (api *API) serveFlags(_ *http.Request) apiFuncResult {
|
||||
return apiFuncResult{api.flagsMap, nil, nil, nil}
|
||||
}
|
||||
|
||||
|
@ -1522,17 +1514,17 @@ func (api *API) deleteSeries(r *http.Request) apiFuncResult {
|
|||
|
||||
start, err := parseTimeParam(r, "start", minTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "start")
|
||||
}
|
||||
end, err := parseTimeParam(r, "end", maxTime)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "end")
|
||||
}
|
||||
|
||||
for _, s := range r.Form["match[]"] {
|
||||
matchers, err := parser.ParseMetricSelector(s)
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
||||
return invalidParamError(err, "match[]")
|
||||
}
|
||||
if err := api.db.Delete(timestamp.FromTime(start), timestamp.FromTime(end), matchers...); err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil}
|
||||
|
@ -1553,7 +1545,7 @@ func (api *API) snapshot(r *http.Request) apiFuncResult {
|
|||
if r.FormValue("skip_head") != "" {
|
||||
skipHead, err = strconv.ParseBool(r.FormValue("skip_head"))
|
||||
if err != nil {
|
||||
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrapf(err, "unable to parse boolean 'skip_head' argument")}, nil, nil}
|
||||
return invalidParamError(errors.Wrapf(err, "unable to parse boolean"), "skip_head")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue