From 9adcba19bdc8ba76f64a630bab346da06616f4ba Mon Sep 17 00:00:00 2001 From: Jiacheng Xu Date: Tue, 21 Dec 2021 18:58:20 +0800 Subject: [PATCH 1/2] Add length and limit to labelNameTooLongError and labelValueTooLongError Signed-off-by: Jiacheng Xu --- pkg/util/validation/errors.go | 30 ++++++++++++++++++++-------- pkg/util/validation/validate.go | 4 ++-- pkg/util/validation/validate_test.go | 4 ++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/pkg/util/validation/errors.go b/pkg/util/validation/errors.go index 4decb435a72..efe738d8fbb 100644 --- a/pkg/util/validation/errors.go +++ b/pkg/util/validation/errors.go @@ -27,29 +27,43 @@ func (e *genericValidationError) Error() string { return fmt.Sprintf(e.message, e.cause, formatLabelSet(e.series)) } -func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string) ValidationError { - return &genericValidationError{ - message: "label name too long: %.200q metric %.200q", - cause: labelName, - series: series, +// labelNameTooLongError is a customized ValidationError, in that the cause and the series are +// formatted in different order in Error. +type labelNameTooLongError struct { + labelName string + series []cortexpb.LabelAdapter + limit int +} + +func (e *labelNameTooLongError) Error() string { + return fmt.Sprintf("label name too long for metric (actual: %d, limit: %d) metric: %.200q label name: %.200q", len(e.labelName), e.limit, formatLabelSet(e.series), e.labelName) +} + +func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string, limit int) ValidationError { + return &labelNameTooLongError{ + labelName: labelName, + series: series, + limit: limit, } } // labelValueTooLongError is a customized ValidationError, in that the cause and the series are -// are formatted in different order in Error. +// formatted in different order in Error. type labelValueTooLongError struct { labelValue string series []cortexpb.LabelAdapter + limit int } func (e *labelValueTooLongError) Error() string { - return fmt.Sprintf("label value too long for metric: %.200q label value: %.200q", formatLabelSet(e.series), e.labelValue) + return fmt.Sprintf("label value too long for metric (actual: %d, limit: %d) metric: %.200q label value: %.200q", len(e.labelValue), e.limit, formatLabelSet(e.series), e.labelValue) } -func newLabelValueTooLongError(series []cortexpb.LabelAdapter, labelValue string) ValidationError { +func newLabelValueTooLongError(series []cortexpb.LabelAdapter, labelValue string, limit int) ValidationError { return &labelValueTooLongError{ labelValue: labelValue, series: series, + limit: limit, } } diff --git a/pkg/util/validation/validate.go b/pkg/util/validation/validate.go index e6cb39777ab..d88bebe74bd 100644 --- a/pkg/util/validation/validate.go +++ b/pkg/util/validation/validate.go @@ -201,10 +201,10 @@ func ValidateLabels(cfg LabelValidationConfig, userID string, ls []cortexpb.Labe return newInvalidLabelError(ls, l.Name) } else if len(l.Name) > maxLabelNameLength { DiscardedSamples.WithLabelValues(labelNameTooLong, userID).Inc() - return newLabelNameTooLongError(ls, l.Name) + return newLabelNameTooLongError(ls, l.Name, maxLabelNameLength) } else if len(l.Value) > maxLabelValueLength { DiscardedSamples.WithLabelValues(labelValueTooLong, userID).Inc() - return newLabelValueTooLongError(ls, l.Value) + return newLabelValueTooLongError(ls, l.Value, maxLabelValueLength) } else if cmp := strings.Compare(lastLabelName, l.Name); cmp >= 0 { if cmp == 0 { DiscardedSamples.WithLabelValues(duplicateLabelNames, userID).Inc() diff --git a/pkg/util/validation/validate_test.go b/pkg/util/validation/validate_test.go index b3e096ca164..00e0944e095 100644 --- a/pkg/util/validation/validate_test.go +++ b/pkg/util/validation/validate_test.go @@ -95,7 +95,7 @@ func TestValidateLabels(t *testing.T) { newLabelNameTooLongError([]cortexpb.LabelAdapter{ {Name: model.MetricNameLabel, Value: "badLabelName"}, {Name: "this_is_a_really_really_long_name_that_should_cause_an_error", Value: "test_value_please_ignore"}, - }, "this_is_a_really_really_long_name_that_should_cause_an_error"), + }, "this_is_a_really_really_long_name_that_should_cause_an_error", cfg.maxLabelNameLength), }, { map[model.LabelName]model.LabelValue{model.MetricNameLabel: "badLabelValue", "much_shorter_name": "test_value_please_ignore_no_really_nothing_to_see_here"}, @@ -103,7 +103,7 @@ func TestValidateLabels(t *testing.T) { newLabelValueTooLongError([]cortexpb.LabelAdapter{ {Name: model.MetricNameLabel, Value: "badLabelValue"}, {Name: "much_shorter_name", Value: "test_value_please_ignore_no_really_nothing_to_see_here"}, - }, "test_value_please_ignore_no_really_nothing_to_see_here"), + }, "test_value_please_ignore_no_really_nothing_to_see_here", cfg.maxLabelValueLength), }, { map[model.LabelName]model.LabelValue{model.MetricNameLabel: "foo", "bar": "baz", "blip": "blop"}, From 2ea6c3a13c98a5185cc8545413a9d018750e2204 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu Date: Tue, 21 Dec 2021 19:01:52 +0800 Subject: [PATCH 2/2] Add CHANGELOG.md Signed-off-by: Jiacheng Xu --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54d2b9fce83..060b7f57d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [ENHANCEMENT] Query Frontend: Add setting `-frontend.forward-headers-list` in frontend to configure the set of headers from the requests to be forwarded to downstream requests. #4486 * [ENHANCEMENT] Blocks storage: Add `-blocks-storage.azure.http.*`, `-alertmanager-storage.azure.http.*`, and `-ruler-storage.azure.http.*` to configure the Azure storage client. #4581 * [ENHANCEMENT] Optimise memberlist receive path when used as a backing store for rings with a large number of members. #4601 +* [ENHANCEMENT] Add length and limit to labelNameTooLongError and labelValueTooLongError #4595 * [BUGFIX] AlertManager: remove stale template files. #4495 * [BUGFIX] Distributor: fix bug in query-exemplar where some results would get dropped. #4582 * [BUGFIX] Update Thanos dependency: compactor tracing support, azure blocks storage memory fix. #4585