assignExemplarToQuantile determines which quantile (if any) an exemplar should be assigned to. Returns the quantile index, or -1 if the exemplar doesn't fit any quantile reasonably well. This uses a simple closest-match strategy with reasonable bucket validation.
(exemplarValue float64, quantileValues []float64, buckets []HistogramBucket)
| 2011 | // Returns the quantile index, or -1 if the exemplar doesn't fit any quantile reasonably well. |
| 2012 | // This uses a simple closest-match strategy with reasonable bucket validation. |
| 2013 | func (h *HistogramAggregator) assignExemplarToQuantile(exemplarValue float64, quantileValues []float64, buckets []HistogramBucket) int { |
| 2014 | if len(quantileValues) == 0 || len(buckets) == 0 { |
| 2015 | return -1 |
| 2016 | } |
| 2017 | |
| 2018 | bestIdx := -1 |
| 2019 | bestDiff := math.Inf(1) |
| 2020 | |
| 2021 | for i, quantileValue := range quantileValues { |
| 2022 | // Find the closest quantile value for better visual alignment |
| 2023 | diff := math.Abs(exemplarValue - quantileValue) |
| 2024 | if diff < bestDiff { |
| 2025 | bestDiff = diff |
| 2026 | bestIdx = i |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | return bestIdx |
| 2031 | } |
| 2032 | |
| 2033 | func (h *HistogramAggregator) Length() int { |
| 2034 | return len(h.ss) * len(h.qs) |