Skip to content

Commit 6b93903

Browse files
authored
Add length and limit to error messages when label names and values are too long (#4595)
* Add length and limit to labelNameTooLongError and labelValueTooLongError Signed-off-by: Jiacheng Xu <[email protected]> * Add CHANGELOG.md Signed-off-by: Jiacheng Xu <[email protected]>
1 parent 64befca commit 6b93903

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [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
2020
* [ENHANCEMENT] Blocks storage: Add `-blocks-storage.azure.http.*`, `-alertmanager-storage.azure.http.*`, and `-ruler-storage.azure.http.*` to configure the Azure storage client. #4581
2121
* [ENHANCEMENT] Optimise memberlist receive path when used as a backing store for rings with a large number of members. #4601
22+
* [ENHANCEMENT] Add length and limit to labelNameTooLongError and labelValueTooLongError #4595
2223
* [BUGFIX] AlertManager: remove stale template files. #4495
2324
* [BUGFIX] Distributor: fix bug in query-exemplar where some results would get dropped. #4582
2425
* [BUGFIX] Update Thanos dependency: compactor tracing support, azure blocks storage memory fix. #4585

pkg/util/validation/errors.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,43 @@ func (e *genericValidationError) Error() string {
2727
return fmt.Sprintf(e.message, e.cause, formatLabelSet(e.series))
2828
}
2929

30-
func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string) ValidationError {
31-
return &genericValidationError{
32-
message: "label name too long: %.200q metric %.200q",
33-
cause: labelName,
34-
series: series,
30+
// labelNameTooLongError is a customized ValidationError, in that the cause and the series are
31+
// formatted in different order in Error.
32+
type labelNameTooLongError struct {
33+
labelName string
34+
series []cortexpb.LabelAdapter
35+
limit int
36+
}
37+
38+
func (e *labelNameTooLongError) Error() string {
39+
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)
40+
}
41+
42+
func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string, limit int) ValidationError {
43+
return &labelNameTooLongError{
44+
labelName: labelName,
45+
series: series,
46+
limit: limit,
3547
}
3648
}
3749

3850
// labelValueTooLongError is a customized ValidationError, in that the cause and the series are
39-
// are formatted in different order in Error.
51+
// formatted in different order in Error.
4052
type labelValueTooLongError struct {
4153
labelValue string
4254
series []cortexpb.LabelAdapter
55+
limit int
4356
}
4457

4558
func (e *labelValueTooLongError) Error() string {
46-
return fmt.Sprintf("label value too long for metric: %.200q label value: %.200q", formatLabelSet(e.series), e.labelValue)
59+
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)
4760
}
4861

49-
func newLabelValueTooLongError(series []cortexpb.LabelAdapter, labelValue string) ValidationError {
62+
func newLabelValueTooLongError(series []cortexpb.LabelAdapter, labelValue string, limit int) ValidationError {
5063
return &labelValueTooLongError{
5164
labelValue: labelValue,
5265
series: series,
66+
limit: limit,
5367
}
5468
}
5569

pkg/util/validation/validate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ func ValidateLabels(cfg LabelValidationConfig, userID string, ls []cortexpb.Labe
201201
return newInvalidLabelError(ls, l.Name)
202202
} else if len(l.Name) > maxLabelNameLength {
203203
DiscardedSamples.WithLabelValues(labelNameTooLong, userID).Inc()
204-
return newLabelNameTooLongError(ls, l.Name)
204+
return newLabelNameTooLongError(ls, l.Name, maxLabelNameLength)
205205
} else if len(l.Value) > maxLabelValueLength {
206206
DiscardedSamples.WithLabelValues(labelValueTooLong, userID).Inc()
207-
return newLabelValueTooLongError(ls, l.Value)
207+
return newLabelValueTooLongError(ls, l.Value, maxLabelValueLength)
208208
} else if cmp := strings.Compare(lastLabelName, l.Name); cmp >= 0 {
209209
if cmp == 0 {
210210
DiscardedSamples.WithLabelValues(duplicateLabelNames, userID).Inc()

pkg/util/validation/validate_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func TestValidateLabels(t *testing.T) {
9595
newLabelNameTooLongError([]cortexpb.LabelAdapter{
9696
{Name: model.MetricNameLabel, Value: "badLabelName"},
9797
{Name: "this_is_a_really_really_long_name_that_should_cause_an_error", Value: "test_value_please_ignore"},
98-
}, "this_is_a_really_really_long_name_that_should_cause_an_error"),
98+
}, "this_is_a_really_really_long_name_that_should_cause_an_error", cfg.maxLabelNameLength),
9999
},
100100
{
101101
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "badLabelValue", "much_shorter_name": "test_value_please_ignore_no_really_nothing_to_see_here"},
102102
false,
103103
newLabelValueTooLongError([]cortexpb.LabelAdapter{
104104
{Name: model.MetricNameLabel, Value: "badLabelValue"},
105105
{Name: "much_shorter_name", Value: "test_value_please_ignore_no_really_nothing_to_see_here"},
106-
}, "test_value_please_ignore_no_really_nothing_to_see_here"),
106+
}, "test_value_please_ignore_no_really_nothing_to_see_here", cfg.maxLabelValueLength),
107107
},
108108
{
109109
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "foo", "bar": "baz", "blip": "blop"},

0 commit comments

Comments
 (0)