|
13 | 13 |
|
14 | 14 | package cortexpb
|
15 | 15 |
|
16 |
| -import "github.com/prometheus/prometheus/model/histogram" |
| 16 | +import ( |
| 17 | + "github.com/prometheus/prometheus/model/histogram" |
| 18 | + "github.com/prometheus/prometheus/prompb" |
| 19 | +) |
17 | 20 |
|
18 | 21 | func (h Histogram) IsFloatHistogram() bool {
|
19 | 22 | _, ok := h.GetCount().(*Histogram_CountFloat)
|
20 | 23 | return ok
|
21 | 24 | }
|
22 | 25 |
|
| 26 | +// HistogramPromProtoToHistogramProto converts a prometheus protobuf Histogram to cortex protobuf Histogram. |
| 27 | +func HistogramPromProtoToHistogramProto(h prompb.Histogram) Histogram { |
| 28 | + ph := Histogram{ |
| 29 | + Sum: h.Sum, |
| 30 | + Schema: h.Schema, |
| 31 | + ZeroThreshold: h.ZeroThreshold, |
| 32 | + NegativeSpans: spansPromProtoToSpansProto(h.NegativeSpans), |
| 33 | + NegativeDeltas: h.NegativeDeltas, |
| 34 | + NegativeCounts: h.NegativeCounts, |
| 35 | + PositiveSpans: spansPromProtoToSpansProto(h.PositiveSpans), |
| 36 | + PositiveDeltas: h.PositiveDeltas, |
| 37 | + PositiveCounts: h.PositiveCounts, |
| 38 | + ResetHint: Histogram_ResetHint(h.ResetHint), |
| 39 | + TimestampMs: h.Timestamp, |
| 40 | + } |
| 41 | + if h.IsFloatHistogram() { |
| 42 | + ph.Count = &Histogram_CountFloat{CountFloat: h.GetCountFloat()} |
| 43 | + ph.ZeroCount = &Histogram_ZeroCountFloat{ZeroCountFloat: h.GetZeroCountFloat()} |
| 44 | + } else { |
| 45 | + ph.Count = &Histogram_CountInt{CountInt: h.GetCountInt()} |
| 46 | + ph.ZeroCount = &Histogram_ZeroCountInt{ZeroCountInt: h.GetZeroCountInt()} |
| 47 | + } |
| 48 | + return ph |
| 49 | +} |
| 50 | + |
23 | 51 | // HistogramProtoToHistogram extracts a (normal integer) Histogram from the
|
24 | 52 | // provided proto message. The caller has to make sure that the proto message
|
25 | 53 | // represents an interger histogram and not a float histogram.
|
@@ -118,3 +146,12 @@ func spansToSpansProto(s []histogram.Span) []BucketSpan {
|
118 | 146 |
|
119 | 147 | return spans
|
120 | 148 | }
|
| 149 | + |
| 150 | +func spansPromProtoToSpansProto(s []prompb.BucketSpan) []BucketSpan { |
| 151 | + spans := make([]BucketSpan, len(s)) |
| 152 | + for i := 0; i < len(s); i++ { |
| 153 | + spans[i] = BucketSpan{Offset: s[i].Offset, Length: s[i].Length} |
| 154 | + } |
| 155 | + |
| 156 | + return spans |
| 157 | +} |
0 commit comments