Browse Source

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>
pull/13137/head
Bryan Boreham 1 year ago committed by GitHub
parent
commit
41758972e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      web/api/v1/api.go

16
web/api/v1/api.go

@ -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}
}

Loading…
Cancel
Save