Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support match[] encoding #1875

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask why sorting is required?

Copy link
Member Author

@owen-d owen-d Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it for deterministic tests, but you're totally right - I could just sort it in the test. Good catch @yeya24

Edit: actually it looks a bit harder to sort it in tests and the runtime cost is minimal, so I'll leave it for simplicity.


// 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