mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Apply matchers when fetching label values
This commit is contained in:
parent
8ef48ad9a7
commit
cc9072ad64
|
@ -338,6 +338,24 @@ func labelValuesWithMatchers(r IndexReader, name string, matchers ...*labels.Mat
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "fetching values of label %s", name)
|
return nil, errors.Wrapf(err, "fetching values of label %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have a matcher for the label name, we can filter out values that don't match
|
||||||
|
// before we fetch postings. This is especially useful for labels with many values.
|
||||||
|
// e.g. __name__ with a selector like {__name__="xyz"}
|
||||||
|
for _, m := range matchers {
|
||||||
|
if m.Name != name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := len(allValues) - 1; i >= 0; i-- {
|
||||||
|
if m.Matches(allValues[i]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
allValues = append(allValues[:i], allValues[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
valuesPostings := make([]index.Postings, len(allValues))
|
valuesPostings := make([]index.Postings, len(allValues))
|
||||||
for i, value := range allValues {
|
for i, value := range allValues {
|
||||||
valuesPostings[i], err = r.Postings(name, value)
|
valuesPostings[i], err = r.Postings(name, value)
|
||||||
|
|
|
@ -182,6 +182,7 @@ func benchmarkLabelValuesWithMatchers(b *testing.B, ir IndexReader) {
|
||||||
labelName string
|
labelName string
|
||||||
matchers []*labels.Matcher
|
matchers []*labels.Matcher
|
||||||
}{
|
}{
|
||||||
|
{`i with i="1"`, "i", []*labels.Matcher{i1}},
|
||||||
// i has 100k values.
|
// i has 100k values.
|
||||||
{`i with n="1"`, "i", []*labels.Matcher{n1}},
|
{`i with n="1"`, "i", []*labels.Matcher{n1}},
|
||||||
{`i with n="^.+$"`, "i", []*labels.Matcher{nPlus}},
|
{`i with n="^.+$"`, "i", []*labels.Matcher{nPlus}},
|
||||||
|
|
Loading…
Reference in a new issue