Skip to content

Commit

Permalink
support match[] encoding (#1875)
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-d authored Mar 31, 2020
1 parent 8d1b345 commit 726b8cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/loghttp/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loghttp

import (
"net/http"
"sort"

"github.com/grafana/loki/pkg/logproto"
)
Expand All @@ -18,18 +19,40 @@ func ParseSeriesQuery(r *http.Request) (*logproto.SeriesRequest, error) {
}

xs := r.Form["match"]
// Prometheus encodes with `match[]`; we use both for compatibility.
ys := r.Form["match[]"]

deduped := union(xs, ys)
sort.Strings(deduped)

// ensure matchers are valid before fanning out to ingesters/store as well as returning valuable parsing errors
// instead of 500s
_, err = Match(xs)
_, err = Match(deduped)
if err != nil {
return nil, err
}

return &logproto.SeriesRequest{
Start: start,
End: end,
Groups: xs,
Groups: deduped,
}, nil

}

func union(cols ...[]string) []string {
m := map[string]struct{}{}

for _, col := range cols {
for _, s := range col {
m[s] = struct{}{}
}
}

res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}

return res
}
22 changes: 22 additions & 0 deletions pkg/loghttp/series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ func TestParseSeriesQuery(t *testing.T) {
false,
mkSeriesRequest(t, "1000", "2000", []string{`{a="1"}`, `{b="2", c=~"3", d!="4"}`}),
},
{
"mixes match encodings",
withForm(url.Values{
"start": []string{"1000"},
"end": []string{"2000"},
"match": []string{`{a="1"}`},
"match[]": []string{`{b="2"}`},
}),
false,
mkSeriesRequest(t, "1000", "2000", []string{`{a="1"}`, `{b="2"}`}),
},
{
"dedupes match encodings",
withForm(url.Values{
"start": []string{"1000"},
"end": []string{"2000"},
"match": []string{`{a="1"}`, `{b="2"}`},
"match[]": []string{`{b="2"}`, `{c="3"}`},
}),
false,
mkSeriesRequest(t, "1000", "2000", []string{`{a="1"}`, `{b="2"}`, `{c="3"}`}),
},
} {
t.Run(tc.desc, func(t *testing.T) {
out, err := ParseSeriesQuery(tc.input)
Expand Down

0 comments on commit 726b8cd

Please sign in to comment.