Skip to content

Commit 93585e6

Browse files
alanprotdanielblando
authored andcommitted
Create cortex_reduced_resolution_histogram_samples_total metric (cortexproject#6182)
* Create new metric to count how many native histrogram samples had the resolution reduced Signed-off-by: alanprot <alanprot@gmail.com> * changelog Signed-off-by: alanprot <alanprot@gmail.com> --------- Signed-off-by: alanprot <alanprot@gmail.com>
1 parent 053b5a1 commit 93585e6

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [ENHANCEMENT] Ruler: Add new ruler metric `cortex_ruler_rule_groups_in_store` that is the total rule groups per tenant in store, which can be used to compare with `cortex_prometheus_rule_group_rules` to count the number of rule groups that are not loaded by a ruler. #5869
66
* [ENHANCEMENT] Ruler: Add query statistics metrics when --ruler.query-stats-enabled=true. #6173
7+
* [ENHANCEMENT] Distributor: Add new `cortex_reduced_resolution_histogram_samples_total` metric to to track the number of histogram samples which resolution was reduced. #6182
78

89
## 1.18.0 2024-09-03
910

pkg/util/validation/validate.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ const (
7474
)
7575

7676
type ValidateMetrics struct {
77-
DiscardedSamples *prometheus.CounterVec
78-
DiscardedExemplars *prometheus.CounterVec
79-
DiscardedMetadata *prometheus.CounterVec
77+
DiscardedSamples *prometheus.CounterVec
78+
DiscardedExemplars *prometheus.CounterVec
79+
DiscardedMetadata *prometheus.CounterVec
80+
HistogramSamplesReducedResolution *prometheus.CounterVec
8081
}
8182

8283
func registerCollector(r prometheus.Registerer, c prometheus.Collector) {
@@ -111,10 +112,19 @@ func NewValidateMetrics(r prometheus.Registerer) *ValidateMetrics {
111112
[]string{discardReasonLabel, "user"},
112113
)
113114
registerCollector(r, discardedMetadata)
115+
histogramSamplesReducedResolution := prometheus.NewCounterVec(
116+
prometheus.CounterOpts{
117+
Name: "cortex_reduced_resolution_histogram_samples_total",
118+
Help: "The total number of histogram samples that had the resolution reduced.",
119+
},
120+
[]string{"user"},
121+
)
122+
registerCollector(r, histogramSamplesReducedResolution)
114123
m := &ValidateMetrics{
115-
DiscardedSamples: discardedSamples,
116-
DiscardedExemplars: discardedExemplars,
117-
DiscardedMetadata: discardedMetadata,
124+
DiscardedSamples: discardedSamples,
125+
DiscardedExemplars: discardedExemplars,
126+
DiscardedMetadata: discardedMetadata,
127+
HistogramSamplesReducedResolution: histogramSamplesReducedResolution,
118128
}
119129

120130
return m
@@ -286,13 +296,17 @@ func ValidateNativeHistogram(validateMetrics *ValidateMetrics, limits *Limits, u
286296
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
287297
}
288298
fh := cortexpb.FloatHistogramProtoToFloatHistogram(histogramSample)
299+
oBuckets := len(fh.PositiveBuckets) + len(fh.NegativeBuckets)
289300
for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > limits.MaxNativeHistogramBuckets {
290301
if fh.Schema <= histogram.ExponentialSchemaMin {
291302
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
292303
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
293304
}
294305
fh = fh.ReduceResolution(fh.Schema - 1)
295306
}
307+
if oBuckets != len(fh.PositiveBuckets)+len(fh.NegativeBuckets) {
308+
validateMetrics.HistogramSamplesReducedResolution.WithLabelValues(userID).Inc()
309+
}
296310
// If resolution reduced, convert new float histogram to protobuf type again.
297311
return cortexpb.FloatHistogramToHistogramProto(histogramSample.TimestampMs, fh), nil
298312
}
@@ -308,13 +322,17 @@ func ValidateNativeHistogram(validateMetrics *ValidateMetrics, limits *Limits, u
308322
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
309323
}
310324
h := cortexpb.HistogramProtoToHistogram(histogramSample)
325+
oBuckets := len(h.PositiveBuckets) + len(h.NegativeBuckets)
311326
for len(h.PositiveBuckets)+len(h.NegativeBuckets) > limits.MaxNativeHistogramBuckets {
312327
if h.Schema <= histogram.ExponentialSchemaMin {
313328
validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID).Inc()
314329
return cortexpb.Histogram{}, newHistogramBucketLimitExceededError(ls, limits.MaxNativeHistogramBuckets)
315330
}
316331
h = h.ReduceResolution(h.Schema - 1)
317332
}
333+
if oBuckets != len(h.PositiveBuckets)+len(h.NegativeBuckets) {
334+
validateMetrics.HistogramSamplesReducedResolution.WithLabelValues(userID).Inc()
335+
}
318336
// If resolution reduced, convert new histogram to protobuf type again.
319337
return cortexpb.HistogramToHistogramProto(histogramSample.TimestampMs, h), nil
320338
}
@@ -331,4 +349,7 @@ func DeletePerUserValidationMetrics(validateMetrics *ValidateMetrics, userID str
331349
if err := util.DeleteMatchingLabels(validateMetrics.DiscardedMetadata, filter); err != nil {
332350
level.Warn(log).Log("msg", "failed to remove cortex_discarded_metadata_total metric for user", "user", userID, "err", err)
333351
}
352+
if err := util.DeleteMatchingLabels(validateMetrics.HistogramSamplesReducedResolution, filter); err != nil {
353+
level.Warn(log).Log("msg", "failed to remove cortex_reduced_resolution_histogram_samples_total metric for user", "user", userID, "err", err)
354+
}
334355
}

