web/api: optimize labelnames/values with 1 set of matchers (#12888)

* web/api: optimize labelnames/values with 1 set of matchers

If there is exactly one set of matchers provided, we can skip adding
the results to a map and getting them back out again.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>

---------

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-11-13 18:28:48 +00:00 committed by GitHub
parent 37573d083b
commit 41758972e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -668,7 +668,7 @@ func (api *API) labelNames(r *http.Request) apiFuncResult {
names []string
warnings annotations.Annotations
)
if len(matcherSets) > 0 {
if len(matcherSets) > 1 {
labelNamesSet := make(map[string]struct{})
for _, matchers := range matcherSets {
@ -690,7 +690,11 @@ func (api *API) labelNames(r *http.Request) apiFuncResult {
}
slices.Sort(names)
} else {
names, warnings, err = q.LabelNames(r.Context())
var matchers []*labels.Matcher
if len(matcherSets) == 1 {
matchers = matcherSets[0]
}
names, warnings, err = q.LabelNames(r.Context(), matchers...)
if err != nil {
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, nil}
}
@ -744,7 +748,7 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) {
vals []string
warnings annotations.Annotations
)
if len(matcherSets) > 0 {
if len(matcherSets) > 1 {
var callWarnings annotations.Annotations
labelValuesSet := make(map[string]struct{})
for _, matchers := range matcherSets {
@ -763,7 +767,11 @@ func (api *API) labelValues(r *http.Request) (result apiFuncResult) {
vals = append(vals, val)
}
} else {
vals, warnings, err = q.LabelValues(ctx, name)
var matchers []*labels.Matcher
if len(matcherSets) == 1 {
matchers = matcherSets[0]
}
vals, warnings, err = q.LabelValues(ctx, name, matchers...)
if err != nil {
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, closer}
}