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/elasticsearch] Add exponential histogram support #34818

Merged
merged 20 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion exporter/elasticsearchexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func (e *elasticsearchExporter) pushMetricsData(
return nil
}

// TODO: support exponential histogram
switch metric.Type() {
case pmetric.MetricTypeSum:
dps := metric.Sum().DataPoints()
Expand Down Expand Up @@ -252,6 +251,20 @@ func (e *elasticsearchExporter) pushMetricsData(
continue
}
}
case pmetric.MetricTypeExponentialHistogram:
dps := metric.ExponentialHistogram().DataPoints()
for l := 0; l < dps.Len(); l++ {
dp := dps.At(l)
val, err := exponentialHistogramToValue(dp)
if err != nil {
errs = append(errs, err)
continue
}
if err := upsertDataPoint(dp, val); err != nil {
errs = append(errs, err)
continue
}
}
case pmetric.MetricTypeHistogram:
dps := metric.Histogram().DataPoints()
for l := 0; l < dps.Len(); l++ {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package exphistogram

func LowerBoundary(index, scale int) float64 {
// Use this form in case the equation above computes +Inf
// as the lower boundary of a valid bucket.
inverseFactor := math.Ldexp(math.Ln2, -scale)

Check failure on line 6 in exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go

View workflow job for this annotation

GitHub Actions / govulncheck (exporter-1)

undefined: math

Check failure on line 6 in exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go

View workflow job for this annotation

GitHub Actions / govulncheck (exporter-1)

undefined: math
return 2.0 * math.Exp((index-(1<<scale))*inverseFactor)

Check failure on line 7 in exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go

View workflow job for this annotation

GitHub Actions / govulncheck (exporter-1)

undefined: math
}
47 changes: 47 additions & 0 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/exphistogram"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/objmodel"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
)
Expand Down Expand Up @@ -480,6 +481,52 @@ func summaryToValue(dp pmetric.SummaryDataPoint) pcommon.Value {
return vm
}

// exponentialHistogramToValue converts an exponential histogram data point to T-digest.
func exponentialHistogramToValue(dp pmetric.ExponentialHistogramDataPoint) (pcommon.Value, error) {
vm := pcommon.NewValueMap()
m := vm.Map()
counts := m.PutEmptySlice("counts")
values := m.PutEmptySlice("values")

scale := int(dp.Scale())

offset := int(dp.Negative().Offset())
bucketCounts := dp.Negative().BucketCounts()
for i := bucketCounts.Len() - 1; i >= 0; i++ {
count := bucketCounts.At(i)
if count == 0 {
continue
}
lb := -exphistogram.LowerBoundary(offset+i+1, scale)
ub := -exphistogram.LowerBoundary(offset+i, scale)
counts.AppendEmpty().SetInt(int64(count))
values.AppendEmpty().SetDouble(lb + (ub-lb)/2)
}

if zeroCount := dp.ZeroCount(); zeroCount != 0 {
counts.AppendEmpty().SetInt(int64(zeroCount))
// FIXME: do we really have to take the median of last negative and first positive, instead of using 0?
Copy link
Contributor

@felixbarny felixbarny Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect us to be able to just use 0. If the offsets if the positive and negative scale are the same, the midpoint is always zero. If the offsets are different, I don't think the midpoint value is meaningful. As the zero count is the count of values that are zero (or within the zero_threshold), I think we should just use 0 as the value.

lastNegativeBoundary := -exphistogram.LowerBoundary(int(dp.Negative().Offset()), scale)
firstPositiveBoundary := exphistogram.LowerBoundary(int(dp.Positive().Offset()), scale)
values.AppendEmpty().SetDouble(lastNegativeBoundary + (firstPositiveBoundary-lastNegativeBoundary)/2)
}

offset = int(dp.Positive().Offset())
bucketCounts = dp.Positive().BucketCounts()
for i := 0; i < bucketCounts.Len(); i++ {
count := bucketCounts.At(i)
if count == 0 {
continue
}
lb := exphistogram.LowerBoundary(offset+i, scale)
ub := exphistogram.LowerBoundary(offset+i+1, scale)
counts.AppendEmpty().SetInt(int64(count))
values.AppendEmpty().SetDouble(lb + (ub-lb)/2)
}

return vm, nil
}

func histogramToValue(dp pmetric.HistogramDataPoint) (pcommon.Value, error) {
// Histogram conversion function is from
// https://github.com/elastic/apm-data/blob/3b28495c3cbdc0902983134276eb114231730249/input/otlp/metrics.go#L277
Expand Down
Loading