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

[Clustering] Filter the label "instance" from the hash computation #6792

Closed
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Main (unreleased)
whenever that argument is explicitly configured. This issue only affected a
small subset of arguments across 15 components. (@erikbaranowski, @rfratto)

- Fix an issue where targets exposed by exporters were not distributed correctly between agents in clustering mode. (@wildum)

### Other changes

- Clustering for Grafana Agent in Flow mode has graduated from beta to stable.
Expand Down
11 changes: 8 additions & 3 deletions internal/component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (t *DistributedTargets) Get() []Target {
res := make([]Target, 0, resCap)

for _, tgt := range t.targets {
peers, err := t.cluster.Lookup(shard.StringKey(tgt.NonMetaLabels().String()), 1, shard.OpReadWrite)
peers, err := t.cluster.Lookup(shard.StringKey(tgt.FilteredLabels().String()), 1, shard.OpReadWrite)
if err != nil {
// This can only fail in case we ask for more owners than the
// available peers. This will never happen, but in any case we fall
Expand All @@ -77,10 +77,15 @@ func (t Target) Labels() labels.Labels {
return lset
}

func (t Target) NonMetaLabels() labels.Labels {
// FilteredLabels drops the label "instance" and the labels starting by MetaLabelPrefix.
// The "instance" label is set by default to the host of the collector in exporters. If the
// collectors are not running on the same host, they will have different default values.
// If the targets have don't have the same set of labels between the collectors in the cluster,
// they won't be able to correctly distribute the workload because they won't compute the same hashes.
func (t Target) FilteredLabels() labels.Labels {
var lset labels.Labels
for k, v := range t {
if !strings.HasPrefix(k, model.MetaLabelPrefix) {
if k != "instance" && !strings.HasPrefix(k, model.MetaLabelPrefix) {
lset = append(lset, labels.Label{Name: k, Value: v})
}
}
Expand Down
18 changes: 18 additions & 0 deletions internal/component/discovery/discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package discovery

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestFilteredLabels(t *testing.T) {
target := Target{
"instance": "instanceTest",
"__meta_test": "metaTest",
"job": "jobTest",
}
labels := target.FilteredLabels()
require.Equal(t, labels.Len(), 1)
require.True(t, labels.Has("job"))
}
Loading