|
| 1 | +// Copyright 2020 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +package histogram |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + |
| 18 | + dto "github.com/prometheus/client_model/go" |
| 19 | + model "github.com/prometheus/prometheus/model/histogram" |
| 20 | +) |
| 21 | + |
| 22 | +type APIBucket[BC model.BucketCount] struct { |
| 23 | + Boundaries uint64 |
| 24 | + Lower, Upper float64 |
| 25 | + Count BC |
| 26 | +} |
| 27 | + |
| 28 | +func NewModelHistogram(ch *dto.Histogram) (*model.Histogram, *model.FloatHistogram) { |
| 29 | + if ch.GetSampleCountFloat() > 0 || ch.GetZeroCountFloat() > 0 { |
| 30 | + // It is a float histogram. |
| 31 | + fh := model.FloatHistogram{ |
| 32 | + Count: ch.GetSampleCountFloat(), |
| 33 | + Sum: ch.GetSampleSum(), |
| 34 | + ZeroThreshold: ch.GetZeroThreshold(), |
| 35 | + ZeroCount: ch.GetZeroCountFloat(), |
| 36 | + Schema: ch.GetSchema(), |
| 37 | + PositiveSpans: make([]model.Span, len(ch.GetPositiveSpan())), |
| 38 | + PositiveBuckets: ch.GetPositiveCount(), |
| 39 | + NegativeSpans: make([]model.Span, len(ch.GetNegativeSpan())), |
| 40 | + NegativeBuckets: ch.GetNegativeCount(), |
| 41 | + } |
| 42 | + for i, span := range ch.GetPositiveSpan() { |
| 43 | + fh.PositiveSpans[i].Offset = span.GetOffset() |
| 44 | + fh.PositiveSpans[i].Length = span.GetLength() |
| 45 | + } |
| 46 | + for i, span := range ch.GetNegativeSpan() { |
| 47 | + fh.NegativeSpans[i].Offset = span.GetOffset() |
| 48 | + fh.NegativeSpans[i].Length = span.GetLength() |
| 49 | + } |
| 50 | + return nil, &fh |
| 51 | + } |
| 52 | + h := model.Histogram{ |
| 53 | + Count: ch.GetSampleCount(), |
| 54 | + Sum: ch.GetSampleSum(), |
| 55 | + ZeroThreshold: ch.GetZeroThreshold(), |
| 56 | + ZeroCount: ch.GetZeroCount(), |
| 57 | + Schema: ch.GetSchema(), |
| 58 | + PositiveSpans: make([]model.Span, len(ch.GetPositiveSpan())), |
| 59 | + PositiveBuckets: ch.GetPositiveDelta(), |
| 60 | + NegativeSpans: make([]model.Span, len(ch.GetNegativeSpan())), |
| 61 | + NegativeBuckets: ch.GetNegativeDelta(), |
| 62 | + } |
| 63 | + for i, span := range ch.GetPositiveSpan() { |
| 64 | + h.PositiveSpans[i].Offset = span.GetOffset() |
| 65 | + h.PositiveSpans[i].Length = span.GetLength() |
| 66 | + } |
| 67 | + for i, span := range ch.GetNegativeSpan() { |
| 68 | + h.NegativeSpans[i].Offset = span.GetOffset() |
| 69 | + h.NegativeSpans[i].Length = span.GetLength() |
| 70 | + } |
| 71 | + return &h, nil |
| 72 | +} |
| 73 | + |
| 74 | +func BucketsAsJson[BC model.BucketCount](buckets []APIBucket[BC]) [][]interface{} { |
| 75 | + ret := make([][]interface{}, len(buckets)) |
| 76 | + for i, b := range buckets { |
| 77 | + ret[i] = []interface{}{b.Boundaries, fmt.Sprintf("%v", b.Lower), fmt.Sprintf("%v", b.Upper), fmt.Sprintf("%v", b.Count)} |
| 78 | + } |
| 79 | + return ret |
| 80 | +} |
| 81 | + |
| 82 | +func GetAPIBuckets(h *model.Histogram) []APIBucket[uint64] { |
| 83 | + var apiBuckets []APIBucket[uint64] |
| 84 | + var nBuckets []model.Bucket[uint64] |
| 85 | + for it := h.NegativeBucketIterator(); it.Next(); { |
| 86 | + bucket := it.At() |
| 87 | + if bucket.Count != 0 { |
| 88 | + nBuckets = append(nBuckets, it.At()) |
| 89 | + } |
| 90 | + } |
| 91 | + for i := len(nBuckets) - 1; i >= 0; i-- { |
| 92 | + apiBuckets = append(apiBuckets, makeBucket[uint64](nBuckets[i])) |
| 93 | + } |
| 94 | + |
| 95 | + if h.ZeroCount != 0 { |
| 96 | + apiBuckets = append(apiBuckets, makeBucket[uint64](h.ZeroBucket())) |
| 97 | + } |
| 98 | + |
| 99 | + for it := h.PositiveBucketIterator(); it.Next(); { |
| 100 | + bucket := it.At() |
| 101 | + if bucket.Count != 0 { |
| 102 | + apiBuckets = append(apiBuckets, makeBucket[uint64](bucket)) |
| 103 | + } |
| 104 | + } |
| 105 | + return apiBuckets |
| 106 | +} |
| 107 | + |
| 108 | +func GetAPIFloatBuckets(h *model.FloatHistogram) []APIBucket[float64] { |
| 109 | + var apiBuckets []APIBucket[float64] |
| 110 | + var nBuckets []model.Bucket[float64] |
| 111 | + for it := h.NegativeBucketIterator(); it.Next(); { |
| 112 | + bucket := it.At() |
| 113 | + if bucket.Count != 0 { |
| 114 | + nBuckets = append(nBuckets, it.At()) |
| 115 | + } |
| 116 | + } |
| 117 | + for i := len(nBuckets) - 1; i >= 0; i-- { |
| 118 | + apiBuckets = append(apiBuckets, makeBucket[float64](nBuckets[i])) |
| 119 | + } |
| 120 | + |
| 121 | + if h.ZeroCount != 0 { |
| 122 | + apiBuckets = append(apiBuckets, makeBucket[float64](h.ZeroBucket())) |
| 123 | + } |
| 124 | + |
| 125 | + for it := h.PositiveBucketIterator(); it.Next(); { |
| 126 | + bucket := it.At() |
| 127 | + if bucket.Count != 0 { |
| 128 | + apiBuckets = append(apiBuckets, makeBucket[float64](bucket)) |
| 129 | + } |
| 130 | + } |
| 131 | + return apiBuckets |
| 132 | +} |
| 133 | + |
| 134 | +func makeBucket[BC model.BucketCount](bucket model.Bucket[BC]) APIBucket[BC] { |
| 135 | + boundaries := uint64(2) // () Exclusive on both sides AKA open interval. |
| 136 | + if bucket.LowerInclusive { |
| 137 | + if bucket.UpperInclusive { |
| 138 | + boundaries = 3 // [] Inclusive on both sides AKA closed interval. |
| 139 | + } else { |
| 140 | + boundaries = 1 // [) Inclusive only on lower end AKA right open. |
| 141 | + } |
| 142 | + } else { |
| 143 | + if bucket.UpperInclusive { |
| 144 | + boundaries = 0 // (] Inclusive only on upper end AKA left open. |
| 145 | + } |
| 146 | + } |
| 147 | + return APIBucket[BC]{ |
| 148 | + Boundaries: boundaries, |
| 149 | + Lower: bucket.Lower, |
| 150 | + Upper: bucket.Upper, |
| 151 | + Count: bucket.Count, |
| 152 | + } |
| 153 | +} |
0 commit comments