Skip to content

Commit

Permalink
Remove usage of Map.Upsert() where possible (#13645)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 26, 2022
1 parent d2f01b4 commit 2590a50
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 96 deletions.
120 changes: 60 additions & 60 deletions pkg/batchperresourceattr/batchperresourceattr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

func TestSplitTracesOneResourceSpans(t *testing.T) {
inBatch := ptrace.NewTraces()
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "1")

sink := new(consumertest.TracesSink)
bpr := NewBatchPerResourceTraces("attr_key", sink)
Expand All @@ -45,8 +45,8 @@ func TestSplitTracesOneResourceSpans(t *testing.T) {

func TestSplitTracesReturnError(t *testing.T) {
inBatch := ptrace.NewTraces()
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "1")

err := errors.New("test_error")
bpr := NewBatchPerResourceTraces("attr_key", consumertest.NewErr(err))
Expand All @@ -55,10 +55,10 @@ func TestSplitTracesReturnError(t *testing.T) {

func TestSplitTracesSameResource(t *testing.T) {
inBatch := ptrace.NewTraces()
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "same_attr_val", "1")
expected := inBatch.Clone()

sink := new(consumertest.TracesSink)
Expand All @@ -71,15 +71,15 @@ func TestSplitTracesSameResource(t *testing.T) {

func TestSplitTracesIntoDifferentBatches(t *testing.T) {
inBatch := ptrace.NewTraces()
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "diff_attr_key", pcommon.NewValueString("1"))
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "2")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "3")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "4")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "1")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "2")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "3")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "attr_key", "4")
fillResourceSpans(inBatch.ResourceSpans().AppendEmpty(), "diff_attr_key", "1")
expected := inBatch.Clone()

sink := new(consumertest.TracesSink)
Expand All @@ -97,7 +97,7 @@ func TestSplitTracesIntoDifferentBatches(t *testing.T) {

func TestSplitMetricsOneResourceMetrics(t *testing.T) {
inBatch := pmetric.NewMetrics()
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "1")
expected := inBatch.Clone()

sink := new(consumertest.MetricsSink)
Expand All @@ -110,8 +110,8 @@ func TestSplitMetricsOneResourceMetrics(t *testing.T) {

func TestSplitMetricsReturnError(t *testing.T) {
inBatch := pmetric.NewMetrics()
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "1")

err := errors.New("test_error")
bpr := NewBatchPerResourceMetrics("attr_key", consumertest.NewErr(err))
Expand All @@ -120,10 +120,10 @@ func TestSplitMetricsReturnError(t *testing.T) {

func TestSplitMetricsSameResource(t *testing.T) {
inBatch := pmetric.NewMetrics()
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "same_attr_val", "1")
expected := inBatch.Clone()

sink := new(consumertest.MetricsSink)
Expand All @@ -136,15 +136,15 @@ func TestSplitMetricsSameResource(t *testing.T) {

func TestSplitMetricsIntoDifferentBatches(t *testing.T) {
inBatch := pmetric.NewMetrics()
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "diff_attr_key", pcommon.NewValueString("1"))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "2")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "3")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "4")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "1")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "2")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "3")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", "4")
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "diff_attr_key", "1")
expected := inBatch.Clone()

sink := new(consumertest.MetricsSink)
Expand All @@ -162,7 +162,7 @@ func TestSplitMetricsIntoDifferentBatches(t *testing.T) {

func TestSplitLogsOneResourceLogs(t *testing.T) {
inBatch := plog.NewLogs()
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "1")
expected := inBatch.Clone()

sink := new(consumertest.LogsSink)
Expand All @@ -175,8 +175,8 @@ func TestSplitLogsOneResourceLogs(t *testing.T) {

func TestSplitLogsReturnError(t *testing.T) {
inBatch := plog.NewLogs()
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "1")

err := errors.New("test_error")
bpr := NewBatchPerResourceLogs("attr_key", consumertest.NewErr(err))
Expand All @@ -185,10 +185,10 @@ func TestSplitLogsReturnError(t *testing.T) {

func TestSplitLogsSameResource(t *testing.T) {
inBatch := plog.NewLogs()
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "same_attr_val", "1")
expected := inBatch.Clone()

sink := new(consumertest.LogsSink)
Expand All @@ -201,15 +201,15 @@ func TestSplitLogsSameResource(t *testing.T) {

func TestSplitLogsIntoDifferentBatches(t *testing.T) {
inBatch := plog.NewLogs()
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("2"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("3"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString("4"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "diff_attr_key", pcommon.NewValueString("1"))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "2")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "3")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "4")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "1")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "2")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "3")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", "4")
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "diff_attr_key", "1")
expected := inBatch.Clone()

sink := new(consumertest.LogsSink)
Expand Down Expand Up @@ -247,9 +247,9 @@ func sortTraces(tds []ptrace.Traces, attrKey string) {
})
}

func fillResourceSpans(rs ptrace.ResourceSpans, key string, val pcommon.Value) {
rs.Resource().Attributes().Upsert(key, val)
rs.Resource().Attributes().Upsert("__other_key__", pcommon.NewValueInt(123))
func fillResourceSpans(rs ptrace.ResourceSpans, key string, val string) {
rs.Resource().Attributes().UpsertString(key, val)
rs.Resource().Attributes().UpsertInt("__other_key__", 123)
ils := rs.ScopeSpans().AppendEmpty()
firstSpan := ils.Spans().AppendEmpty()
firstSpan.SetName("first-span")
Expand Down Expand Up @@ -281,9 +281,9 @@ func sortMetrics(tds []pmetric.Metrics, attrKey string) {
})
}

