| 1 |
// Copyright 2024 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 |
// Provenance-includes-location: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/95e8f8fdc2a9dc87230406c9a3cf02be4fd68bea/pkg/translator/prometheusremotewrite/histograms.go |
| 14 |
// Provenance-includes-license: Apache-2.0 |
| 15 |
// Provenance-includes-copyright: Copyright The OpenTelemetry Authors. |
| 16 |
|
| 17 |
package prometheusremotewrite |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"math" |
| 22 |
|
| 23 |
"github.com/prometheus/common/model" |
| 24 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 25 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 26 |
|
| 27 |
"github.com/prometheus/prometheus/model/value" |
| 28 |
"github.com/prometheus/prometheus/prompb" |
| 29 |
) |
| 30 |
|
| 31 |
const defaultZeroThreshold = 1e-128 |
| 32 |
|
| 33 |
func (c *PrometheusConverter) addExponentialHistogramDataPoints(dataPoints pmetric.ExponentialHistogramDataPointSlice, |
| 34 |
resource pcommon.Resource, settings Settings, baseName string) error { |
| 35 |
for x := 0; x < dataPoints.Len(); x++ { |
| 36 |
pt := dataPoints.At(x) |
| 37 |
lbls := createAttributes( |
| 38 |
resource, |
| 39 |
pt.Attributes(), |
| 40 |
settings.ExternalLabels, |
| 41 |
nil, |
| 42 |
true, |
| 43 |
model.MetricNameLabel, |
| 44 |
baseName, |
| 45 |
) |
| 46 |
ts, _ := c.getOrCreateTimeSeries(lbls) |
| 47 |
|
| 48 |
histogram, err := exponentialToNativeHistogram(pt) |
| 49 |
if err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
ts.Histograms = append(ts.Histograms, histogram) |
| 53 |
|
| 54 |
exemplars := getPromExemplars[pmetric.ExponentialHistogramDataPoint](pt) |
| 55 |
ts.Exemplars = append(ts.Exemplars, exemplars...) |
| 56 |
} |
| 57 |
|
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
// exponentialToNativeHistogram translates OTel Exponential Histogram data point |
| 62 |
// to Prometheus Native Histogram. |
| 63 |
func exponentialToNativeHistogram(p pmetric.ExponentialHistogramDataPoint) (prompb.Histogram, error) { |
| 64 |
scale := p.Scale() |
| 65 |
if scale < -4 { |
| 66 |
return prompb.Histogram{}, |
| 67 |
fmt.Errorf("cannot convert exponential to native histogram."+ |
| 68 |
" Scale must be >= -4, was %d", scale) |
| 69 |
} |
| 70 |
|
| 71 |
var scaleDown int32 |
| 72 |
if scale > 8 { |
| 73 |
scaleDown = scale - 8 |
| 74 |
scale = 8 |
| 75 |
} |
| 76 |
|
| 77 |
pSpans, pDeltas := convertBucketsLayout(p.Positive(), scaleDown) |
| 78 |
nSpans, nDeltas := convertBucketsLayout(p.Negative(), scaleDown) |
| 79 |
|
| 80 |
h := prompb.Histogram{ |
| 81 |
// The counter reset detection must be compatible with Prometheus to |
| 82 |
// safely set ResetHint to NO. This is not ensured currently. |
| 83 |
// Sending a sample that triggers counter reset but with ResetHint==NO |
| 84 |
// would lead to Prometheus panic as it does not double check the hint. |
| 85 |
// Thus we're explicitly saying UNKNOWN here, which is always safe. |
| 86 |
// TODO: using created time stamp should be accurate, but we |
| 87 |
// need to know here if it was used for the detection. |
| 88 |
// Ref: https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/28663#issuecomment-1810577303 |
| 89 |
// Counter reset detection in Prometheus: https://github.com/prometheus/prometheus/blob/f997c72f294c0f18ca13fa06d51889af04135195/tsdb/chunkenc/histogram.go#L232 |
| 90 |
ResetHint: prompb.Histogram_UNKNOWN, |
| 91 |
Schema: scale, |
| 92 |
|
| 93 |
ZeroCount: &prompb.Histogram_ZeroCountInt{ZeroCountInt: p.ZeroCount()}, |
| 94 |
// TODO use zero_threshold, if set, see |
| 95 |
// https://github.com/open-telemetry/opentelemetry-proto/pull/441 |
| 96 |
ZeroThreshold: defaultZeroThreshold, |
| 97 |
|
| 98 |
PositiveSpans: pSpans, |
| 99 |
PositiveDeltas: pDeltas, |
| 100 |
NegativeSpans: nSpans, |
| 101 |
NegativeDeltas: nDeltas, |
| 102 |
|
| 103 |
Timestamp: convertTimeStamp(p.Timestamp()), |
| 104 |
} |
| 105 |
|
| 106 |
if p.Flags().NoRecordedValue() { |
| 107 |
h.Sum = math.Float64frombits(value.StaleNaN) |
| 108 |
h.Count = &prompb.Histogram_CountInt{CountInt: value.StaleNaN} |
| 109 |
} else { |
| 110 |
if p.HasSum() { |
| 111 |
h.Sum = p.Sum() |
| 112 |
} |
| 113 |
h.Count = &prompb.Histogram_CountInt{CountInt: p.Count()} |
| 114 |
} |
| 115 |
return h, nil |
| 116 |
} |
| 117 |
|
| 118 |
// convertBucketsLayout translates OTel Exponential Histogram dense buckets |
| 119 |
// representation to Prometheus Native Histogram sparse bucket representation. |
| 120 |
// |
| 121 |
// The translation logic is taken from the client_golang `histogram.go#makeBuckets` |
| 122 |
// function, see `makeBuckets` https://github.com/prometheus/client_golang/blob/main/prometheus/histogram.go |
| 123 |
// The bucket indexes conversion was adjusted, since OTel exp. histogram bucket |
| 124 |
// index 0 corresponds to the range (1, base] while Prometheus bucket index 0 |
| 125 |
// to the range (base 1]. |
| 126 |
// |
| 127 |
// scaleDown is the factor by which the buckets are scaled down. In other words 2^scaleDown buckets will be merged into one. |
| 128 |
func convertBucketsLayout(buckets pmetric.ExponentialHistogramDataPointBuckets, scaleDown int32) ([]prompb.BucketSpan, []int64) { |
| 129 |
bucketCounts := buckets.BucketCounts() |
| 130 |
if bucketCounts.Len() == 0 { |
| 131 |
return nil, nil |
| 132 |
} |
| 133 |
|
| 134 |
var ( |
| 135 |
spans []prompb.BucketSpan |
| 136 |
deltas []int64 |
| 137 |
count int64 |
| 138 |
prevCount int64 |
| 139 |
) |
| 140 |
|
| 141 |
appendDelta := func(count int64) { |
| 142 |
spans[len(spans)-1].Length++ |
| 143 |
deltas = append(deltas, count-prevCount) |
| 144 |
prevCount = count |
| 145 |
} |
| 146 |
|
| 147 |
// Let the compiler figure out that this is const during this function by |
| 148 |
// moving it into a local variable. |
| 149 |
numBuckets := bucketCounts.Len() |
| 150 |
|
| 151 |
// The offset is scaled and adjusted by 1 as described above. |
| 152 |
bucketIdx := buckets.Offset()>>scaleDown + 1 |
| 153 |
spans = append(spans, prompb.BucketSpan{ |
| 154 |
Offset: bucketIdx, |
| 155 |
Length: 0, |
| 156 |
}) |
| 157 |
|
| 158 |
for i := 0; i < numBuckets; i++ { |
| 159 |
// The offset is scaled and adjusted by 1 as described above. |
| 160 |
nextBucketIdx := (int32(i)+buckets.Offset())>>scaleDown + 1 |
| 161 |
if bucketIdx == nextBucketIdx { // We have not collected enough buckets to merge yet. |
| 162 |
count += int64(bucketCounts.At(i)) |
| 163 |
continue |
| 164 |
} |
| 165 |
if count == 0 { |
| 166 |
count = int64(bucketCounts.At(i)) |
| 167 |
continue |
| 168 |
} |
| 169 |
|
| 170 |
gap := nextBucketIdx - bucketIdx - 1 |
| 171 |
if gap > 2 { |
| 172 |
// We have to create a new span, because we have found a gap |
| 173 |
// of more than two buckets. The constant 2 is copied from the logic in |
| 174 |
// https://github.com/prometheus/client_golang/blob/27f0506d6ebbb117b6b697d0552ee5be2502c5f2/prometheus/histogram.go#L1296 |
| 175 |
spans = append(spans, prompb.BucketSpan{ |
| 176 |
Offset: gap, |
| 177 |
Length: 0, |
| 178 |
}) |
| 179 |
} else { |
| 180 |
// We have found a small gap (or no gap at all). |
| 181 |
// Insert empty buckets as needed. |
| 182 |
for j := int32(0); j < gap; j++ { |
| 183 |
appendDelta(0) |
| 184 |
} |
| 185 |
} |
| 186 |
appendDelta(count) |
| 187 |
count = int64(bucketCounts.At(i)) |
| 188 |
bucketIdx = nextBucketIdx |
| 189 |
} |
| 190 |
// Need to use the last item's index. The offset is scaled and adjusted by 1 as described above. |
| 191 |
gap := (int32(numBuckets)+buckets.Offset()-1)>>scaleDown + 1 - bucketIdx |
| 192 |
if gap > 2 { |
| 193 |
// We have to create a new span, because we have found a gap |
| 194 |
// of more than two buckets. The constant 2 is copied from the logic in |
| 195 |
// https://github.com/prometheus/client_golang/blob/27f0506d6ebbb117b6b697d0552ee5be2502c5f2/prometheus/histogram.go#L1296 |
| 196 |
spans = append(spans, prompb.BucketSpan{ |
| 197 |
Offset: gap, |
| 198 |
Length: 0, |
| 199 |
}) |
| 200 |
} else { |
| 201 |
// We have found a small gap (or no gap at all). |
| 202 |
// Insert empty buckets as needed. |
| 203 |
for j := int32(0); j < gap; j++ { |
| 204 |
appendDelta(0) |
| 205 |
} |
| 206 |
} |
| 207 |
appendDelta(count) |
| 208 |
|
| 209 |
return spans, deltas |
| 210 |
} |