Skip to content

Commit

Permalink
Fix MetricAndDataPointCount to deal with nil (open-telemetry#825)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Apr 13, 2020
1 parent 7375ea2 commit a7794ed
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
27 changes: 19 additions & 8 deletions internal/data/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,30 @@ func (md MetricData) MetricCount() int {
return metricCount
}

// MetricAndDataPointCount calculates the total number of metrics and datapoints.
// MetricAndDataPointCount calculates the total number of metrics and data points.
func (md MetricData) MetricAndDataPointCount() (metricCount int, dataPointCount int) {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
ilms := rms.At(i).InstrumentationLibraryMetrics()
rm := rms.At(i)
if rm.IsNil() {
continue
}
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
metrics := ilms.At(j).Metrics()
ilm := ilms.At(j)
if ilm.IsNil() {
continue
}
metrics := ilm.Metrics()
metricCount += metrics.Len()
for k := 0; k < metrics.Len(); k++ {
dataPointCount += metrics.At(k).Int64DataPoints().Len()
dataPointCount += metrics.At(k).DoubleDataPoints().Len()
dataPointCount += metrics.At(k).HistogramDataPoints().Len()
dataPointCount += metrics.At(k).SummaryDataPoints().Len()
ms := ilm.Metrics()
for k := 0; k < ms.Len(); k++ {
m := ms.At(k)
if m.IsNil() {
continue
}
dataPointCount += m.Int64DataPoints().Len() + m.DoubleDataPoints().Len() +
m.HistogramDataPoints().Len() + m.SummaryDataPoints().Len()
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions internal/data/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,58 @@ func TestMetricAndDataPointCount(t *testing.T) {
assert.EqualValues(t, 4, dps)
}

func TestMetricAndDataPointCountWithNil(t *testing.T) {
ms, dps := MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{nil, {}}).MetricAndDataPointCount()
assert.EqualValues(t, 0, ms)
assert.EqualValues(t, 0, dps)

ms, dps = MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{nil, {}},
},
}).MetricAndDataPointCount()
assert.EqualValues(t, 0, ms)
assert.EqualValues(t, 0, dps)

ms, dps = MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
Metrics: []*otlpmetrics.Metric{nil, {}},
},
},
},
}).MetricAndDataPointCount()
assert.EqualValues(t, 2, ms)
assert.EqualValues(t, 0, dps)

ms, dps = MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
Metrics: []*otlpmetrics.Metric{{
Int64DataPoints: []*otlpmetrics.Int64DataPoint{
nil, {},
},
DoubleDataPoints: []*otlpmetrics.DoubleDataPoint{
nil, {},
},
HistogramDataPoints: []*otlpmetrics.HistogramDataPoint{
nil, {},
},
SummaryDataPoints: []*otlpmetrics.SummaryDataPoint{
nil, {},
},
}},
},
},
},
}).MetricAndDataPointCount()
assert.EqualValues(t, 1, ms)
assert.EqualValues(t, 8, dps)

}

func TestOtlpToInternalReadOnly(t *testing.T) {
metricData := MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
Expand Down

0 comments on commit a7794ed

Please sign in to comment.