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

Make label name character set validation optional #2835

Merged
Merged
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
10 changes: 4 additions & 6 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ type Config struct {
// for testing
ingesterClientFactory ring_client.PoolFactory `yaml:"-"`

// when true the distributor does not validate labels at ingest time, Cortex doesn't directly use
// when true the distributor does not validate the label name, Cortex doesn't directly use
// this (and should never use it) but this feature is used by other projects built on top of it
SkipLabelValidation bool `yaml:"-"`
SkipLabelNameValidation bool `yaml:"-"`
}

// RegisterFlags adds the flags required to config this to the given FlagSet
Expand Down Expand Up @@ -336,10 +336,8 @@ func (d *Distributor) checkSample(ctx context.Context, userID, cluster, replica
// Returns the validated series with it's labels/samples, and any error.
func (d *Distributor) validateSeries(ts ingester_client.PreallocTimeseries, userID string) (client.PreallocTimeseries, error) {
labelsHistogram.Observe(float64(len(ts.Labels)))
if !d.cfg.SkipLabelValidation {
if err := validation.ValidateLabels(d.limits, userID, ts.Labels); err != nil {
return emptyPreallocSeries, err
}
if err := validation.ValidateLabels(d.limits, userID, ts.Labels, d.cfg.SkipLabelNameValidation); err != nil {
return emptyPreallocSeries, err
}

metricName, _ := extract.MetricNameFromLabelAdapters(ts.Labels)
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/validation/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ type LabelValidationConfig interface {
}

// ValidateLabels returns an err if the labels are invalid.
func ValidateLabels(cfg LabelValidationConfig, userID string, ls []client.LabelAdapter) error {
metricName, err := extract.MetricNameFromLabelAdapters(ls)
func ValidateLabels(cfg LabelValidationConfig, userID string, ls []client.LabelAdapter, skipLabelNameValidation bool) error {
if cfg.EnforceMetricName(userID) {
metricName, err := extract.MetricNameFromLabelAdapters(ls)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this (moving it inside the branch)!

if err != nil {
DiscardedSamples.WithLabelValues(missingMetricName, userID).Inc()
return httpgrpc.Errorf(http.StatusBadRequest, errMissingMetricName)
Expand All @@ -139,7 +139,7 @@ func ValidateLabels(cfg LabelValidationConfig, userID string, ls []client.LabelA
var errTemplate string
var reason string
var cause interface{}
if !model.LabelName(l.Name).IsValid() {
if !skipLabelNameValidation && !model.LabelName(l.Name).IsValid() {
reason = invalidLabel
errTemplate = errInvalidLabel
cause = l.Name
Expand Down
25 changes: 19 additions & 6 deletions pkg/util/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,53 @@ func TestValidateLabels(t *testing.T) {
cfg.enforceMetricName = true

for _, c := range []struct {
metric model.Metric
err error
metric model.Metric
skipLabelNameValidation bool
err error
}{
{
map[model.LabelName]model.LabelValue{},
false,
httpgrpc.Errorf(http.StatusBadRequest, errMissingMetricName),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: " "},
false,
httpgrpc.Errorf(http.StatusBadRequest, errInvalidMetricName, " "),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "valid", "foo ": "bar"},
false,
httpgrpc.Errorf(http.StatusBadRequest, errInvalidLabel, "foo ", `valid{foo ="bar"}`),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "valid"},
false,
nil,
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "badLabelName", "this_is_a_really_really_long_name_that_should_cause_an_error": "test_value_please_ignore"},
false,
httpgrpc.Errorf(http.StatusBadRequest, errLabelNameTooLong, "this_is_a_really_really_long_name_that_should_cause_an_error", `badLabelName{this_is_a_really_really_long_name_that_should_cause_an_error="test_value_please_ignore"}`),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "badLabelValue", "much_shorter_name": "test_value_please_ignore_no_really_nothing_to_see_here"},
false,
httpgrpc.Errorf(http.StatusBadRequest, errLabelValueTooLong, "test_value_please_ignore_no_really_nothing_to_see_here", `badLabelValue{much_shorter_name="test_value_please_ignore_no_really_nothing_to_see_here"}`),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "foo", "bar": "baz", "blip": "blop"},
false,
httpgrpc.Errorf(http.StatusBadRequest, errTooManyLabels, `foo{bar="baz", blip="blop"}`, 3, 2),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "foo", "invalid%label&name": "bar"},
true,
nil,
},
} {

err := ValidateLabels(cfg, userID, client.FromMetricsToLabelAdapters(c.metric))
err := ValidateLabels(cfg, userID, client.FromMetricsToLabelAdapters(c.metric), c.skipLabelNameValidation)
assert.Equal(t, c.err, err, "wrong error")
}
}
Expand Down Expand Up @@ -151,7 +164,7 @@ func TestValidateLabelOrder(t *testing.T) {
{Name: model.MetricNameLabel, Value: "m"},
{Name: "b", Value: "b"},
{Name: "a", Value: "a"},
})
}, false)
assert.Equal(t, httpgrpc.Errorf(http.StatusBadRequest, errLabelsNotSorted, "a", `m{b="b", a="a"}`), err)
}

Expand All @@ -166,13 +179,13 @@ func TestValidateLabelDuplication(t *testing.T) {
err := ValidateLabels(cfg, userID, []client.LabelAdapter{
{Name: model.MetricNameLabel, Value: "a"},
{Name: model.MetricNameLabel, Value: "b"},
})
}, false)
assert.Equal(t, httpgrpc.Errorf(http.StatusBadRequest, errDuplicateLabelName, "__name__", `a{__name__="b"}`), err)

err = ValidateLabels(cfg, userID, []client.LabelAdapter{
{Name: model.MetricNameLabel, Value: "a"},
{Name: "a", Value: "a"},
{Name: "a", Value: "a"},
})
}, false)
assert.Equal(t, httpgrpc.Errorf(http.StatusBadRequest, errDuplicateLabelName, "a", `a{a="a", a="a"}`), err)
}