Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/datadog] Use exact sum and count on distributions when available #7830

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `awscloudwatchlogsexporter`: Remove name from aws cloudwatch logs exporter (#7554)
- `hostreceiver/memoryscraper`: Add memory.utilization (#6221)
- `elasticsearchexporter`: Remove usage of deprecated LogRecord.Name field (#7829).
- `datadogexporter`: Use exact sum, count and average on Datadog distributions (#7830)

### 🛑 Breaking changes 🛑

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,21 @@ func getBounds(p pdata.HistogramDataPoint, idx int) (lowerBound float64, upperBo
return
}

type histogramInfo struct {
// sum of histogram (exact)
sum float64
// count of histogram (exact)
count uint64
// ok to use
ok bool
}

func (t *Translator) getSketchBuckets(
ctx context.Context,
consumer SketchConsumer,
pointDims metricsDimensions,
p pdata.HistogramDataPoint,
histInfo histogramInfo,
delta bool,
) {
startTs := uint64(p.StartTimestamp())
Expand Down Expand Up @@ -199,6 +209,12 @@ func (t *Translator) getSketchBuckets(

sketch := as.Finish()
if sketch != nil {
if histInfo.ok {
// override approximate sum, count and average in sketch with exact values if available.
sketch.Basic.Cnt = int64(histInfo.count)
sketch.Basic.Sum = histInfo.sum
sketch.Basic.Avg = sketch.Basic.Sum / float64(sketch.Basic.Cnt)
}
consumer.ConsumeSketch(ctx, pointDims.name, ts, sketch, pointDims.tags, pointDims.host)
}
}
Expand Down Expand Up @@ -257,33 +273,41 @@ func (t *Translator) mapHistogramMetrics(
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())

if t.cfg.SendCountSum {
count := float64(p.Count())
countDims := pointDims.WithSuffix("count")
histInfo := histogramInfo{ok: true}

countDims := pointDims.WithSuffix("count")
if delta {
histInfo.count = p.Count()
} else if dx, ok := t.prevPts.Diff(countDims, startTs, ts, float64(p.Count())); ok {
histInfo.count = uint64(dx)
} else { // not ok
histInfo.ok = false
}

sumDims := pointDims.WithSuffix("sum")
if !t.isSkippable(sumDims.name, p.Sum()) {
if delta {
consumer.ConsumeTimeSeries(ctx, countDims.name, Count, ts, count, countDims.tags, countDims.host)
} else if dx, ok := t.prevPts.Diff(countDims, startTs, ts, count); ok {
consumer.ConsumeTimeSeries(ctx, countDims.name, Count, ts, dx, countDims.tags, countDims.host)
histInfo.sum = p.Sum()
} else if dx, ok := t.prevPts.Diff(sumDims, startTs, ts, p.Sum()); ok {
histInfo.sum = dx
} else { // not ok
histInfo.ok = false
}
} else { // skippable
histInfo.ok = false
}

if t.cfg.SendCountSum {
sum := p.Sum()
sumDims := pointDims.WithSuffix("sum")
if !t.isSkippable(sumDims.name, p.Sum()) {
if delta {
consumer.ConsumeTimeSeries(ctx, sumDims.name, Count, ts, sum, sumDims.tags, sumDims.host)
} else if dx, ok := t.prevPts.Diff(sumDims, startTs, ts, sum); ok {
consumer.ConsumeTimeSeries(ctx, sumDims.name, Count, ts, dx, sumDims.tags, sumDims.host)
}
}
if t.cfg.SendCountSum && histInfo.ok {
// We only send the sum and count if both values were ok.
consumer.ConsumeTimeSeries(ctx, countDims.name, Count, ts, float64(histInfo.count), countDims.tags, countDims.host)
consumer.ConsumeTimeSeries(ctx, sumDims.name, Count, ts, histInfo.sum, sumDims.tags, sumDims.host)
}

switch t.cfg.HistMode {
case HistogramModeCounters:
t.getLegacyBuckets(ctx, consumer, pointDims, p, delta)
case HistogramModeDistributions:
t.getSketchBuckets(ctx, consumer, pointDims, p, delta)
t.getSketchBuckets(ctx, consumer, pointDims, p, histInfo, delta)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,18 @@ func TestMapDeltaHistogramMetrics(t *testing.T) {
newSketch(dims, uint64(ts), summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Cnt: 20,
Sum: point.Sum(),
Avg: point.Sum() / float64(point.Count()),
Cnt: int64(point.Count()),
}),
}

sketchesAttributeTags := []sketch{
newSketch(dimsTags, uint64(ts), summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: point.Sum(),
Avg: point.Sum() / float64(point.Count()),
Cnt: 20,
}),
}
Expand Down Expand Up @@ -716,8 +716,8 @@ func TestMapCumulativeHistogramMetrics(t *testing.T) {
newSketch(dims, uint64(seconds(2)), summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: 20,
Avg: 20.0 / 30.0,
Cnt: 30,
}),
}
Expand Down Expand Up @@ -1183,8 +1183,8 @@ func TestMapMetrics(t *testing.T) {
newSketchWithHostname("double.histogram", summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: math.Phi,
Avg: math.Phi / 20,
Cnt: 20,
}, attrTags),
},
Expand Down Expand Up @@ -1213,8 +1213,8 @@ func TestMapMetrics(t *testing.T) {
newSketchWithHostname("double.histogram", summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: math.Phi,
Avg: math.Phi / 20.0,
Cnt: 20,
}, attrTags),
},
Expand Down Expand Up @@ -1243,8 +1243,8 @@ func TestMapMetrics(t *testing.T) {
newSketchWithHostname("double.histogram", summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: math.Phi,
Avg: math.Phi / 20,
Cnt: 20,
}, append(attrTags, ilTags...)),
},
Expand Down Expand Up @@ -1273,8 +1273,8 @@ func TestMapMetrics(t *testing.T) {
newSketchWithHostname("double.histogram", summary.Summary{
Min: 0,
Max: 0,
Sum: 0,
Avg: 0,
Sum: math.Phi,
Avg: math.Phi / 20,
Cnt: 20,
}, append(attrTags, ilTags...)),
},
Expand Down Expand Up @@ -1428,5 +1428,5 @@ func TestNaNMetrics(t *testing.T) {
})

// One metric type was unknown or unsupported
assert.Equal(t, observed.FilterMessage("Unsupported metric value").Len(), 6)
assert.Equal(t, observed.FilterMessage("Unsupported metric value").Len(), 7)
}
Loading