Skip to content

Commit 7a83bb5

Browse files
committed
Simplified ValidationError
Signed-off-by: Marco Pracucci <[email protected]>
1 parent e581fed commit 7a83bb5

File tree

3 files changed

+11
-42
lines changed

3 files changed

+11
-42
lines changed

pkg/distributor/distributor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
549549
// Errors in validation are considered non-fatal, as one series in a request may contain
550550
// invalid data but all the remaining series could be perfectly valid.
551551
if validationErr != nil && firstPartialErr == nil {
552-
firstPartialErr = validationErr.ToHTTPGRPCError()
552+
firstPartialErr = httpgrpc.Errorf(http.StatusBadRequest, validationErr.Error())
553553
}
554554

555555
// validateSeries would have returned an emptyPreallocSeries if there were no valid samples.

pkg/util/validation/errors.go

+8-37
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@ package validation
22

33
import (
44
"fmt"
5-
"net/http"
65
"strings"
76

87
"github.com/prometheus/common/model"
9-
"github.com/weaveworks/common/httpgrpc"
108

119
"github.com/cortexproject/cortex/pkg/cortexpb"
1210
)
1311

14-
// ValidationError is an error returned by series validation. It provides a function to convert
15-
// the error into an HTTPGRPC error.
12+
// ValidationError is an error returned by series validation.
1613
//
1714
// nolint:golint ignore stutter warning
18-
type ValidationError interface {
19-
error
20-
21-
// ToHTTPGRPCError returns the httpgrpc version of the error.
22-
ToHTTPGRPCError() error
23-
}
15+
type ValidationError error
2416

2517
// genericValidationError is a basic implementation of ValidationError which can be used when the
2618
// error format only contains the cause and the series.
@@ -30,12 +22,8 @@ type genericValidationError struct {
3022
series []cortexpb.LabelAdapter
3123
}
3224

33-
func (e *genericValidationError) ToHTTPGRPCError() error {
34-
return httpgrpc.Errorf(http.StatusBadRequest, e.message, e.cause, formatLabelSet(e.series))
35-
}
36-
3725
func (e *genericValidationError) Error() string {
38-
return e.ToHTTPGRPCError().Error()
26+
return fmt.Sprintf(e.message, e.cause, formatLabelSet(e.series))
3927
}
4028

4129
func newLabelNameTooLongError(series []cortexpb.LabelAdapter, labelName string) ValidationError {
@@ -90,29 +78,20 @@ func newTooManyLabelsError(series []cortexpb.LabelAdapter, limit int) Validation
9078
}
9179
}
9280

93-
func (e *tooManyLabelsError) ToHTTPGRPCError() error {
94-
return httpgrpc.Errorf(
95-
http.StatusBadRequest,
81+
func (e *tooManyLabelsError) Error() string {
82+
return fmt.Sprintf(
9683
"series has too many labels (actual: %d, limit: %d) series: '%s'",
9784
len(e.series), e.limit, cortexpb.FromLabelAdaptersToMetric(e.series).String())
9885
}
9986

100-
func (e *tooManyLabelsError) Error() string {
101-
return e.ToHTTPGRPCError().Error()
102-
}
103-
10487
type noMetricNameError struct{}
10588

10689
func newNoMetricNameError() ValidationError {
10790
return &noMetricNameError{}
10891
}
10992

110-
func (e *noMetricNameError) ToHTTPGRPCError() error {
111-
return httpgrpc.Errorf(http.StatusBadRequest, "sample missing metric name")
112-
}
113-
11493
func (e *noMetricNameError) Error() string {
115-
return e.ToHTTPGRPCError().Error()
94+
return "sample missing metric name"
11695
}
11796

11897
type invalidMetricNameError struct {
@@ -125,12 +104,8 @@ func newInvalidMetricNameError(metricName string) ValidationError {
125104
}
126105
}
127106

128-
func (e *invalidMetricNameError) ToHTTPGRPCError() error {
129-
return httpgrpc.Errorf(http.StatusBadRequest, "sample invalid metric name: %.200q", e.metricName)
130-
}
131-
132107
func (e *invalidMetricNameError) Error() string {
133-
return e.ToHTTPGRPCError().Error()
108+
return fmt.Sprintf("sample invalid metric name: %.200q", e.metricName)
134109
}
135110

136111
// sampleValidationError is a ValidationError implementation suitable for sample validation errors.
@@ -140,12 +115,8 @@ type sampleValidationError struct {
140115
timestamp int64
141116
}
142117

143-
func (e *sampleValidationError) ToHTTPGRPCError() error {
144-
return httpgrpc.Errorf(http.StatusBadRequest, e.message, e.timestamp, e.metricName)
145-
}
146-
147118
func (e *sampleValidationError) Error() string {
148-
return e.ToHTTPGRPCError().Error()
119+
return fmt.Sprintf(e.message, e.timestamp, e.metricName)
149120
}
150121

151122
func newSampleTimestampTooOldError(metricName string, timestamp int64) ValidationError {

pkg/util/validation/validate.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ type SampleValidationConfig interface {
8282
CreationGracePeriod(userID string) time.Duration
8383
}
8484

85-
// ValidateSample returns an err if the sample is invalid. The returned ValidationError
86-
// provides a function to convert it to an HTTPGRPC error.
85+
// ValidateSample returns an err if the sample is invalid.
8786
func ValidateSample(cfg SampleValidationConfig, userID string, metricName string, s cortexpb.Sample) ValidationError {
8887
if cfg.RejectOldSamples(userID) && model.Time(s.TimestampMs) < model.Now().Add(-cfg.RejectOldSamplesMaxAge(userID)) {
8988
DiscardedSamples.WithLabelValues(greaterThanMaxSampleAge, userID).Inc()
@@ -106,8 +105,7 @@ type LabelValidationConfig interface {
106105
MaxLabelValueLength(userID string) int
107106
}
108107

109-
// ValidateLabels returns an err if the labels are invalid. The returned ValidationError
110-
// provides a function to convert it to an HTTPGRPC error.
108+
// ValidateLabels returns an err if the labels are invalid.
111109
func ValidateLabels(cfg LabelValidationConfig, userID string, ls []cortexpb.LabelAdapter, skipLabelNameValidation bool) ValidationError {
112110
if cfg.EnforceMetricName(userID) {
113111
metricName, err := extract.MetricNameFromLabelAdapters(ls)

0 commit comments

Comments
 (0)