Check for duplicate label names in remote read (#5829)

* Check for duplicate label names in remote read

Also add test to confirm that #5731 is fixed

* Use subtests in TestValidateLabelsAndMetricName

* Really check that expectedErr matches err

Signed-off-by: Vadym Martsynovskyy <vmartsynovskyy@gmail.com>
This commit is contained in:
Vadym Martsynovskyy 2019-08-07 08:13:10 -07:00 committed by Brian Brazil
parent 2184b79763
commit 8318aa2d5d
2 changed files with 86 additions and 13 deletions

View File

@ -277,9 +277,10 @@ func (c *concreteSeriesIterator) Err() error {
return nil return nil
} }
// validateLabelsAndMetricName validates the label names/values and metric names returned from remote read. // validateLabelsAndMetricName validates the label names/values and metric names returned from remote read,
// also making sure that there are no labels with duplicate names
func validateLabelsAndMetricName(ls labels.Labels) error { func validateLabelsAndMetricName(ls labels.Labels) error {
for _, l := range ls { for i, l := range ls {
if l.Name == labels.MetricName && !model.IsValidMetricName(model.LabelValue(l.Value)) { if l.Name == labels.MetricName && !model.IsValidMetricName(model.LabelValue(l.Value)) {
return errors.Errorf("invalid metric name: %v", l.Value) return errors.Errorf("invalid metric name: %v", l.Value)
} }
@ -289,6 +290,9 @@ func validateLabelsAndMetricName(ls labels.Labels) error {
if !model.LabelValue(l.Value).IsValid() { if !model.LabelValue(l.Value).IsValid() {
return errors.Errorf("invalid label value: %v", l.Value) return errors.Errorf("invalid label value: %v", l.Value)
} }
if i > 0 && l.Name == ls[i-1].Name {
return errors.Errorf("duplicate label with name: %v", l.Name)
}
} }
return nil return nil
} }

View File

@ -14,6 +14,7 @@
package remote package remote
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -21,6 +22,7 @@ import (
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestValidateLabelsAndMetricName(t *testing.T) { func TestValidateLabelsAndMetricName(t *testing.T) {
@ -28,6 +30,7 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
input labels.Labels input labels.Labels
expectedErr string expectedErr string
shouldPass bool shouldPass bool
description string
}{ }{
{ {
input: labels.FromStrings( input: labels.FromStrings(
@ -36,6 +39,7 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
), ),
expectedErr: "", expectedErr: "",
shouldPass: true, shouldPass: true,
description: "regular labels",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
@ -44,57 +48,96 @@ func TestValidateLabelsAndMetricName(t *testing.T) {
), ),
expectedErr: "", expectedErr: "",
shouldPass: true, shouldPass: true,
description: "label name with _",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
"__name__", "name", "__name__", "name",
"@labelName", "labelValue", "@labelName", "labelValue",
), ),
expectedErr: "Invalid label name: @labelName", expectedErr: "invalid label name: @labelName",
shouldPass: false, shouldPass: false,
description: "label name with @",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
"__name__", "name", "__name__", "name",
"123labelName", "labelValue", "123labelName", "labelValue",
), ),
expectedErr: "Invalid label name: 123labelName", expectedErr: "invalid label name: 123labelName",
shouldPass: false, shouldPass: false,
description: "label name starts with numbers",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
"__name__", "name", "__name__", "name",
"", "labelValue", "", "labelValue",
), ),
expectedErr: "Invalid label name: ", expectedErr: "invalid label name: ",
shouldPass: false, shouldPass: false,
description: "label name is empty string",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
"__name__", "name", "__name__", "name",
"labelName", string([]byte{0xff}), "labelName", string([]byte{0xff}),
), ),
expectedErr: "Invalid label value: " + string([]byte{0xff}), expectedErr: "invalid label value: " + string([]byte{0xff}),
shouldPass: false, shouldPass: false,
description: "label value is an invalid UTF-8 value",
}, },
{ {
input: labels.FromStrings( input: labels.FromStrings(
"__name__", "@invalid_name", "__name__", "@invalid_name",
), ),
expectedErr: "Invalid metric name: @invalid_name", expectedErr: "invalid metric name: @invalid_name",
shouldPass: false, shouldPass: false,
description: "metric name starts with @",
},
{
input: labels.FromStrings(
"__name__", "name1",
"__name__", "name2",
),
expectedErr: "duplicate label with name: __name__",
shouldPass: false,
description: "duplicate label names",
},
{
input: labels.FromStrings(
"label1", "name",
"label2", "name",
),
expectedErr: "",
shouldPass: true,
description: "duplicate label values",
},
{
input: labels.FromStrings(
"", "name",
"label2", "name",
),
expectedErr: "invalid label name: ",
shouldPass: false,
description: "don't report as duplicate label name",
}, },
} }
for _, test := range tests { for _, test := range tests {
err := validateLabelsAndMetricName(test.input) t.Run(test.description, func(t *testing.T) {
if test.shouldPass != (err == nil) { err := validateLabelsAndMetricName(test.input)
if test.shouldPass { if err == nil {
t.Fatalf("Test should pass, got unexpected error: %v", err) if !test.shouldPass {
t.Fatalf("Test should fail, but passed instead.")
}
} else { } else {
t.Fatalf("Test should fail, unexpected error, got: %v, expected: %v", err, test.expectedErr) if test.shouldPass {
t.Fatalf("Test should pass, got unexpected error: %v", err)
} else if err.Error() != test.expectedErr {
t.Fatalf("Test should fail with: %s got unexpected error instead: %v", test.expectedErr, err)
}
} }
} })
} }
} }
@ -145,3 +188,29 @@ func TestConcreteSeriesClonesLabels(t *testing.T) {
gotLabels = cs.Labels() gotLabels = cs.Labels()
require.Equal(t, lbls, gotLabels) require.Equal(t, lbls, gotLabels)
} }
func TestFromQueryResultWithDuplicates(t *testing.T) {
ts1 := prompb.TimeSeries{
Labels: []prompb.Label{
prompb.Label{Name: "foo", Value: "bar"},
prompb.Label{Name: "foo", Value: "def"},
},
Samples: []prompb.Sample{
prompb.Sample{Value: 0.0, Timestamp: 0},
},
}
res := prompb.QueryResult{
Timeseries: []*prompb.TimeSeries{
&ts1,
},
}
series := FromQueryResult(&res)
errSeries, isErrSeriesSet := series.(errSeriesSet)
testutil.Assert(t, isErrSeriesSet, "Expected resulting series to be an errSeriesSet")
errMessage := errSeries.Err().Error()
testutil.Assert(t, errMessage == "duplicate label with name: foo", fmt.Sprintf("Expected error to be from duplicate label, but got: %s", errMessage))
}