diff --git a/internal/data/metric.go b/internal/data/metric.go index 2cc6d79d834..d6f6e57cf3a 100644 --- a/internal/data/metric.go +++ b/internal/data/metric.go @@ -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() } } } diff --git a/internal/data/metric_test.go b/internal/data/metric_test.go index 0f09cf7fd64..bb95652efa9 100644 --- a/internal/data/metric_test.go +++ b/internal/data/metric_test.go @@ -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{ {