Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metric/spans count, add tests for nil entries in the slices #787

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions internal/data/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,17 @@ func (md MetricData) MetricCount() int {
metricCount := 0
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
ils := rms.At(i).InstrumentationLibraryMetrics()
for j := 0; j < ils.Len(); j++ {
metricCount += ils.At(j).Metrics().Len()
rm := rms.At(i)
if rm.IsNil() {
continue
}
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
if ilm.IsNil() {
continue
}
metricCount += ilm.Metrics().Len()
}
}
return metricCount
Expand Down Expand Up @@ -104,12 +112,12 @@ func (md MetricData) MetricAndDataPointCount() (metricCount int, dataPointCount
type MetricType otlpmetrics.MetricDescriptor_Type

const (
MetricTypeUnspecified MetricType = MetricType(otlpmetrics.MetricDescriptor_UNSPECIFIED)
MetricTypeGaugeInt64 MetricType = MetricType(otlpmetrics.MetricDescriptor_GAUGE_INT64)
MetricTypeGaugeDouble MetricType = MetricType(otlpmetrics.MetricDescriptor_GAUGE_DOUBLE)
MetricTypeGaugeHistogram MetricType = MetricType(otlpmetrics.MetricDescriptor_GAUGE_HISTOGRAM)
MetricTypeCounterInt64 MetricType = MetricType(otlpmetrics.MetricDescriptor_COUNTER_INT64)
MetricTypeCounterDouble MetricType = MetricType(otlpmetrics.MetricDescriptor_COUNTER_DOUBLE)
MetricTypeCumulativeHistogram MetricType = MetricType(otlpmetrics.MetricDescriptor_CUMULATIVE_HISTOGRAM)
MetricTypeSummary MetricType = MetricType(otlpmetrics.MetricDescriptor_SUMMARY)
MetricTypeUnspecified = MetricType(otlpmetrics.MetricDescriptor_UNSPECIFIED)
MetricTypeGaugeInt64 = MetricType(otlpmetrics.MetricDescriptor_GAUGE_INT64)
MetricTypeGaugeDouble = MetricType(otlpmetrics.MetricDescriptor_GAUGE_DOUBLE)
MetricTypeGaugeHistogram = MetricType(otlpmetrics.MetricDescriptor_GAUGE_HISTOGRAM)
MetricTypeCounterInt64 = MetricType(otlpmetrics.MetricDescriptor_COUNTER_INT64)
MetricTypeCounterDouble = MetricType(otlpmetrics.MetricDescriptor_COUNTER_DOUBLE)
MetricTypeCumulativeHistogram = MetricType(otlpmetrics.MetricDescriptor_CUMULATIVE_HISTOGRAM)
MetricTypeSummary = MetricType(otlpmetrics.MetricDescriptor_SUMMARY)
)
18 changes: 18 additions & 0 deletions internal/data/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func TestMetricCount(t *testing.T) {
assert.EqualValues(t, 6, md.MetricCount())
}

func TestMetricCountWithNils(t *testing.T) {
assert.EqualValues(t, 0, MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{nil, {}}).MetricCount())
assert.EqualValues(t, 0, MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{nil, {}},
},
}).MetricCount())
assert.EqualValues(t, 2, MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
Metrics: []*otlpmetrics.Metric{nil, {}},
},
},
},
}).MetricCount())
}

func TestMetricAndDataPointCount(t *testing.T) {
md := NewMetricData()
ms, dps := md.MetricAndDataPointCount()
Expand Down
124 changes: 124 additions & 0 deletions internal/data/testdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ var (
spanAttributes = map[string]data.AttributeValue{"span-attr": data.NewAttributeValueString("span-attr-val")}
)

const (
TestLabelKey = "label"
TestLabelKey1 = "label-1"
TestLabelValue1 = "label-value-1"
TestLabelKey2 = "label-2"
TestLabelValue2 = "label-value-2"
TestLabelKey3 = "label-3"
TestLabelValue3 = "label-value-3"
TestAttachmentKey = "exemplar-attachment"
TestAttachmentValue = "exemplar-attachment-value"
)

func initResourceAttributes1(dest data.AttributeMap) {
dest.InitFromMap(resourceAttributes1)
}
Expand Down Expand Up @@ -92,3 +104,115 @@ func generateOtlpSpanLinkAttributes() []*otlpcommon.AttributeKeyValue {
},
}
}

func initMetricLabels1(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey1: TestLabelValue1})
}

func generateOtlpMetricLabels1() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey1,
Value: TestLabelValue1,
},
}
}

func initMetricLabelValue1(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey: TestLabelValue1})
}

func generateOtlpMetricLabelValue1() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey,
Value: TestLabelValue1,
},
}
}

func initMetricLabels12(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey1: TestLabelValue1, TestLabelKey2: TestLabelValue2}).Sort()
}

func generateOtlpMetricLabels12() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey1,
Value: TestLabelValue1,
},
{
Key: TestLabelKey2,
Value: TestLabelValue2,
},
}
}

func initMetricLabels13(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey1: TestLabelValue1, TestLabelKey3: TestLabelValue3}).Sort()
}

func generateOtlpMetricLabels13() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey1,
Value: TestLabelValue1,
},
{
Key: TestLabelKey3,
Value: TestLabelValue3,
},
}
}

func initMetricLabels2(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey2: TestLabelValue2})
}

func generateOtlpMetricLabels2() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey2,
Value: TestLabelValue2,
},
}
}

func initMetricLabelValue2(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey: TestLabelValue2})
}

func generateOtlpMetricLabelValue2() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey,
Value: TestLabelValue2,
},
}
}

func initMetricLabels3(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestLabelKey3: TestLabelValue3})
}

func generateOtlpMetricLabels3() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestLabelKey3,
Value: TestLabelValue3,
},
}
}

func initMetricAttachment(dest data.StringMap) {
dest.InitFromMap(map[string]string{TestAttachmentKey: TestAttachmentValue})
}

func generateOtlpMetricAttachment() []*otlpcommon.StringKeyValue {
return []*otlpcommon.StringKeyValue{
{
Key: TestAttachmentKey,
Value: TestAttachmentValue,
},
}
}