|
8 | 8 | "github.com/prometheus/client_golang/prometheus"
|
9 | 9 | "github.com/prometheus/client_golang/prometheus/testutil"
|
10 | 10 | "github.com/prometheus/common/model"
|
| 11 | + "github.com/prometheus/prometheus/model/labels" |
| 12 | + "github.com/prometheus/prometheus/tsdb/tsdbutil" |
11 | 13 | "github.com/stretchr/testify/assert"
|
12 | 14 | "github.com/stretchr/testify/require"
|
13 | 15 | "github.com/weaveworks/common/httpgrpc"
|
@@ -291,3 +293,110 @@ func TestValidateLabelDuplication(t *testing.T) {
|
291 | 293 | }, "a")
|
292 | 294 | assert.Equal(t, expected, actual)
|
293 | 295 | }
|
| 296 | + |
| 297 | +func TestValidateNativeHistogram(t *testing.T) { |
| 298 | + userID := "fake" |
| 299 | + lbls := cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("foo", "bar")) |
| 300 | + |
| 301 | + // Test histogram has 4 positive buckets and 4 negative buckets so 8 in total. Schema set to 1. |
| 302 | + h := tsdbutil.GenerateTestHistogram(0) |
| 303 | + fh := tsdbutil.GenerateTestFloatHistogram(0) |
| 304 | + |
| 305 | + histogramWithSchemaMin := tsdbutil.GenerateTestHistogram(0) |
| 306 | + histogramWithSchemaMin.Schema = cortexpb.ExponentialSchemaMin |
| 307 | + floatHistogramWithSchemaMin := tsdbutil.GenerateTestFloatHistogram(0) |
| 308 | + floatHistogramWithSchemaMin.Schema = cortexpb.ExponentialSchemaMin |
| 309 | + for _, tc := range []struct { |
| 310 | + name string |
| 311 | + bucketLimit int |
| 312 | + histogram cortexpb.Histogram |
| 313 | + expectedHistogram cortexpb.Histogram |
| 314 | + expectedErr error |
| 315 | + }{ |
| 316 | + { |
| 317 | + name: "no limit, histogram", |
| 318 | + histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 319 | + expectedHistogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 320 | + }, |
| 321 | + { |
| 322 | + name: "no limit, float histogram", |
| 323 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 324 | + expectedHistogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 325 | + }, |
| 326 | + { |
| 327 | + name: "within limit, histogram", |
| 328 | + bucketLimit: 8, |
| 329 | + histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 330 | + expectedHistogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 331 | + }, |
| 332 | + { |
| 333 | + name: "within limit, float histogram", |
| 334 | + bucketLimit: 8, |
| 335 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 336 | + expectedHistogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 337 | + }, |
| 338 | + { |
| 339 | + name: "exceed limit and reduce resolution for 1 level, histogram", |
| 340 | + bucketLimit: 6, |
| 341 | + histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 342 | + expectedHistogram: cortexpb.HistogramToHistogramProto(0, h.Copy().ReduceResolution(0)), |
| 343 | + }, |
| 344 | + { |
| 345 | + name: "exceed limit and reduce resolution for 1 level, float histogram", |
| 346 | + bucketLimit: 6, |
| 347 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 348 | + expectedHistogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy().ReduceResolution(0)), |
| 349 | + }, |
| 350 | + { |
| 351 | + name: "exceed limit and reduce resolution for 2 levels, histogram", |
| 352 | + bucketLimit: 4, |
| 353 | + histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 354 | + expectedHistogram: cortexpb.HistogramToHistogramProto(0, h.Copy().ReduceResolution(-1)), |
| 355 | + }, |
| 356 | + { |
| 357 | + name: "exceed limit and reduce resolution for 2 levels, float histogram", |
| 358 | + bucketLimit: 4, |
| 359 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 360 | + expectedHistogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy().ReduceResolution(-1)), |
| 361 | + }, |
| 362 | + { |
| 363 | + name: "exceed limit but cannot reduce resolution further, histogram", |
| 364 | + bucketLimit: 1, |
| 365 | + histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()), |
| 366 | + expectedErr: newHistogramBucketLimitExceededError(lbls, 1), |
| 367 | + }, |
| 368 | + { |
| 369 | + name: "exceed limit but cannot reduce resolution further, float histogram", |
| 370 | + bucketLimit: 1, |
| 371 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()), |
| 372 | + expectedErr: newHistogramBucketLimitExceededError(lbls, 1), |
| 373 | + }, |
| 374 | + { |
| 375 | + name: "exceed limit but cannot reduce resolution further with min schema, histogram", |
| 376 | + bucketLimit: 4, |
| 377 | + histogram: cortexpb.HistogramToHistogramProto(0, histogramWithSchemaMin.Copy()), |
| 378 | + expectedErr: newHistogramBucketLimitExceededError(lbls, 4), |
| 379 | + }, |
| 380 | + { |
| 381 | + name: "exceed limit but cannot reduce resolution further with min schema, float histogram", |
| 382 | + bucketLimit: 4, |
| 383 | + histogram: cortexpb.FloatHistogramToHistogramProto(0, floatHistogramWithSchemaMin.Copy()), |
| 384 | + expectedErr: newHistogramBucketLimitExceededError(lbls, 4), |
| 385 | + }, |
| 386 | + } { |
| 387 | + t.Run(tc.name, func(t *testing.T) { |
| 388 | + reg := prometheus.NewRegistry() |
| 389 | + validateMetrics := NewValidateMetrics(reg) |
| 390 | + limits := new(Limits) |
| 391 | + limits.MaxNativeHistogramBuckets = tc.bucketLimit |
| 392 | + actualHistogram, actualErr := ValidateNativeHistogram(validateMetrics, limits, userID, lbls, tc.histogram) |
| 393 | + if tc.expectedErr != nil { |
| 394 | + require.Equal(t, tc.expectedErr, actualErr) |
| 395 | + require.Equal(t, float64(1), testutil.ToFloat64(validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketsExceeded, userID))) |
| 396 | + } else { |
| 397 | + require.NoError(t, actualErr) |
| 398 | + require.Equal(t, tc.expectedHistogram, actualHistogram) |
| 399 | + } |
| 400 | + }) |
| 401 | + } |
| 402 | +} |
0 commit comments