Skip to content

Commit 10b5d99

Browse files
authored
Fix: Address Remaining Stringlabels Issues Before Enabling (#4810)
* Address compilation errors There were some further compilation errors I didn't catch that were causing failures in the pipeline of the PR where stringlabels were enabled * Use StableHash() for backwards compatability * Ensure that loki.source.kubernetes log tailer computes stable hash for targets * Ensure that pyroscope.scrape computes stable hash for targets
1 parent 78c73d6 commit 10b5d99

File tree

7 files changed

+17
-20
lines changed

7 files changed

+17
-20
lines changed

internal/component/discovery/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (t Target) groupLabelsHash() uint64 {
308308
return t.hashLabelsInOrder(labelsInOrder)
309309
}
310310

311-
// NOTE 1: This function is copied from Prometheus codebase and adapted to work correctly with Alloy types.
311+
// NOTE 1: This function is copied from Prometheus codebase (labels.StableHash()) and adapted to work correctly with Alloy types.
312312
// NOTE 2: It is important to keep the hashing function consistent between Alloy versions in order to have
313313
//
314314
// smooth rollouts without duplicated or missing data. There are tests to verify this behaviour. Do not change it.

internal/component/discovery/target_builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func TestTargetBuilder(t *testing.T) {
267267
assert.Equal(t, expected, actual)
268268
}
269269

270-
assert.Equal(t, actual.PromLabels().Hash(), actual.HashLabelsWithPredicate(func(key string) bool {
270+
assert.Equal(t, modellabels.StableHash(actual.PromLabels()), actual.HashLabelsWithPredicate(func(key string) bool {
271271
return true
272272
}), "prometheus and alloy target hash codes should match")
273273
}

internal/component/discovery/target_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/grafana/ckit/shard"
1212
"github.com/prometheus/common/model"
1313
"github.com/prometheus/prometheus/discovery/targetgroup"
14+
"github.com/prometheus/prometheus/model/labels"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617

@@ -835,7 +836,7 @@ func TestHashLargeLabelSets(t *testing.T) {
835836
require.Equal(t, uint64(expectedAllLabelsHash), target.HashLabelsWithPredicate(func(key string) bool {
836837
return true
837838
}))
838-
require.Equal(t, uint64(expectedAllLabelsHash), target.PromLabels().Hash()) // check it matches Prometheus algo
839+
require.Equal(t, uint64(expectedAllLabelsHash), labels.StableHash(target.PromLabels())) // check it matches Prometheus algo
839840

840841
var allNonMetaLabels []string
841842
target.ForEachLabel(func(k string, v string) bool {

internal/component/loki/source/kubernetes/kubetail/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func NewTarget(origLabels labels.Labels, lset labels.Labels) *Target {
7676
// Precompute the hash of the target from the public labels and the ID of the
7777
// target.
7878
hasher := xxhash.New()
79-
_, _ = fmt.Fprintf(hasher, "%016d", publicLabels.Hash())
79+
_, _ = fmt.Fprintf(hasher, "%016d", labels.StableHash(publicLabels))
8080
_, _ = fmt.Fprint(hasher, id)
8181
_, _ = fmt.Fprint(hasher, uid)
8282
hash := hasher.Sum64()

internal/component/pyroscope/scrape/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewTarget(lbls labels.Labels, params url.Values) *Target {
9797
url := urlFromTarget(lbls, params)
9898

9999
h := fnv.New64a()
100-
_, _ = h.Write([]byte(strconv.FormatUint(publicLabels.Hash(), 16)))
100+
_, _ = h.Write([]byte(strconv.FormatUint(labels.StableHash(publicLabels), 16)))
101101
_, _ = h.Write([]byte(url))
102102

103103
return &Target{

internal/service/labelstore/service_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ func TestReplaceLocalMappings(t *testing.T) {
100100

101101
func TestReplaceWithoutAddingLocalMapping(t *testing.T) {
102102
mapping := New(log.NewNopLogger(), prometheus.DefaultRegisterer)
103-
l := labels.Labels{}
104-
l = append(l, labels.Label{
105-
Name: "__name__",
106-
Value: "test",
107-
})
103+
l := labels.FromStrings("__name__", "test")
108104

109105
globalID := mapping.GetOrAddGlobalRefID(l)
110106
localRefID := uint64(2)

internal/static/metrics/wal/wal_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,8 @@ func buildMixedTypeSeries() seriesList {
820820
name: "float_series",
821821
samples: []sample{{1, 10.0, nil, nil}, {10, 100.0, nil, nil}},
822822
exemplars: []exemplar.Exemplar{
823-
{Labels: labels.Labels{{Name: "foobar", Value: "barfoo"}}, Value: float64(10.0), Ts: int64(1), HasTs: true},
824-
{Labels: labels.Labels{{Name: "lorem", Value: "ipsum"}}, Value: float64(100.0), Ts: int64(10), HasTs: true},
823+
{Labels: labels.FromStrings("foobar", "barfoo"), Value: float64(10.0), Ts: int64(1), HasTs: true},
824+
{Labels: labels.FromStrings("lorem", "ipsum"), Value: float64(100.0), Ts: int64(10), HasTs: true},
825825
},
826826
},
827827
// From now on I put -1 into the float to be different from default 0.
@@ -831,32 +831,32 @@ func buildMixedTypeSeries() seriesList {
831831
name: "integer histogram",
832832
samples: []sample{{2, -1, tsdbutil.GenerateTestHistogram(1), nil}, {20, -1, tsdbutil.GenerateTestHistogram(100), nil}},
833833
exemplars: []exemplar.Exemplar{
834-
{Labels: labels.Labels{{Name: "foobar", Value: "barfoo"}}, Value: float64(10.0), Ts: int64(2), HasTs: true},
835-
{Labels: labels.Labels{{Name: "lorem", Value: "ipsum"}}, Value: float64(100.0), Ts: int64(20), HasTs: true},
834+
{Labels: labels.FromStrings("foobar", "barfoo"), Value: float64(10.0), Ts: int64(2), HasTs: true},
835+
{Labels: labels.FromStrings("lorem", "ipsum"), Value: float64(100.0), Ts: int64(20), HasTs: true},
836836
},
837837
},
838838
{
839839
name: "float histogram",
840840
samples: []sample{{3, -1, nil, tsdbutil.GenerateTestFloatHistogram(1)}, {30, -1, nil, tsdbutil.GenerateTestFloatHistogram(100)}},
841841
exemplars: []exemplar.Exemplar{
842-
{Labels: labels.Labels{{Name: "foobar", Value: "barfoo"}}, Value: float64(10.0), Ts: int64(3), HasTs: true},
843-
{Labels: labels.Labels{{Name: "lorem", Value: "ipsum"}}, Value: float64(100.0), Ts: int64(30), HasTs: true},
842+
{Labels: labels.FromStrings("foobar", "barfoo"), Value: float64(10.0), Ts: int64(3), HasTs: true},
843+
{Labels: labels.FromStrings("lorem", "ipsum"), Value: float64(100.0), Ts: int64(30), HasTs: true},
844844
},
845845
},
846846
{
847847
name: "integer NHCB",
848848
samples: []sample{{2, -1, tsdbutil.GenerateTestCustomBucketsHistogram(1), nil}, {20, -1, tsdbutil.GenerateTestCustomBucketsHistogram(100), nil}},
849849
exemplars: []exemplar.Exemplar{
850-
{Labels: labels.Labels{{Name: "foobar", Value: "barfoo"}}, Value: float64(10.0), Ts: int64(2), HasTs: true},
851-
{Labels: labels.Labels{{Name: "lorem", Value: "ipsum"}}, Value: float64(100.0), Ts: int64(20), HasTs: true},
850+
{Labels: labels.FromStrings("foobar", "barfoo"), Value: float64(10.0), Ts: int64(2), HasTs: true},
851+
{Labels: labels.FromStrings("lorem", "ipsum"), Value: float64(100.0), Ts: int64(20), HasTs: true},
852852
},
853853
},
854854
{
855855
name: "float NHCB",
856856
samples: []sample{{3, -1, nil, tsdbutil.GenerateTestCustomBucketsFloatHistogram(1)}, {30, -1, nil, tsdbutil.GenerateTestCustomBucketsFloatHistogram(100)}},
857857
exemplars: []exemplar.Exemplar{
858-
{Labels: labels.Labels{{Name: "foobar", Value: "barfoo"}}, Value: float64(10.0), Ts: int64(3), HasTs: true},
859-
{Labels: labels.Labels{{Name: "lorem", Value: "ipsum"}}, Value: float64(100.0), Ts: int64(30), HasTs: true},
858+
{Labels: labels.FromStrings("foobar", "barfoo"), Value: float64(10.0), Ts: int64(3), HasTs: true},
859+
{Labels: labels.FromStrings("lorem", "ipsum"), Value: float64(100.0), Ts: int64(30), HasTs: true},
860860
},
861861
},
862862
}

0 commit comments

Comments
 (0)