Commit b0a5e13
authored
fix: correct native histogram bucket merge on scale-down (#2274)
## Summary
Fix native histogram bucket remapping when the bucket width is doubled
during schema scale-down.
The previous merge formula used:
```
(keys[i] + 1) / 2
```
That works for positive bucket indexes, but not for negative even
indexes. Java integer division truncates toward zero, so negative even
buckets were merged into the bucket immediately to the right of the
correct target.
This matches the behaviour described in #2270 and aligns the merge
formula with client_golang:
```
(keys[i] > 0 ? keys[i] + 1 : keys[i]) / 2
```
## Why
When `doubleBucketWidth()` runs, counts from some negative native
histogram buckets could be shifted into neighbouring buckets. That can
make Prometheus interpret the reshuffled bucket counters as resets,
causing spikes in queries such as:
```
histogram_count(rate(my_native_histogram[1m]))
```
## Tests
Added a focused regression test for negative, zero, and positive bucket
index remapping during `doubleBucketWidth(Map)`.
Verified locally:
```
./mvnw test -pl prometheus-metrics-core -am \
-Dtest=HistogramTest#testDoubleBucketWidthMergesNegativeIndexesCorrectly \
-Dcoverage.skip=true \
-Dcheckstyle.skip=true \
-Dsurefire.failIfNoSpecifiedTests=false
```
Also verified:
```bash
./mvnw install -DskipTests -Dcoverage.skip=true
```
Fixes #2270
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>1 parent 4080581 commit b0a5e13
2 files changed
Lines changed: 34 additions & 1 deletion
File tree
- prometheus-metrics-core/src
- main/java/io/prometheus/metrics/core/metrics
- test/java/io/prometheus/metrics/core/metrics
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
614 | 614 | | |
615 | 615 | | |
616 | 616 | | |
617 | | - | |
| 617 | + | |
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
| |||
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| 44 | + | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
| |||
836 | 839 | | |
837 | 840 | | |
838 | 841 | | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
839 | 872 | | |
840 | 873 | | |
841 | 874 | | |
| |||
0 commit comments