Update package storage/remote tests for new labels.Labels type

Use ScratchBuilder to create labels.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
pull/11717/head
Bryan Boreham 2022-03-09 22:35:14 +00:00
parent abd9909595
commit 047585360b
5 changed files with 92 additions and 78 deletions

View File

@ -71,86 +71,86 @@ var writeRequestFixture = &prompb.WriteRequest{
func TestValidateLabelsAndMetricName(t *testing.T) { func TestValidateLabelsAndMetricName(t *testing.T) {
tests := []struct { tests := []struct {
input labels.Labels input []prompb.Label
expectedErr string expectedErr string
description string description string
}{ }{
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"labelName", "labelValue", {Name: "labelName", Value: "labelValue"},
), },
expectedErr: "", expectedErr: "",
description: "regular labels", description: "regular labels",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"_labelName", "labelValue", {Name: "_labelName", Value: "labelValue"},
), },
expectedErr: "", expectedErr: "",
description: "label name with _", description: "label name with _",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"@labelName", "labelValue", {Name: "@labelName", Value: "labelValue"},
), },
expectedErr: "invalid label name: @labelName", expectedErr: "invalid label name: @labelName",
description: "label name with @", description: "label name with @",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"123labelName", "labelValue", {Name: "123labelName", Value: "labelValue"},
), },
expectedErr: "invalid label name: 123labelName", expectedErr: "invalid label name: 123labelName",
description: "label name starts with numbers", description: "label name starts with numbers",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"", "labelValue", {Name: "", Value: "labelValue"},
), },
expectedErr: "invalid label name: ", expectedErr: "invalid label name: ",
description: "label name is empty string", description: "label name is empty string",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name", {Name: "__name__", Value: "name"},
"labelName", string([]byte{0xff}), {Name: "labelName", Value: string([]byte{0xff})},
), },
expectedErr: "invalid label value: " + string([]byte{0xff}), expectedErr: "invalid label value: " + string([]byte{0xff}),
description: "label value is an invalid UTF-8 value", description: "label value is an invalid UTF-8 value",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "@invalid_name", {Name: "__name__", Value: "@invalid_name"},
), },
expectedErr: "invalid metric name: @invalid_name", expectedErr: "invalid metric name: @invalid_name",
description: "metric name starts with @", description: "metric name starts with @",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"__name__", "name1", {Name: "__name__", Value: "name1"},
"__name__", "name2", {Name: "__name__", Value: "name2"},
), },
expectedErr: "duplicate label with name: __name__", expectedErr: "duplicate label with name: __name__",
description: "duplicate label names", description: "duplicate label names",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"label1", "name", {Name: "label1", Value: "name"},
"label2", "name", {Name: "label2", Value: "name"},
), },
expectedErr: "", expectedErr: "",
description: "duplicate label values", description: "duplicate label values",
}, },
{ {
input: labels.FromStrings( input: []prompb.Label{
"", "name", {Name: "", Value: "name"},
"label2", "name", {Name: "label2", Value: "name"},
), },
expectedErr: "invalid label name: ", expectedErr: "invalid label name: ",
description: "don't report as duplicate label name", description: "don't report as duplicate label name",
}, },
@ -197,8 +197,7 @@ func TestConcreteSeriesClonesLabels(t *testing.T) {
gotLabels := cs.Labels() gotLabels := cs.Labels()
require.Equal(t, lbls, gotLabels) require.Equal(t, lbls, gotLabels)
gotLabels[0].Value = "foo" gotLabels.CopyFrom(labels.FromStrings("a", "foo", "c", "foo"))
gotLabels[1].Value = "bar"
gotLabels = cs.Labels() gotLabels = cs.Labels()
require.Equal(t, lbls, gotLabels) require.Equal(t, lbls, gotLabels)

View File

@ -161,7 +161,7 @@ func TestMetadataDelivery(t *testing.T) {
mcfg := config.DefaultMetadataConfig mcfg := config.DefaultMetadataConfig
metrics := newQueueManagerMetrics(nil, "", "") metrics := newQueueManagerMetrics(nil, "", "")
m := NewQueueManager(metrics, nil, nil, nil, dir, newEWMARate(ewmaWeight, shardUpdateDuration), cfg, mcfg, nil, nil, c, defaultFlushDeadline, newPool(), newHighestTimestampMetric(), nil, false, false) m := NewQueueManager(metrics, nil, nil, nil, dir, newEWMARate(ewmaWeight, shardUpdateDuration), cfg, mcfg, labels.EmptyLabels(), nil, c, defaultFlushDeadline, newPool(), newHighestTimestampMetric(), nil, false, false)
m.Start() m.Start()
defer m.Stop() defer m.Stop()
@ -539,6 +539,7 @@ func TestShouldReshard(t *testing.T) {
func createTimeseries(numSamples, numSeries int, extraLabels ...labels.Label) ([]record.RefSample, []record.RefSeries) { func createTimeseries(numSamples, numSeries int, extraLabels ...labels.Label) ([]record.RefSample, []record.RefSeries) {
samples := make([]record.RefSample, 0, numSamples) samples := make([]record.RefSample, 0, numSamples)
series := make([]record.RefSeries, 0, numSeries) series := make([]record.RefSeries, 0, numSeries)
b := labels.ScratchBuilder{}
for i := 0; i < numSeries; i++ { for i := 0; i < numSeries; i++ {
name := fmt.Sprintf("test_metric_%d", i) name := fmt.Sprintf("test_metric_%d", i)
for j := 0; j < numSamples; j++ { for j := 0; j < numSamples; j++ {
@ -548,9 +549,16 @@ func createTimeseries(numSamples, numSeries int, extraLabels ...labels.Label) ([
V: float64(i), V: float64(i),
}) })
} }
// Create Labels that is name of series plus any extra labels supplied.
b.Reset()
b.Add(labels.MetricName, name)
for _, l := range extraLabels {
b.Add(l.Name, l.Value)
}
b.Sort()
series = append(series, record.RefSeries{ series = append(series, record.RefSeries{
Ref: chunks.HeadSeriesRef(i), Ref: chunks.HeadSeriesRef(i),
Labels: append(labels.Labels{{Name: "__name__", Value: name}}, extraLabels...), Labels: b.Labels(),
}) })
} }
return samples, series return samples, series
@ -603,7 +611,7 @@ func createHistograms(numSamples, numSeries int) ([]record.RefHistogramSample, [
} }
series = append(series, record.RefSeries{ series = append(series, record.RefSeries{
Ref: chunks.HeadSeriesRef(i), Ref: chunks.HeadSeriesRef(i),
Labels: labels.Labels{{Name: "__name__", Value: name}}, Labels: labels.FromStrings("__name__", name),
}) })
} }
return histograms, series return histograms, series
@ -815,7 +823,7 @@ func BenchmarkSampleSend(b *testing.B) {
const numSeries = 10000 const numSeries = 10000
// Extra labels to make a more realistic workload - taken from Kubernetes' embedded cAdvisor metrics. // Extra labels to make a more realistic workload - taken from Kubernetes' embedded cAdvisor metrics.
extraLabels := labels.Labels{ extraLabels := []labels.Label{
{Name: "kubernetes_io_arch", Value: "amd64"}, {Name: "kubernetes_io_arch", Value: "amd64"},
{Name: "kubernetes_io_instance_type", Value: "c3.somesize"}, {Name: "kubernetes_io_instance_type", Value: "c3.somesize"},
{Name: "kubernetes_io_os", Value: "linux"}, {Name: "kubernetes_io_os", Value: "linux"},
@ -902,56 +910,63 @@ func BenchmarkStartup(b *testing.B) {
func TestProcessExternalLabels(t *testing.T) { func TestProcessExternalLabels(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
labels labels.Labels labels labels.Labels
externalLabels labels.Labels externalLabels []labels.Label
expected labels.Labels expected labels.Labels
}{ }{
// Test adding labels at the end. // Test adding labels at the end.
{ {
labels: labels.Labels{{Name: "a", Value: "b"}}, labels: labels.FromStrings("a", "b"),
externalLabels: labels.Labels{{Name: "c", Value: "d"}}, externalLabels: []labels.Label{{Name: "c", Value: "d"}},
expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}}, expected: labels.FromStrings("a", "b", "c", "d"),
}, },
// Test adding labels at the beginning. // Test adding labels at the beginning.
{ {
labels: labels.Labels{{Name: "c", Value: "d"}}, labels: labels.FromStrings("c", "d"),
externalLabels: labels.Labels{{Name: "a", Value: "b"}}, externalLabels: []labels.Label{{Name: "a", Value: "b"}},
expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}}, expected: labels.FromStrings("a", "b", "c", "d"),
}, },
// Test we don't override existing labels. // Test we don't override existing labels.
{ {
labels: labels.Labels{{Name: "a", Value: "b"}}, labels: labels.FromStrings("a", "b"),
externalLabels: labels.Labels{{Name: "a", Value: "c"}}, externalLabels: []labels.Label{{Name: "a", Value: "c"}},
expected: labels.Labels{{Name: "a", Value: "b"}}, expected: labels.FromStrings("a", "b"),
}, },
// Test empty externalLabels. // Test empty externalLabels.
{ {
labels: labels.Labels{{Name: "a", Value: "b"}}, labels: labels.FromStrings("a", "b"),
externalLabels: labels.Labels{}, externalLabels: []labels.Label{},
expected: labels.Labels{{Name: "a", Value: "b"}}, expected: labels.FromStrings("a", "b"),
}, },
// Test empty labels. // Test empty labels.
{ {
labels: labels.Labels{}, labels: labels.EmptyLabels(),
externalLabels: labels.Labels{{Name: "a", Value: "b"}}, externalLabels: []labels.Label{{Name: "a", Value: "b"}},
expected: labels.Labels{{Name: "a", Value: "b"}}, expected: labels.FromStrings("a", "b"),
}, },
// Test labels is longer than externalLabels. // Test labels is longer than externalLabels.
{ {
labels: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}}, labels: labels.FromStrings("a", "b", "c", "d"),
externalLabels: labels.Labels{{Name: "e", Value: "f"}}, externalLabels: []labels.Label{{Name: "e", Value: "f"}},
expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}, {Name: "e", Value: "f"}}, expected: labels.FromStrings("a", "b", "c", "d", "e", "f"),
}, },
// Test externalLabels is longer than labels. // Test externalLabels is longer than labels.
{ {
labels: labels.Labels{{Name: "c", Value: "d"}}, labels: labels.FromStrings("c", "d"),
externalLabels: labels.Labels{{Name: "a", Value: "b"}, {Name: "e", Value: "f"}}, externalLabels: []labels.Label{{Name: "a", Value: "b"}, {Name: "e", Value: "f"}},
expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}, {Name: "e", Value: "f"}}, expected: labels.FromStrings("a", "b", "c", "d", "e", "f"),
},
// Adding with and without clashing labels.
{
labels: labels.FromStrings("a", "b", "c", "d"),
externalLabels: []labels.Label{{Name: "a", Value: "xxx"}, {Name: "c", Value: "yyy"}, {Name: "e", Value: "f"}},
expected: labels.FromStrings("a", "b", "c", "d", "e", "f"),
}, },
} { } {
require.Equal(t, tc.expected, processExternalLabels(tc.labels, tc.externalLabels)) require.Equal(t, tc.expected, processExternalLabels(tc.labels, tc.externalLabels))

View File

@ -110,7 +110,7 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
el labels.Labels el labels.Labels
inMatchers []*labels.Matcher inMatchers []*labels.Matcher
outMatchers []*labels.Matcher outMatchers []*labels.Matcher
added labels.Labels added []string
}{ }{
{ {
inMatchers: []*labels.Matcher{ inMatchers: []*labels.Matcher{
@ -119,7 +119,7 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
outMatchers: []*labels.Matcher{ outMatchers: []*labels.Matcher{
labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"), labels.MustNewMatcher(labels.MatchEqual, "job", "api-server"),
}, },
added: labels.Labels{}, added: []string{},
}, },
{ {
el: labels.FromStrings("dc", "berlin-01", "region", "europe"), el: labels.FromStrings("dc", "berlin-01", "region", "europe"),
@ -131,7 +131,7 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
labels.MustNewMatcher(labels.MatchEqual, "region", "europe"), labels.MustNewMatcher(labels.MatchEqual, "region", "europe"),
labels.MustNewMatcher(labels.MatchEqual, "dc", "berlin-01"), labels.MustNewMatcher(labels.MatchEqual, "dc", "berlin-01"),
}, },
added: labels.FromStrings("dc", "berlin-01", "region", "europe"), added: []string{"dc", "region"},
}, },
{ {
el: labels.FromStrings("dc", "berlin-01", "region", "europe"), el: labels.FromStrings("dc", "berlin-01", "region", "europe"),
@ -144,7 +144,7 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
labels.MustNewMatcher(labels.MatchEqual, "region", "europe"), labels.MustNewMatcher(labels.MatchEqual, "region", "europe"),
labels.MustNewMatcher(labels.MatchEqual, "dc", "munich-02"), labels.MustNewMatcher(labels.MatchEqual, "dc", "munich-02"),
}, },
added: labels.FromStrings("region", "europe"), added: []string{"region"},
}, },
} }
@ -163,12 +163,12 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) {
func TestSeriesSetFilter(t *testing.T) { func TestSeriesSetFilter(t *testing.T) {
tests := []struct { tests := []struct {
in *prompb.QueryResult in *prompb.QueryResult
toRemove labels.Labels toRemove []string
expected *prompb.QueryResult expected *prompb.QueryResult
}{ }{
{ {
toRemove: labels.Labels{{Name: "foo", Value: "bar"}}, toRemove: []string{"foo"},
in: &prompb.QueryResult{ in: &prompb.QueryResult{
Timeseries: []*prompb.TimeSeries{ Timeseries: []*prompb.TimeSeries{
{Labels: labelsToLabelsProto(labels.FromStrings("foo", "bar", "a", "b"), nil), Samples: []prompb.Sample{}}, {Labels: labelsToLabelsProto(labels.FromStrings("foo", "bar", "a", "b"), nil), Samples: []prompb.Sample{}},

View File

@ -91,7 +91,7 @@ func TestFilterExternalLabels(t *testing.T) {
require.NoError(t, s.ApplyConfig(conf)) require.NoError(t, s.ApplyConfig(conf))
require.Equal(t, 1, len(s.queryables)) require.Equal(t, 1, len(s.queryables))
require.Equal(t, 1, len(s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels)) require.Equal(t, 1, s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels.Len())
err := s.Close() err := s.Close()
require.NoError(t, err) require.NoError(t, err)
@ -118,7 +118,7 @@ func TestIgnoreExternalLabels(t *testing.T) {
require.NoError(t, s.ApplyConfig(conf)) require.NoError(t, s.ApplyConfig(conf))
require.Equal(t, 1, len(s.queryables)) require.Equal(t, 1, len(s.queryables))
require.Equal(t, 0, len(s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels)) require.Equal(t, 0, s.queryables[0].(*sampleAndChunkQueryableClient).externalLabels.Len())
err := s.Close() err := s.Close()
require.NoError(t, err) require.NoError(t, err)

View File

@ -228,14 +228,14 @@ func TestUpdateExternalLabels(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, s.ApplyConfig(conf)) require.NoError(t, s.ApplyConfig(conf))
require.Equal(t, 1, len(s.queues)) require.Equal(t, 1, len(s.queues))
require.Equal(t, labels.Labels(nil), s.queues[hash].externalLabels) require.Equal(t, 0, len(s.queues[hash].externalLabels))
conf.GlobalConfig.ExternalLabels = externalLabels conf.GlobalConfig.ExternalLabels = externalLabels
hash, err = toHash(conf.RemoteWriteConfigs[0]) hash, err = toHash(conf.RemoteWriteConfigs[0])
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, s.ApplyConfig(conf)) require.NoError(t, s.ApplyConfig(conf))
require.Equal(t, 1, len(s.queues)) require.Equal(t, 1, len(s.queues))
require.Equal(t, externalLabels, s.queues[hash].externalLabels) require.Equal(t, []labels.Label{{Name: "external", Value: "true"}}, s.queues[hash].externalLabels)
err = s.Close() err = s.Close()
require.NoError(t, err) require.NoError(t, err)