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

Add support for stringlabels in Thanos Query #7645

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 7 additions & 8 deletions cmd/thanos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package main

import (
"net/url"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -266,23 +265,23 @@ func (ac *alertMgrConfig) registerFlag(cmd extflag.FlagClause) *alertMgrConfig {
}

func parseFlagLabels(s []string) (labels.Labels, error) {
var lset labels.Labels
var lset labels.ScratchBuilder
for _, l := range s {
parts := strings.SplitN(l, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("unrecognized label %q", l)
return labels.EmptyLabels(), errors.Errorf("unrecognized label %q", l)
}
if !model.LabelName.IsValid(model.LabelName(parts[0])) {
return nil, errors.Errorf("unsupported format for label %s", l)
return labels.EmptyLabels(), errors.Errorf("unsupported format for label %s", l)
}
val, err := strconv.Unquote(parts[1])
if err != nil {
return nil, errors.Wrap(err, "unquote label value")
return labels.EmptyLabels(), errors.Wrap(err, "unquote label value")
}
lset = append(lset, labels.Label{Name: parts[0], Value: val})
lset.Add(parts[0], val)
}
sort.Sort(lset)
return lset, nil
lset.Sort()
return lset.Labels(), nil
}

type goMemLimitConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func registerReceive(app *extkingpin.App) {
if !model.LabelName.IsValid(model.LabelName(conf.tenantLabelName)) {
return errors.Errorf("unsupported format for tenant label name, got %s", conf.tenantLabelName)
}
if len(lset) == 0 {
if lset.Len() == 0 {
return errors.New("no external labels configured for receive, uniquely identifying external labels must be configured (ideally with `receive_` prefix); see https://thanos.io/tip/thanos/storage.md#external-labels for details.")
}

Expand Down
8 changes: 1 addition & 7 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,13 +887,7 @@ func removeLockfileIfAny(logger log.Logger, dataDir string) error {
}

func labelsTSDBToProm(lset labels.Labels) (res labels.Labels) {
for _, l := range lset {
res = append(res, labels.Label{
Name: l.Name,
Value: l.Value,
})
}
return res
return lset.Copy()
}

func queryFuncCreator(
Expand Down
4 changes: 2 additions & 2 deletions cmd/thanos/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func runSidecar(
return errors.Wrap(err, "initial external labels query")
}

if len(m.Labels()) == 0 {
if m.Labels().Len() == 0 {
return errors.New("no external labels configured on Prometheus server, uniquely identifying external labels must be configured; see https://thanos.io/tip/thanos/storage.md#external-labels for details.")
}
promUp.Set(1)
Expand Down Expand Up @@ -393,7 +393,7 @@ func runSidecar(
defer cancel()

if err := runutil.Retry(2*time.Second, extLabelsCtx.Done(), func() error {
if len(m.Labels()) == 0 {
if m.Labels().Len() == 0 {
return errors.New("not uploading as no external labels are configured yet - is Prometheus healthy/reachable?")
}
return nil
Expand Down
12 changes: 6 additions & 6 deletions cmd/thanos/tools_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,12 +1013,12 @@ func getKeysAlphabetically(labels map[string]string) []string {
// matchesSelector checks if blockMeta contains every label from
// the selector with the correct value.
func matchesSelector(blockMeta *metadata.Meta, selectorLabels labels.Labels) bool {
for _, l := range selectorLabels {
if v, ok := blockMeta.Thanos.Labels[l.Name]; !ok || v != l.Value {
return false
}
}
return true
matches := true
selectorLabels.Range(func(l labels.Label) {
val, ok := blockMeta.Thanos.Labels[l.Name]
matches = matches && ok && val == l.Value
})
return matches
}

// getIndex calculates the index of s in strs.
Expand Down
6 changes: 1 addition & 5 deletions pkg/query/endpointset.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,7 @@ func (er *endpointRef) labelSets() []labels.Labels {

labelSet := make([]labels.Labels, 0, len(er.metadata.LabelSets))
for _, ls := range labelpb.ZLabelSetsToPromLabelSets(er.metadata.LabelSets...) {
if len(ls) == 0 {
continue
}
// Compatibility label for Queriers pre 0.8.1. Filter it out now.
if ls[0].Name == store.CompatibilityTypeLabelName {
if ls.Len() == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This removes the ancient store.CompatibilityTypeLabelName which I think is okay given how old the supported version is.

continue
}
labelSet = append(labelSet, ls.Copy())
Expand Down
11 changes: 0 additions & 11 deletions pkg/query/endpointset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/info/infopb"
"github.com/thanos-io/thanos/pkg/store"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
)
Expand Down Expand Up @@ -954,11 +953,6 @@ func TestEndpointSetUpdate_AvailabilityScenarios(t *testing.T) {
{Name: "l3", Value: "v4"},
},
},
{
Labels: []labelpb.ZLabel{
{Name: store.CompatibilityTypeLabelName, Value: "store"},
},
},
}
},
},
Expand All @@ -978,11 +972,6 @@ func TestEndpointSetUpdate_AvailabilityScenarios(t *testing.T) {
{Name: "l3", Value: "v4"},
},
},
{
Labels: []labelpb.ZLabel{
{Name: store.CompatibilityTypeLabelName, Value: "store"},
},
},
}
},
},
Expand Down
Loading