diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index b0e512fbc5..60d1575125 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -830,7 +830,7 @@ func (h *FloatHistogram) Validate() error { return errors.New("histogram with exponential schema must not have custom bounds") } default: - return fmt.Errorf("schema %d: %w", h.Schema, ErrHistogramsInvalidSchema) + return InvalidSchemaError(h.Schema) } err := checkHistogramBuckets(h.PositiveBuckets, &pCount, false) if err != nil { diff --git a/model/histogram/generic.go b/model/histogram/generic.go index cc1fbce685..1788da7fc1 100644 --- a/model/histogram/generic.go +++ b/model/histogram/generic.go @@ -42,6 +42,10 @@ var ( ErrHistogramsInvalidSchema = fmt.Errorf("histogram has an invalid schema, which must be between %d and %d for exponential buckets, or %d for custom buckets", ExponentialSchemaMin, ExponentialSchemaMax, CustomBucketsSchema) ) +func InvalidSchemaError(s int32) error { + return fmt.Errorf("%w, got schema %d", ErrHistogramsInvalidSchema, s) +} + func IsCustomBucketsSchema(s int32) bool { return s == CustomBucketsSchema } diff --git a/model/histogram/histogram.go b/model/histogram/histogram.go index 169be9a6ac..ca5260c75e 100644 --- a/model/histogram/histogram.go +++ b/model/histogram/histogram.go @@ -457,7 +457,7 @@ func (h *Histogram) Validate() error { return errors.New("histogram with exponential schema must not have custom bounds") } default: - return fmt.Errorf("schema %d: %w", h.Schema, ErrHistogramsInvalidSchema) + return InvalidSchemaError(h.Schema) } err := checkHistogramBuckets(h.PositiveBuckets, &pCount, true) if err != nil { diff --git a/model/histogram/histogram_test.go b/model/histogram/histogram_test.go index 35603bc01c..52a3392154 100644 --- a/model/histogram/histogram_test.go +++ b/model/histogram/histogram_test.go @@ -1569,13 +1569,13 @@ func TestHistogramValidation(t *testing.T) { h: &Histogram{ Schema: 10, }, - errMsg: `schema 10: histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets`, + errMsg: `histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets, got schema 10`, }, "schema too low": { h: &Histogram{ Schema: -10, }, - errMsg: `schema -10: histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets`, + errMsg: `histogram has an invalid schema, which must be between -4 and 8 for exponential buckets, or -53 for custom buckets, got schema -10`, }, } diff --git a/storage/remote/codec.go b/storage/remote/codec.go index 87b7ff2290..25e5ba04b6 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -475,7 +475,7 @@ func validateHistogramSchema(h *prompb.Histogram) error { if histogram.IsValidSchema(h.Schema) { return nil } - return return fmt.Errorf("%w, got schema %d", h.Schema, histogram.ErrHistogramsInvalidSchema) + return histogram.InvalidSchemaError(h.Schema) } func getHistogramValType(h *prompb.Histogram) chunkenc.ValueType { diff --git a/tsdb/chunkenc/float_histogram.go b/tsdb/chunkenc/float_histogram.go index 1626b45239..13faf9961a 100644 --- a/tsdb/chunkenc/float_histogram.go +++ b/tsdb/chunkenc/float_histogram.go @@ -956,7 +956,7 @@ func (it *floatHistogramIterator) Next() ValueType { } if !histogram.IsValidSchema(schema) { - it.err = fmt.Errorf("invalid histogram schema %d", schema) + it.err = histogram.InvalidSchemaError(schema) return ValNone } diff --git a/tsdb/chunkenc/float_histogram_test.go b/tsdb/chunkenc/float_histogram_test.go index be5a2673e5..a9813d2c64 100644 --- a/tsdb/chunkenc/float_histogram_test.go +++ b/tsdb/chunkenc/float_histogram_test.go @@ -1488,7 +1488,7 @@ func TestFloatHistogramIteratorFailIfSchemaInValid(t *testing.T) { it := c.Iterator(nil) require.Equal(t, ValNone, it.Next()) - require.EqualError(t, it.Err(), fmt.Sprintf("invalid histogram schema %d", schema)) + require.ErrorIs(t, it.Err(), histogram.ErrHistogramsInvalidSchema) }) } } diff --git a/tsdb/chunkenc/histogram.go b/tsdb/chunkenc/histogram.go index deb52d83a8..194b67962f 100644 --- a/tsdb/chunkenc/histogram.go +++ b/tsdb/chunkenc/histogram.go @@ -1079,7 +1079,7 @@ func (it *histogramIterator) Next() ValueType { } if !histogram.IsValidSchema(schema) { - it.err = fmt.Errorf("invalid histogram schema %d", schema) + it.err = histogram.InvalidSchemaError(schema) return ValNone } diff --git a/tsdb/chunkenc/histogram_test.go b/tsdb/chunkenc/histogram_test.go index d2b1f593a3..b191960d89 100644 --- a/tsdb/chunkenc/histogram_test.go +++ b/tsdb/chunkenc/histogram_test.go @@ -1844,7 +1844,7 @@ func TestHistogramIteratorFailIfSchemaInValid(t *testing.T) { it := c.Iterator(nil) require.Equal(t, ValNone, it.Next()) - require.EqualError(t, it.Err(), fmt.Sprintf("invalid histogram schema %d", schema)) + require.ErrorIs(t, it.Err(), histogram.ErrHistogramsInvalidSchema) }) } }