diff --git a/receiver/statsdreceiver/internal/protocol/metric_translator_test.go b/receiver/statsdreceiver/internal/protocol/metric_translator_test.go index 4753469a22ed..e880ad976372 100644 --- a/receiver/statsdreceiver/internal/protocol/metric_translator_test.go +++ b/receiver/statsdreceiver/internal/protocol/metric_translator_test.go @@ -7,7 +7,9 @@ import ( "testing" "time" + "github.com/lightstep/go-expohisto/structure" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/otel/attribute" @@ -215,3 +217,60 @@ func TestBuildSummaryMetricSampled(t *testing.T) { assert.Equal(t, expectedMetric, metric) } } + +func TestBuildHistogramMetric(t *testing.T) { + timeNow := time.Now() + startTime := timeNow.Add(-5 * time.Second) + + attrs := attribute.NewSet( + attribute.String("mykey", "myvalue"), + attribute.String("mykey2", "myvalue2"), + ) + + desc := statsDMetricDescription{ + name: "testHistogram", + metricType: HistogramType, + attrs: attrs, + } + + agg := new(histogramStructure) + cfg := structure.NewConfig(structure.WithMaxSize(10)) + agg.Init(cfg) + agg.UpdateByIncr(2, 2) + agg.UpdateByIncr(2, 1) + agg.UpdateByIncr(2, 4) + agg.UpdateByIncr(1, 1) + agg.UpdateByIncr(8, 3) + agg.UpdateByIncr(0.5, 8) + agg.UpdateByIncr(-5, 8) + + histMetric := histogramMetric{ + agg: agg, + } + + ilm := pmetric.NewScopeMetrics() + + buildHistogramMetric(desc, histMetric, startTime, timeNow, ilm) + + require.NotNil(t, ilm.Metrics()) + require.Equal(t, "testHistogram", ilm.Metrics().At(0).Name()) + + hist := ilm.Metrics().At(0).ExponentialHistogram() + require.NotNil(t, hist) + require.Equal(t, 1, hist.DataPoints().Len()) + require.Equal(t, pmetric.AggregationTemporalityDelta, hist.AggregationTemporality()) + + datapoint := hist.DataPoints().At(0) + require.Equal(t, uint64(27), datapoint.Count()) + require.Equal(t, int32(1), datapoint.Scale()) + require.Equal(t, 3.0, datapoint.Sum()) + require.Equal(t, int32(-3), datapoint.Positive().Offset()) + require.Equal(t, int32(4), datapoint.Negative().Offset()) + require.Equal(t, -5.0, datapoint.Min()) + require.Equal(t, 8.0, datapoint.Max()) + val, _ := datapoint.Attributes().Get("mykey") + require.Equal(t, "myvalue", val.Str()) + val, _ = datapoint.Attributes().Get("mykey2") + require.Equal(t, "myvalue2", val.Str()) + +}