Skip to content

Commit

Permalink
dropping histogram buckets. part-1
Browse files Browse the repository at this point in the history
  • Loading branch information
samiura committed Aug 18, 2023
1 parent f000cfc commit cc7650f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
15 changes: 10 additions & 5 deletions pkg/translator/signalfx/from_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
type FromTranslator struct{}

// FromMetrics converts pmetric.Metrics to SignalFx proto data points.
func (ft *FromTranslator) FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, error) {
func (ft *FromTranslator) FromMetrics(md pmetric.Metrics, dropHistogramBuckets bool) ([]*sfxpb.DataPoint, error) {
var sfxDataPoints []*sfxpb.DataPoint

rms := md.ResourceMetrics()
Expand All @@ -47,7 +47,7 @@ func (ft *FromTranslator) FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, e
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
for k := 0; k < ilm.Metrics().Len(); k++ {
sfxDataPoints = append(sfxDataPoints, ft.FromMetric(ilm.Metrics().At(k), extraDimensions)...)
sfxDataPoints = append(sfxDataPoints, ft.FromMetric(ilm.Metrics().At(k), extraDimensions, dropHistogramBuckets)...)
}
}
}
Expand All @@ -57,7 +57,7 @@ func (ft *FromTranslator) FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, e

// FromMetric converts pmetric.Metric to SignalFx proto data points.
// TODO: Remove this and change signalfxexporter to us FromMetrics.
func (ft *FromTranslator) FromMetric(m pmetric.Metric, extraDimensions []*sfxpb.Dimension) []*sfxpb.DataPoint {
func (ft *FromTranslator) FromMetric(m pmetric.Metric, extraDimensions []*sfxpb.Dimension, dropHistogramBuckets bool) []*sfxpb.DataPoint {
var dps []*sfxpb.DataPoint

mt := fromMetricTypeToMetricType(m)
Expand All @@ -68,7 +68,7 @@ func (ft *FromTranslator) FromMetric(m pmetric.Metric, extraDimensions []*sfxpb.
case pmetric.MetricTypeSum:
dps = convertNumberDataPoints(m.Sum().DataPoints(), m.Name(), mt, extraDimensions)
case pmetric.MetricTypeHistogram:
dps = convertHistogram(m.Histogram().DataPoints(), m.Name(), mt, extraDimensions)
dps = convertHistogram(m.Histogram().DataPoints(), m.Name(), mt, extraDimensions, dropHistogramBuckets)
case pmetric.MetricTypeSummary:
dps = convertSummaryDataPoints(m.Summary().DataPoints(), m.Name(), extraDimensions)
case pmetric.MetricTypeExponentialHistogram:
Expand Down Expand Up @@ -131,7 +131,7 @@ func convertNumberDataPoints(in pmetric.NumberDataPointSlice, name string, mt *s
return dps.out
}

func convertHistogram(in pmetric.HistogramDataPointSlice, name string, mt *sfxpb.MetricType, extraDims []*sfxpb.Dimension) []*sfxpb.DataPoint {
func convertHistogram(in pmetric.HistogramDataPointSlice, name string, mt *sfxpb.MetricType, extraDims []*sfxpb.Dimension, dropHistogramBuckets bool) []*sfxpb.DataPoint {
var numDPs int
for i := 0; i < in.Len(); i++ {
histDP := in.At(i)
Expand Down Expand Up @@ -179,6 +179,11 @@ func convertHistogram(in pmetric.HistogramDataPointSlice, name string, mt *sfxpb
maxDP.Value.DoubleValue = &max
}

// Drop Histogram Buckets if flag is set.
if dropHistogramBuckets {
continue
}

bounds := histDP.ExplicitBounds()
counts := histDP.BucketCounts()

Expand Down
91 changes: 90 additions & 1 deletion pkg/translator/signalfx/from_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,96 @@ func Test_FromMetrics(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
from := &FromTranslator{}
gotSfxDataPoints, err := from.FromMetrics(tt.metricsFn())
gotSfxDataPoints, err := from.FromMetrics(tt.metricsFn(), false)
require.NoError(t, err)
// Sort SFx dimensions since they are built from maps and the order
// of those is not deterministic.
sortDimensions(tt.wantSfxDataPoints)
sortDimensions(gotSfxDataPoints)
assert.EqualValues(t, tt.wantSfxDataPoints, gotSfxDataPoints)
})
}

testsWithDropHistogramBuckets := []struct {
name string
metricsFn func() pmetric.Metrics
wantSfxDataPoints []*sfxpb.DataPoint
}{
{
name: "histogram",
metricsFn: func() pmetric.Metrics {
out := pmetric.NewMetrics()
ilm := out.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
m := ilm.Metrics().AppendEmpty()
m.SetName("histogram")
m.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
initHistDP(m.Histogram().DataPoints().AppendEmpty())
return out
},
wantSfxDataPoints: []*sfxpb.DataPoint{
int64SFxDataPoint("histogram_count", &sfxMetricTypeCumulativeCounter, labelMap, 16),
doubleSFxDataPoint("histogram_sum", &sfxMetricTypeCumulativeCounter, labelMap, 100.0),
doubleSFxDataPoint("histogram_min", &sfxMetricTypeGauge, labelMap, 0.1),
doubleSFxDataPoint("histogram_max", &sfxMetricTypeGauge, labelMap, 11.11),
},
},
{
name: "histogram_no_optional",
metricsFn: func() pmetric.Metrics {
out := pmetric.NewMetrics()
ilm := out.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
m := ilm.Metrics().AppendEmpty()
m.SetName("histogram")
m.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
initHistDPNoOptional(m.Histogram().DataPoints().AppendEmpty())
return out
},
wantSfxDataPoints: []*sfxpb.DataPoint{
int64SFxDataPoint("histogram_count", &sfxMetricTypeCumulativeCounter, labelMap, 16),
},
},
{
name: "delta_histogram",
metricsFn: func() pmetric.Metrics {
out := pmetric.NewMetrics()
ilm := out.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
m := ilm.Metrics().AppendEmpty()
m.SetName("delta_histogram")
m.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
initHistDP(m.Histogram().DataPoints().AppendEmpty())
return out
},
wantSfxDataPoints: []*sfxpb.DataPoint{
int64SFxDataPoint("delta_histogram_count", &sfxMetricTypeCounter, labelMap, 16),
doubleSFxDataPoint("delta_histogram_sum", &sfxMetricTypeCounter, labelMap, 100.0),
doubleSFxDataPoint("delta_histogram_min", &sfxMetricTypeGauge, labelMap, 0.1),
doubleSFxDataPoint("delta_histogram_max", &sfxMetricTypeGauge, labelMap, 11.11),
},
},
{
name: "distribution_no_buckets",
metricsFn: func() pmetric.Metrics {
out := pmetric.NewMetrics()
ilm := out.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
m := ilm.Metrics().AppendEmpty()
m.SetName("no_bucket_histo")
dp := m.SetEmptyHistogram().DataPoints().AppendEmpty()
dp.SetCount(2)
dp.SetSum(10)
dp.SetTimestamp(ts)
attrMap.CopyTo(dp.Attributes())
return out
},
wantSfxDataPoints: []*sfxpb.DataPoint{
int64SFxDataPoint("no_bucket_histo_count", &sfxMetricTypeCumulativeCounter, labelMap, 2),
doubleSFxDataPoint("no_bucket_histo_sum", &sfxMetricTypeCumulativeCounter, labelMap, 10),
},
},
}
for _, tt := range testsWithDropHistogramBuckets {
t.Run(tt.name, func(t *testing.T) {
from := &FromTranslator{}
gotSfxDataPoints, err := from.FromMetrics(tt.metricsFn(), true)
require.NoError(t, err)
// Sort SFx dimensions since they are built from maps and the order
// of those is not deterministic.
Expand Down

0 comments on commit cc7650f

Please sign in to comment.