pkg/util/validation/validate_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestValidateNativeHistogram(t *testing.T) {
310310
for _, tc := range []struct {
311311
name string
312312
bucketLimit int
313+
resolutionReduced bool
313314
histogram cortexpb.Histogram
314315
expectedHistogram cortexpb.Histogram
315316
expectedErr error
@@ -341,12 +342,14 @@ func TestValidateNativeHistogram(t *testing.T) {
341342
bucketLimit: 6,
342343
histogram: cortexpb.HistogramToHistogramProto(0, h.Copy()),
343344
expectedHistogram: cortexpb.HistogramToHistogramProto(0, h.Copy().ReduceResolution(0)),
345+
resolutionReduced: true,
344346
},
345347
{
346348
name: "exceed limit and reduce resolution for 1 level, float histogram",
347349
bucketLimit: 6,
348350
histogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy()),
349351
expectedHistogram: cortexpb.FloatHistogramToHistogramProto(0, fh.Copy().ReduceResolution(0)),
352+
resolutionReduced: true,
350353
},
351354
{
352355
name: "exceed limit and reduce resolution for 2 levels, histogram",
@@ -394,7 +397,13 @@ func TestValidateNativeHistogram(t *testing.T) {
394397
if tc.expectedErr != nil {
395398
require.Equal(t, tc.expectedErr, actualErr)
396399
require.Equal(t, float64(1), testutil.ToFloat64(validateMetrics.DiscardedSamples.WithLabelValues(nativeHistogramBucketCountLimitExceeded, userID)))
400+
// Should never increment if error was returned
401+
require.Equal(t, float64(0), testutil.ToFloat64(validateMetrics.HistogramSamplesReducedResolution.WithLabelValues(userID)))
402+
397403
} else {
404+
if tc.resolutionReduced {
405+
require.Equal(t, float64(1), testutil.ToFloat64(validateMetrics.HistogramSamplesReducedResolution.WithLabelValues(userID)))
406+
}
398407
require.NoError(t, actualErr)
399408
require.Equal(t, tc.expectedHistogram, actualHistogram)
400409
}

0 commit comments

Comments
 (0)