Commit 41e8876
committed
Fix HdrHistogram range issues by computing ratio from min/max expected values
The fix computes an appropriate `highestToLowestValueRatio` from the configured `minimumExpectedValue` and `maximumExpectedValue` in `DistributionStatisticConfig`, allowing HdrHistogram to cover the expected range without constant resizing.
## Problem
The current implementation hardcodes a ratio of 2:
```java
new DoubleHistogram(percentilePrecision(distributionStatisticConfig));
// This constructor internally calls: this(2, numberOfSignificantValueDigits, ...)
```
This means the histogram can only handle values that differ by a factor of 2 (e.g., 1ms to 2ms). When recording typical operation times that span microseconds to seconds, HdrHistogram constantly attempts to resize, frequently throwing `ArrayIndexOutOfBoundsException` during the resize operations.
In production environments, this can result in hundreds of thousands of exceptions being thrown, causing significant performance overhead.
## Solution
This PR modifies `TimeWindowPercentileHistogram` to:
1. **Compute the ratio from configuration**: When `minimumExpectedValue` and `maximumExpectedValue` are provided, calculate an appropriate ratio
2. **Use the computed ratio**: Pass this ratio to HdrHistogram constructors instead of relying on the default
3. **Maintain backward compatibility**: When min/max values are not configured, fall back to the original behavior (ratio=2)
## Performance Impact
### Before
- Frequent `ArrayIndexOutOfBoundsException` as values exceed the narrow range
- Initial `HdrHistogram#counts` has length of 127
### After
- No exceptions when values are within the configured min/max range
- Initial `HdrHistogram#counts` has length of 272 when using defaults (1ms - 30s)
This last bullet could be a problem and require a different solution.
## Issues
- Fixes #4327
Signed-off-by: Knut Wannheden <[email protected]>1 parent 79e3c5a commit 41e8876
File tree
1 file changed
+29
-3
lines changed- micrometer-core/src/main/java/io/micrometer/core/instrument/distribution
1 file changed
+29
-3
lines changedLines changed: 29 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| |||
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
83 | | - | |
| 86 | + | |
| 87 | + | |
84 | 88 | | |
85 | 89 | | |
86 | 90 | | |
| |||
100 | 104 | | |
101 | 105 | | |
102 | 106 | | |
103 | | - | |
| 107 | + | |
| 108 | + | |
104 | 109 | | |
105 | 110 | | |
106 | 111 | | |
| |||
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
146 | 172 | | |
147 | 173 | | |
148 | 174 | | |
| |||
0 commit comments