func fillResourceMetrics(rs pmetric.ResourceMetrics, key string, val pcommon.Value) {
rs.Resource().Attributes().Upsert(key, val)
rs.Resource().Attributes().Upsert("__other_key__", pcommon.NewValueInt(123))
func fillResourceMetrics(rs pmetric.ResourceMetrics, key string, val string) {
rs.Resource().Attributes().UpsertString(key, val)
rs.Resource().Attributes().UpsertInt("__other_key__", 123)
ils := rs.ScopeMetrics().AppendEmpty()
firstMetric := ils.Metrics().AppendEmpty()
firstMetric.SetName("first-metric")
Expand Down Expand Up @@ -315,9 +315,9 @@ func sortLogs(tds []plog.Logs, attrKey string) {
})
}

func fillResourceLogs(rs plog.ResourceLogs, key string, val pcommon.Value) {
rs.Resource().Attributes().Upsert(key, val)
rs.Resource().Attributes().Upsert("__other_key__", pcommon.NewValueInt(123))
func fillResourceLogs(rs plog.ResourceLogs, key string, val string) {
rs.Resource().Attributes().UpsertString(key, val)
rs.Resource().Attributes().UpsertInt("__other_key__", 123)
ils := rs.ScopeLogs().AppendEmpty()
firstLogRecord := ils.LogRecords().AppendEmpty()
firstLogRecord.FlagsStruct().SetIsSampled(rand.Int31()%2 == 1)
Expand All @@ -330,7 +330,7 @@ func BenchmarkBatchPerResourceTraces(b *testing.B) {
rss := inBatch.ResourceSpans()
rss.EnsureCapacity(64)
for i := 0; i < 64; i++ {
fillResourceSpans(rss.AppendEmpty(), "attr_key", pcommon.NewValueString(strconv.Itoa(i%8)))
fillResourceSpans(rss.AppendEmpty(), "attr_key", strconv.Itoa(i%8))
}
bpr := NewBatchPerResourceTraces("attr_key", consumertest.NewNop())
b.ReportAllocs()
Expand All @@ -346,7 +346,7 @@ func BenchmarkBatchPerResourceMetrics(b *testing.B) {
inBatch := pmetric.NewMetrics()
inBatch.ResourceMetrics().EnsureCapacity(64)
for i := 0; i < 64; i++ {
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", pcommon.NewValueString(strconv.Itoa(i%8)))
fillResourceMetrics(inBatch.ResourceMetrics().AppendEmpty(), "attr_key", strconv.Itoa(i%8))
}
bpr := NewBatchPerResourceMetrics("attr_key", consumertest.NewNop())
b.ReportAllocs()
Expand All @@ -362,7 +362,7 @@ func BenchmarkBatchPerResourceLogs(b *testing.B) {
inBatch := plog.NewLogs()
inBatch.ResourceLogs().EnsureCapacity(64)
for i := 0; i < 64; i++ {
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", pcommon.NewValueString(strconv.Itoa(i%8)))
fillResourceLogs(inBatch.ResourceLogs().AppendEmpty(), "attr_key", strconv.Itoa(i%8))
}
bpr := NewBatchPerResourceLogs("attr_key", consumertest.NewNop())
b.ReportAllocs()
Expand Down
13 changes: 6 additions & 7 deletions pkg/translator/opencensus/metrics_to_oc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
ocmetrics "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
ocresource "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -35,12 +34,12 @@ import (
func TestMetricsToOC(t *testing.T) {
sampleMetricData := testdata.GeneratMetricsAllTypesWithSampleDatapoints()
attrs := sampleMetricData.ResourceMetrics().At(0).Resource().Attributes()
attrs.Upsert(conventions.AttributeHostName, pcommon.NewValueString("host1"))
attrs.Upsert(conventions.AttributeProcessPID, pcommon.NewValueInt(123))
attrs.Upsert(occonventions.AttributeProcessStartTime, pcommon.NewValueString("2020-02-11T20:26:00Z"))
attrs.Upsert(conventions.AttributeTelemetrySDKLanguage, pcommon.NewValueString("cpp"))
attrs.Upsert(conventions.AttributeTelemetrySDKVersion, pcommon.NewValueString("v2.0.1"))
attrs.Upsert(occonventions.AttributeExporterVersion, pcommon.NewValueString("v1.2.0"))
attrs.UpsertString(conventions.AttributeHostName, "host1")
attrs.UpsertInt(conventions.AttributeProcessPID, 123)
attrs.UpsertString(occonventions.AttributeProcessStartTime, "2020-02-11T20:26:00Z")
attrs.UpsertString(conventions.AttributeTelemetrySDKLanguage, "cpp")
attrs.UpsertString(conventions.AttributeTelemetrySDKVersion, "v2.0.1")
attrs.UpsertString(occonventions.AttributeExporterVersion, "v1.2.0")

tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/opencensus/oc_to_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestOcNodeResourceToInternal(t *testing.T) {
ocResource = generateOcResource()
expectedAttrs := generateResourceWithOcNodeAndResource().Attributes()
// We don't have type information in ocResource, so need to make int attr string
expectedAttrs.Upsert("resource-int-attr", pcommon.NewValueString("123"))
expectedAttrs.UpsertString("resource-int-attr", "123")
ocNodeResourceToInternal(ocNode, ocResource, resource)
assert.EqualValues(t, expectedAttrs.Sort(), resource.Attributes().Sort())

Expand Down
Loading

0 comments on commit 2590a50

Please sign in to comment.