Skip to content

Commit 1fe5228

Browse files
committed
fix: handle incompatible schemas in histogram aggregation
When downsampling histograms, if the schema type changes between custom bucket histograms (schema < 0) and exponential bucket histograms (schema >= 0), the Add operation would fail because these histogram types cannot be added together. This fix checks for incompatible schemas before attempting to add histograms. When incompatible schemas are detected, the aggregation is reset to the new histogram, similar to how counter resets are handled. Fixes panic when mixing custom bucket and exponential bucket histograms during downsampling. Signed-off-by: Br1an67 <932039080@qq.com>
1 parent 7bbca4d commit 1fe5228

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

pkg/compact/downsample/downsample.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,14 @@ func (h *histogramAggregator) add(s sample) {
367367
}
368368

369369
if h.total > 0 {
370-
if fh.CounterResetHint != histogram.GaugeType && oFh.DetectReset(h.previous) {
370+
// Check if schemas are incompatible (custom bucket vs exponential bucket).
371+
// Custom bucket histograms (schema < 0) and exponential bucket histograms (schema >= 0)
372+
// cannot be added together. Treat as a counter reset.
373+
incompatibleSchemas := (h.counter.Schema < 0) != (fh.Schema < 0)
374+
if incompatibleSchemas {
375+
// Schema type changed, treat as counter reset.
376+
h.counter = fh.Copy()
377+
} else if fh.CounterResetHint != histogram.GaugeType && oFh.DetectReset(h.previous) {
371378
// Counter reset, correct the value.
372379
mustHistogramOp(h.counter.Add(fh))
373380
} else if oFh.Schema < 0 {
@@ -391,7 +398,15 @@ func (h *histogramAggregator) add(s sample) {
391398
if h.sum == nil {
392399
h.sum = fh.Copy()
393400
} else {
394-
mustHistogramOp(h.sum.Add(fh))
401+
// Check if schemas are incompatible (custom bucket vs exponential bucket).
402+
// Custom bucket histograms (schema < 0) and exponential bucket histograms (schema >= 0)
403+
// cannot be added together. Reset the sum to the new histogram.
404+
incompatibleSchemas := (h.sum.Schema < 0) != (fh.Schema < 0)
405+
if incompatibleSchemas {
406+
h.sum = fh.Copy()
407+
} else {
408+
mustHistogramOp(h.sum.Add(fh))
409+
}
395410
}
396411

397412
// This needs to be h gauge histogram, otherwise reset detection will be triggered

0 commit comments

Comments
 (0)