Skip to content

Commit adfcd28

Browse files
authored
Update histogram helpers api (#44978)
### What does this PR do? Add assertion on input data and allow helpers to produce blank sketches - for use in DataDog/dd-go#212091 ### Motivation ### Describe how you validated your changes ### Additional Notes Co-authored-by: ibraheem.aboulnaga <ibraheem.aboulnaga@datadoghq.com>
1 parent a991a9c commit adfcd28

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

pkg/opentelemetry-mapping-go/otlp/metrics/histograms.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package metrics
77

88
import (
9+
"errors"
910
"fmt"
1011
"math"
1112
"time"
@@ -56,17 +57,25 @@ func getBounds(explicitBounds pcommon.Float64Slice, idx int) (lowerBound float64
5657
}
5758

5859
// CreateDDSketchFromHistogramOfDuration creates a DDSketch from regular histogram data point
59-
func CreateDDSketchFromHistogramOfDuration(dp pmetric.HistogramDataPoint, unit string) (*ddsketch.DDSketch, error) {
60+
func CreateDDSketchFromHistogramOfDuration(dp *pmetric.HistogramDataPoint, unit string) (*ddsketch.DDSketch, error) {
6061
relativeAccuracy := 0.01 // 1% relative accuracy
6162
maxNumBins := 2048
6263
newSketch, err := ddsketch.LogCollapsingLowestDenseDDSketch(relativeAccuracy, maxNumBins)
6364
if err != nil {
6465
return nil, err
6566
}
6667

68+
if dp == nil {
69+
return newSketch, nil
70+
}
71+
6772
bucketCounts := dp.BucketCounts()
6873
explicitBounds := dp.ExplicitBounds()
6974

75+
if bucketCounts.Len() != explicitBounds.Len()+1 {
76+
return nil, errors.New("bucket counts length does not match explicit bounds length")
77+
}
78+
7079
// Get scaling factor to convert unit to nanoseconds
7180
scaleToNanos := getTimeUnitScaleToNanos(unit)
7281

@@ -161,13 +170,13 @@ func toStoreFromExponentialBucketsWithUnitScale(b pmetric.ExponentialHistogramDa
161170
}
162171

163172
// CreateDDSketchFromExponentialHistogramOfDuration creates a DDSketch from exponential histogram data point
164-
func CreateDDSketchFromExponentialHistogramOfDuration(p pmetric.ExponentialHistogramDataPoint, unit string) (*ddsketch.DDSketch, error) {
173+
func CreateDDSketchFromExponentialHistogramOfDuration(p *pmetric.ExponentialHistogramDataPoint, scale int32, unit string) (*ddsketch.DDSketch, error) {
165174
// Create the DDSketch stores
166175
scaleToNanos := getTimeUnitScaleToNanos(unit)
167176

168177
// Create the DDSketch mapping that corresponds to the ExponentialHistogram settings
169178
gammaWithOnePercentAccuracy := 1.01 / 0.99
170-
gamma := math.Pow(2, math.Pow(2, float64(-p.Scale())))
179+
gamma := math.Pow(2, math.Pow(2, float64(-scale)))
171180
gamma = math.Min(gamma, gammaWithOnePercentAccuracy)
172181
indexOffset := math.Log(scaleToNanos)
173182
mapping, err := mapping.NewLogarithmicMappingWithGamma(gamma, indexOffset)
@@ -176,9 +185,16 @@ func CreateDDSketchFromExponentialHistogramOfDuration(p pmetric.ExponentialHisto
176185
}
177186

178187
// Calculate the base for the exponential histogram
179-
base := math.Pow(2, math.Pow(2, float64(-p.Scale())))
180-
positiveStore := toStoreFromExponentialBucketsWithUnitScale(p.Positive(), mapping, base, scaleToNanos)
181-
negativeStore := toStoreFromExponentialBucketsWithUnitScale(p.Negative(), mapping, base, scaleToNanos)
188+
base := math.Pow(2, math.Pow(2, float64(-scale)))
189+
var positiveStore store.Store
190+
var negativeStore store.Store
191+
if p != nil {
192+
positiveStore = toStoreFromExponentialBucketsWithUnitScale(p.Positive(), mapping, base, scaleToNanos)
193+
negativeStore = toStoreFromExponentialBucketsWithUnitScale(p.Negative(), mapping, base, scaleToNanos)
194+
} else {
195+
positiveStore = store.NewDenseStore()
196+
negativeStore = store.NewDenseStore()
197+
}
182198

183199
// Create DDSketch with the above mapping and stores
184200
sketch := ddsketch.NewDDSketch(mapping, positiveStore, negativeStore)

pkg/opentelemetry-mapping-go/otlp/metrics/histograms_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func TestCreateDDSketchFromHistogramOfDuration(t *testing.T) {
471471
}
472472

473473
// Test the function
474-
sketch, err := CreateDDSketchFromHistogramOfDuration(dp, tt.unit)
474+
sketch, err := CreateDDSketchFromHistogramOfDuration(&dp, tt.unit)
475475
if tt.hasError {
476476
assert.Error(t, err)
477477
return
@@ -596,7 +596,7 @@ func TestCreateDDSketchFromExponentialHistogramOfDuration(t *testing.T) {
596596
}
597597

598598
// Test the function
599-
sketch, err := CreateDDSketchFromExponentialHistogramOfDuration(dp, tt.unit)
599+
sketch, err := CreateDDSketchFromExponentialHistogramOfDuration(&dp, tt.scale, tt.unit)
600600
if tt.hasError {
601601
assert.Error(t, err)
602602
return
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Each section from every release note are combined when the
2+
# CHANGELOG.rst is rendered. So the text needs to be worded so that
3+
# it does not depend on any information only available in another
4+
# section. This may mean repeating some details, but each section
5+
# must be readable independently of the other.
6+
#
7+
# Each section note must be formatted as reStructuredText.
8+
---
9+
enhancements:
10+
- |
11+
Update the histogram helpers API in the `pkg/opentelemetry-mapping-go/otlp/metrics` package. The API now accepts accept pointers to the OTLP data points, and returns blank DDSketches when the pointer is nil.

0 commit comments

Comments
 (0)