Skip to content

Commit

Permalink
[receiver/statsdreceiver]: test histogram metric builder (#34041)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Jul 16, 2024
1 parent ed3a56a commit d8759e4
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

}

0 comments on commit d8759e4

Please sign in to comment.