From ee4288987b45156944ac64845168740f0f01a509 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 11 Feb 2021 15:00:12 +0200 Subject: [PATCH] app/vmselect/prometheus: treat `match` query arg in the same way as `match[]` query arg --- app/vmselect/prometheus/prometheus.go | 33 ++++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index d8d0fbfb22..414f112501 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -266,9 +266,9 @@ func ExportHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r if err := r.ParseForm(); err != nil { return fmt.Errorf("cannot parse request form values: %w", err) } - matches, err := getMatchesFromRequest(r) - if err != nil { - return err + matches := getMatchesFromRequest(r) + if len(matches) == 0 { + return fmt.Errorf("missing `match[]` query arg") } start, err := searchutils.GetTime(r, "start", 0) if err != nil { @@ -524,10 +524,11 @@ func LabelValuesHandler(startTime time.Time, at *auth.Token, labelName string, w if err != nil { return err } + matches := getMatchesFromRequest(r) var labelValues []string var isPartial bool denyPartialResponse := searchutils.GetDenyPartialResponse(r) - if len(r.Form["match[]"]) == 0 && len(etf) == 0 { + if len(matches) == 0 && len(etf) == 0 { if len(r.Form["start"]) == 0 && len(r.Form["end"]) == 0 { var err error labelValues, isPartial, err = netstorage.GetLabelValues(at, denyPartialResponse, labelName, deadline) @@ -558,7 +559,6 @@ func LabelValuesHandler(startTime time.Time, at *auth.Token, labelName string, w // i.e. /api/v1/label/foo/values?match[]=foobar{baz="abc"}&start=...&end=... // is equivalent to `label_values(foobar{baz="abc"}, foo)` call on the selected // time range in Grafana templating. - matches := r.Form["match[]"] if len(matches) == 0 { matches = []string{fmt.Sprintf("{%s!=''}", labelName)} } @@ -749,10 +749,11 @@ func LabelsHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r if err != nil { return err } + matches := getMatchesFromRequest(r) var labels []string var isPartial bool denyPartialResponse := searchutils.GetDenyPartialResponse(r) - if len(r.Form["match[]"]) == 0 && len(etf) == 0 { + if len(matches) == 0 && len(etf) == 0 { if len(r.Form["start"]) == 0 && len(r.Form["end"]) == 0 { var err error labels, isPartial, err = netstorage.GetLabels(at, denyPartialResponse, deadline) @@ -781,7 +782,6 @@ func LabelsHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r } else { // Extended functionality that allows filtering by label filters and time range // i.e. /api/v1/labels?match[]=foobar{baz="abc"}&start=...&end=... - matches := r.Form["match[]"] if len(matches) == 0 { matches = []string{"{__name__!=''}"} } @@ -1346,9 +1346,9 @@ func getTagFilterssFromMatches(matches []string) ([][]storage.TagFilter, error) } func getTagFilterssFromRequest(r *http.Request) ([][]storage.TagFilter, error) { - matches, err := getMatchesFromRequest(r) - if err != nil { - return nil, err + matches := getMatchesFromRequest(r) + if len(matches) == 0 { + return nil, fmt.Errorf("missing `match[]` query arg") } tagFilterss, err := getTagFilterssFromMatches(matches) if err != nil { @@ -1362,16 +1362,11 @@ func getTagFilterssFromRequest(r *http.Request) ([][]storage.TagFilter, error) { return tagFilterss, nil } -func getMatchesFromRequest(r *http.Request) ([]string, error) { +func getMatchesFromRequest(r *http.Request) []string { matches := r.Form["match[]"] - if len(matches) > 0 { - return matches, nil - } - match := r.Form.Get("match") - if len(match) == 0 { - return nil, fmt.Errorf("missing `match[]` query arg") - } - return []string{match}, nil + // This is needed for backwards compatibility + matches = append(matches, r.Form["match"]...) + return matches } func getLatencyOffsetMilliseconds() int64 {