Skip to content

Commit

Permalink
Fix nits in pdata usage
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Dec 2, 2020
1 parent f7cc49d commit 887b953
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 107 deletions.
9 changes: 5 additions & 4 deletions consumer/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,11 +1070,11 @@ func TestAttributeValueArray(t *testing.T) {
assert.EqualValues(t, NewAnyValueArray(), a1.ArrayVal())
assert.EqualValues(t, 0, a1.ArrayVal().Len())

a1.ArrayVal().Append(NewAttributeValueDouble(123))
a1.ArrayVal().Resize(1)
v := a1.ArrayVal().At(0)
v.SetDoubleVal(123)
assertArrayJSON(t, `[123]`, a1)
assert.EqualValues(t, 1, a1.ArrayVal().Len())

v := a1.ArrayVal().At(0)
assert.EqualValues(t, AttributeValueDOUBLE, v.Type())
assert.EqualValues(t, 123, v.DoubleVal())

Expand All @@ -1083,7 +1083,8 @@ func TestAttributeValueArray(t *testing.T) {
assertArrayJSON(t, `[]`, a2)
assert.EqualValues(t, 0, a2.ArrayVal().Len())

a2.ArrayVal().Append(NewAttributeValueString("somestr"))
a2.ArrayVal().Resize(1)
a2.ArrayVal().At(0).SetStringVal("somestr")
assertArrayJSON(t, `["somestr"]`, a2)
assert.EqualValues(t, 1, a2.ArrayVal().Len())

Expand Down
8 changes: 4 additions & 4 deletions consumer/pdata/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ func TestMetricAndDataPointCountWithEmpty(t *testing.T) {

func TestMetricAndDataPointCountWithNilDataPoints(t *testing.T) {
metrics := NewMetrics()
rm := NewResourceMetrics()
metrics.ResourceMetrics().Append(rm)
ilm := NewInstrumentationLibraryMetrics()
rm.InstrumentationLibraryMetrics().Append(ilm)
metrics.ResourceMetrics().Resize(1)
rm := metrics.ResourceMetrics().At(0)
rm.InstrumentationLibraryMetrics().Resize(1)
ilm := rm.InstrumentationLibraryMetrics().At(0)
intGauge := NewMetric()
ilm.Metrics().Append(intGauge)
intGauge.SetDataType(MetricDataTypeIntGauge)
Expand Down
17 changes: 1 addition & 16 deletions exporter/exporterhelper/resource_to_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestConvertResourceToLabels(t *testing.T) {

}

func TestConvertResourceToLabelsAllDataTypes(t *testing.T) {
func TestConvertResourceToLabelsAllDataTypesEmptyDataPoint(t *testing.T) {
md := testdata.GenerateMetricsAllTypesEmptyDataPoint()
assert.NotNil(t, md)

Expand Down Expand Up @@ -73,18 +73,3 @@ func TestConvertResourceToLabelsAllDataTypes(t *testing.T) {
assert.Equal(t, 0, md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(5).IntHistogram().DataPoints().At(0).LabelsMap().Len())

}

func TestConvertResourceToLabelsAllDataTypesNilDataPoint(t *testing.T) {
md := testdata.GenerateMetricsAllTypesNilDataPoint()
assert.NotNil(t, md)

// Before converting resource to labels
assert.Equal(t, 1, md.ResourceMetrics().At(0).Resource().Attributes().Len())

cloneMd := convertResourceToLabels(md)

// After converting resource to labels
assert.Equal(t, 1, cloneMd.ResourceMetrics().At(0).Resource().Attributes().Len())

assert.Equal(t, 1, md.ResourceMetrics().At(0).Resource().Attributes().Len())
}
1 change: 0 additions & 1 deletion exporter/loggingexporter/logging_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestLoggingMetricsExporterNoErrors(t *testing.T) {

assert.NoError(t, lme.ConsumeMetrics(context.Background(), testdata.GenerateMetricsEmpty()))
assert.NoError(t, lme.ConsumeMetrics(context.Background(), testdata.GeneratMetricsAllTypesWithSampleDatapoints()))
assert.NoError(t, lme.ConsumeMetrics(context.Background(), testdata.GenerateMetricsAllTypesNilDataPoint()))
assert.NoError(t, lme.ConsumeMetrics(context.Background(), testdata.GenerateMetricsAllTypesEmptyDataPoint()))
assert.NoError(t, lme.ConsumeMetrics(context.Background(), testdata.GenerateMetricsMetricTypeInvalid()))

Expand Down
23 changes: 11 additions & 12 deletions internal/processor/filterexpr/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestIntGaugeEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeIntGauge)
dps := m.IntGauge().DataPoints()
dps.Append(pdata.NewIntDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand All @@ -107,7 +107,7 @@ func TestDoubleGaugeEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeDoubleGauge)
dps := m.DoubleGauge().DataPoints()
dps.Append(pdata.NewDoubleDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand All @@ -120,7 +120,7 @@ func TestDoubleSumEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeDoubleSum)
dps := m.DoubleSum().DataPoints()
dps.Append(pdata.NewDoubleDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand All @@ -133,7 +133,7 @@ func TestIntSumEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeIntSum)
dps := m.IntSum().DataPoints()
dps.Append(pdata.NewIntDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand All @@ -146,7 +146,7 @@ func TestIntHistogramEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeIntHistogram)
dps := m.IntHistogram().DataPoints()
dps.Append(pdata.NewIntHistogramDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand All @@ -159,7 +159,7 @@ func TestDoubleHistogramEmptyDataPoint(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeDoubleHistogram)
dps := m.DoubleHistogram().DataPoints()
dps.Append(pdata.NewDoubleHistogramDataPoint())
dps.Resize(1)
matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
assert.True(t, matched)
Expand Down Expand Up @@ -202,11 +202,11 @@ func testMatchIntGauge(t *testing.T, metricName, expression string, lbls map[str
m.SetName(metricName)
m.SetDataType(pdata.MetricDataTypeIntGauge)
dps := m.IntGauge().DataPoints()
pt := pdata.NewIntDataPoint()
dps.Resize(1)
pt := dps.At(0)
if lbls != nil {
pt.LabelsMap().InitFromMap(lbls)
}
dps.Append(pt)
match, err := matcher.MatchMetric(m)
assert.NoError(t, err)
return match
Expand All @@ -221,14 +221,13 @@ func TestMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) {
m.SetName("my.metric")
m.SetDataType(pdata.MetricDataTypeIntGauge)
dps := m.IntGauge().DataPoints()
dps.Resize(2)

pt1 := pdata.NewIntDataPoint()
pt1 := dps.At(0)
pt1.LabelsMap().Insert("foo", "bar")
dps.Append(pt1)

pt2 := pdata.NewIntDataPoint()
pt2 := dps.At(1)
pt2.LabelsMap().Insert("baz", "glarch")
dps.Append(pt2)

matched, err := matcher.MatchMetric(m)
assert.NoError(t, err)
Expand Down
24 changes: 0 additions & 24 deletions internal/testdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,6 @@ func GenerateMetricsAllTypesNoDataPoints() pdata.Metrics {
return md
}

func GenerateMetricsAllTypesNilDataPoint() pdata.Metrics {
md := GenerateMetricsOneEmptyInstrumentationLibrary()
ilm0 := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0)
ms := ilm0.Metrics()
ms.Resize(7)

initMetric(ms.At(0), TestGaugeDoubleMetricName, pdata.MetricDataTypeDoubleGauge)
ms.At(0).DoubleGauge().DataPoints().Append(pdata.NewDoubleDataPoint())
initMetric(ms.At(1), TestGaugeIntMetricName, pdata.MetricDataTypeIntGauge)
ms.At(1).IntGauge().DataPoints().Append(pdata.NewIntDataPoint())
initMetric(ms.At(2), TestCounterDoubleMetricName, pdata.MetricDataTypeDoubleSum)
ms.At(2).DoubleSum().DataPoints().Append(pdata.NewDoubleDataPoint())
initMetric(ms.At(3), TestCounterIntMetricName, pdata.MetricDataTypeIntSum)
ms.At(3).IntSum().DataPoints().Append(pdata.NewIntDataPoint())
initMetric(ms.At(4), TestDoubleHistogramMetricName, pdata.MetricDataTypeDoubleHistogram)
ms.At(4).DoubleHistogram().DataPoints().Append(pdata.NewDoubleHistogramDataPoint())
initMetric(ms.At(5), TestIntHistogramMetricName, pdata.MetricDataTypeIntHistogram)
ms.At(5).IntHistogram().DataPoints().Append(pdata.NewIntHistogramDataPoint())
initMetric(ms.At(6), TestDoubleSummaryMetricName, pdata.MetricDataTypeDoubleSummary)
ms.At(6).DoubleSummary().DataPoints().Append(pdata.NewDoubleSummaryDataPoint())

return md
}

func GenerateMetricsAllTypesEmptyDataPoint() pdata.Metrics {
md := GenerateMetricsOneEmptyInstrumentationLibrary()
ilm0 := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0)
Expand Down
18 changes: 10 additions & 8 deletions processor/filterprocessor/metric_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ func (idx metricIndex) add(rmIdx, ilmIdx, mIdx int) {
}

func (idx metricIndex) extract(pdm pdata.Metrics) pdata.Metrics {
rmsIn := pdm.ResourceMetrics()
out := pdata.NewMetrics()
rmSliceOut := out.ResourceMetrics()
for _, rmIdx := range sortRM(idx.m) {

sortRMIdx := sortRM(idx.m)
rmsIn := pdm.ResourceMetrics()
rmSliceOut.Resize(len(sortRMIdx))
pos := 0
for _, rmIdx := range sortRMIdx {
rmIn := rmsIn.At(rmIdx)
ilmSliceIn := rmIn.InstrumentationLibraryMetrics()

rmOut := pdata.NewResourceMetrics()
rmSliceOut.Append(rmOut)
resourceOut := rmOut.Resource()
rmIn.Resource().CopyTo(resourceOut)
rmOut := rmSliceOut.At(pos)
pos++
rmIn.Resource().CopyTo(rmOut.Resource())
ilmSliceOut := rmOut.InstrumentationLibraryMetrics()
ilmIndexes := idx.m[rmIdx]
for _, ilmIdx := range sortILM(ilmIndexes) {
Expand All @@ -73,8 +76,7 @@ func (idx metricIndex) extract(pdm pdata.Metrics) pdata.Metrics {
ilmIn.InstrumentationLibrary().CopyTo(ilOut)
mSliceOut := ilmOut.Metrics()
for _, metricIdx := range sortMetrics(ilmIndexes[ilmIdx]) {
metric := mSliceIn.At(metricIdx)
mSliceOut.Append(metric)
mSliceOut.Append(mSliceIn.At(metricIdx))
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions service/internal/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions translator/trace/jaeger/jaegerproto_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func jSpansToInternal(spans []*model.Span) map[instrumentationLibrary]pdata.Inst
if span == nil || reflect.DeepEqual(span, blankJaegerProtoSpan) {
continue
}
span, library := jSpanToInternal(span)
pSpan, library := jSpanToInternal(span)
ils, found := spansByLibrary[library]
if !found {
ils = pdata.NewInstrumentationLibrarySpans()
Expand All @@ -153,7 +153,7 @@ func jSpansToInternal(spans []*model.Span) map[instrumentationLibrary]pdata.Inst
ils.InstrumentationLibrary().SetVersion(library.version)
}
}
ils.Spans().Append(span)
ils.Spans().Append(pSpan)
}
return spansByLibrary
}
Expand Down
18 changes: 6 additions & 12 deletions translator/trace/protospan_translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,27 +278,21 @@ func jsonMapToAttributeMap(attrs map[string]interface{}, dest pdata.AttributeMap
func jsonArrayToAttributeArray(jArray []interface{}, dest pdata.AnyValueArray) {
for _, val := range jArray {
if val == nil {
av := pdata.NewAttributeValueNull()
dest.Append(av)
dest.Append(pdata.NewAttributeValueNull())
continue
}
if s, ok := val.(string); ok {
av := pdata.NewAttributeValueString(s)
dest.Append(av)
dest.Append(pdata.NewAttributeValueString(s))
} else if d, ok := val.(float64); ok {
if math.Mod(d, 1.0) == 0.0 {
av := pdata.NewAttributeValueInt(int64(d))
dest.Append(av)
dest.Append(pdata.NewAttributeValueInt(int64(d)))
} else {
av := pdata.NewAttributeValueDouble(d)
dest.Append(av)
dest.Append(pdata.NewAttributeValueDouble(d))
}
} else if b, ok := val.(bool); ok {
av := pdata.NewAttributeValueBool(b)
dest.Append(av)
dest.Append(pdata.NewAttributeValueBool(b))
} else {
av := pdata.NewAttributeValueString("<Invalid array value>")
dest.Append(av)
dest.Append(pdata.NewAttributeValueString("<Invalid array value>"))
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions translator/trace/protospan_translation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,11 @@ func TestAttributeMapToStringAndBack(t *testing.T) {
func TestAttributeArrayToStringAndBack(t *testing.T) {
expected := pdata.NewAttributeValueArray()
attrArr := expected.ArrayVal()
strAV := pdata.NewAttributeValueString("strVal")
attrArr.Append(strAV)
intAV := pdata.NewAttributeValueInt(7)
attrArr.Append(intAV)
floatAV := pdata.NewAttributeValueDouble(18.6)
attrArr.Append(floatAV)
boolAV := pdata.NewAttributeValueBool(false)
attrArr.Append(boolAV)
nullAV := pdata.NewAttributeValueNull()
attrArr.Append(nullAV)
attrArr.Append(pdata.NewAttributeValueString("strVal"))
attrArr.Append(pdata.NewAttributeValueInt(7))
attrArr.Append(pdata.NewAttributeValueDouble(18.6))
attrArr.Append(pdata.NewAttributeValueBool(false))
attrArr.Append(pdata.NewAttributeValueNull())
strVal := AttributeValueToString(expected, false)
dest := pdata.NewAttributeMap()
UpsertStringToAttributeMap("parent", strVal, dest, false)
Expand Down

0 comments on commit 887b953

Please sign in to comment.