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

Replace usages of Map.Insert* with Map.Upsert* #13742

Merged
merged 1 commit into from
Aug 31, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,16 @@ func createLogData(numberOfLogs int) plog.Logs {
fillComplexAttributeValueMap(logRecord.Attributes().UpsertEmptyMap("map-value"))
logRecord.Body().SetStringVal("log contents")
case 6:
arrayVal := pcommon.NewValueSlice()
arrayVal.SliceVal().AppendEmpty().SetStringVal("array")
logRecord.Attributes().Insert("array-value", arrayVal)
logRecord.Attributes().UpsertEmptySlice("array-value").AppendEmpty().SetStringVal("array")
logRecord.Body().SetStringVal("log contents")
default:
logRecord.Body().SetStringVal("log contents")
}
logRecord.Attributes().InsertString(conventions.AttributeServiceName, "myapp")
logRecord.Attributes().InsertString("my-label", "myapp-type")
logRecord.Attributes().InsertString(conventions.AttributeHostName, "myhost")
logRecord.Attributes().InsertString("custom", "custom")
logRecord.Attributes().Insert("null-value", pcommon.NewValueEmpty())
logRecord.Attributes().UpsertString(conventions.AttributeServiceName, "myapp")
logRecord.Attributes().UpsertString("my-label", "myapp-type")
logRecord.Attributes().UpsertString(conventions.AttributeHostName, "myhost")
logRecord.Attributes().UpsertString("custom", "custom")
logRecord.Attributes().UpsertEmpty("null-value")

logRecord.SetTimestamp(ts)
}
Expand Down
6 changes: 3 additions & 3 deletions exporter/awsxrayexporter/awsxray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func constructSpanAttributes(attributes map[string]interface{}) pcommon.Map {
attrs := pcommon.NewMap()
for key, value := range attributes {
if cast, ok := value.(int); ok {
attrs.InsertInt(key, int64(cast))
attrs.UpsertInt(key, int64(cast))
} else if cast, ok := value.(int64); ok {
attrs.InsertInt(key, cast)
attrs.UpsertInt(key, cast)
} else {
attrs.InsertString(key, fmt.Sprintf("%v", value))
attrs.UpsertString(key, fmt.Sprintf("%v", value))
}
}
return attrs
Expand Down
178 changes: 86 additions & 92 deletions exporter/awsxrayexporter/internal/translator/aws_test.go

Large diffs are not rendered by default.

250 changes: 115 additions & 135 deletions exporter/awsxrayexporter/internal/translator/segment_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions exporter/datadogexporter/metrics_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ func createTestMetrics(additionalAttributes map[string]string) pmetric.Metrics {
rm := rms.AppendEmpty()

attrs := rm.Resource().Attributes()
attrs.InsertString("datadog.host.name", host)
attrs.UpsertString("datadog.host.name", host)
for attr, val := range additionalAttributes {
attrs.InsertString(attr, val)
attrs.UpsertString(attr, val)
}
ilms := rm.ScopeMetrics()

Expand Down
10 changes: 4 additions & 6 deletions exporter/humioexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,8 @@ func TestToHumioAttributes(t *testing.T) {
desc: "Nested map",
attr: func() pcommon.Map {
attrMap := pcommon.NewMap()
nested := pcommon.NewValueMap()
nested.MapVal().UpsertString("key", "val")
attrMap.Insert("nested", nested)
attrMap.InsertBool("active", true)
attrMap.UpsertEmptyMap("nested").UpsertString("key", "val")
attrMap.UpsertBool("active", true)
return attrMap
},
expected: map[string]interface{}{
Expand All @@ -447,10 +445,10 @@ func TestToHumioAttributesShaded(t *testing.T) {
// Arrange
attrMapA := pcommon.NewMap()
attrMapA.UpsertString("string", "val")
attrMapA.InsertInt("integer", 42)
attrMapA.UpsertInt("integer", 42)

attrMapB := pcommon.NewMap()
attrMapB.InsertInt("integer", 0)
attrMapB.UpsertInt("integer", 0)
attrMapB.UpsertString("key", "val")

expected := map[string]interface{}{
Expand Down
26 changes: 10 additions & 16 deletions exporter/logzioexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ func fillLogOne(log plog.LogRecord) {
attrs.UpsertDouble("instance_num", 1)

// nested body map
attVal := pcommon.NewValueMap()
attNestedVal := pcommon.NewValueMap()

attMap := attVal.MapVal()
attMap := log.Body().SetEmptyMapVal()
attMap.UpsertDouble("23", 45)
attMap.UpsertString("foo", "bar")
attMap.UpsertString("message", "hello there")
attNestedMap := attNestedVal.MapVal()
attNestedMap := attMap.UpsertEmptyMap("nested")
attNestedMap.UpsertString("string", "v1")
attNestedMap.UpsertDouble("number", 499)
attMap.Insert("nested", attNestedVal)
attVal.CopyTo(log.Body())

}

func fillLogTwo(log plog.LogRecord) {
Expand All @@ -99,10 +93,10 @@ func fillLogTwo(log plog.LogRecord) {
log.SetSeverityText("Info")

attrs := log.Attributes()
attrs.InsertString("customer", "acme")
attrs.InsertDouble("number", 64)
attrs.InsertBool("bool", true)
attrs.InsertString("env", "dev")
attrs.UpsertString("customer", "acme")
attrs.UpsertDouble("number", 64)
attrs.UpsertBool("bool", true)
attrs.UpsertString("env", "dev")
log.Body().SetStringVal("something happened")
}
func fillLogNoTimestamp(log plog.LogRecord) {
Expand All @@ -111,10 +105,10 @@ func fillLogNoTimestamp(log plog.LogRecord) {
log.SetSeverityText("Info")

attrs := log.Attributes()
attrs.InsertString("customer", "acme")
attrs.InsertDouble("number", 64)
attrs.InsertBool("bool", true)
attrs.InsertString("env", "dev")
attrs.UpsertString("customer", "acme")
attrs.UpsertDouble("number", 64)
attrs.UpsertBool("bool", true)
attrs.UpsertString("env", "dev")
log.Body().SetStringVal("something happened")
}

Expand Down
64 changes: 32 additions & 32 deletions exporter/lokiexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestTenantSource(t *testing.T) {

ld := plog.NewLogs()
ld.ResourceLogs().AppendEmpty()
ld.ResourceLogs().At(0).Resource().Attributes().InsertString("tenant.name", "acme")
ld.ResourceLogs().At(0).Resource().Attributes().UpsertString("tenant.name", "acme")

tenant, err := exp.tenantSource.GetTenant(ctx, ld)
assert.NoError(t, err)
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestExporter_logDataToLoki(t *testing.T) {
ts := pcommon.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
lr.Body().SetStringVal("log message")
lr.Attributes().InsertString("not.in.config", "not allowed")
lr.Attributes().UpsertString("not.in.config", "not allowed")
lr.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand All @@ -391,9 +391,9 @@ func TestExporter_logDataToLoki(t *testing.T) {
ts := pcommon.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
lr.Body().SetStringVal("log message")
lr.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr.Attributes().InsertString("severity", "info")
lr.Attributes().InsertString("random.attribute", "random attribute")
lr.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer")
lr.Attributes().UpsertString("severity", "info")
lr.Attributes().UpsertString("random.attribute", "random attribute")
lr.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand All @@ -409,16 +409,16 @@ func TestExporter_logDataToLoki(t *testing.T) {
sl := logs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty()
lr1 := sl.LogRecords().AppendEmpty()
lr1.Body().SetStringVal("log message 1")
lr1.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr1.Attributes().InsertString(conventions.AttributeK8SClusterName, "mycluster")
lr1.Attributes().InsertString("severity", "info")
lr1.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer")
lr1.Attributes().UpsertString(conventions.AttributeK8SClusterName, "mycluster")
lr1.Attributes().UpsertString("severity", "info")
lr1.SetTimestamp(ts)

lr2 := sl.LogRecords().AppendEmpty()
lr2.Body().SetStringVal("log message 2")
lr2.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr2.Attributes().InsertString(conventions.AttributeK8SClusterName, "mycluster")
lr2.Attributes().InsertString("severity", "info")
lr2.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer")
lr2.Attributes().UpsertString(conventions.AttributeK8SClusterName, "mycluster")
lr2.Attributes().UpsertString("severity", "info")
lr2.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand All @@ -435,16 +435,16 @@ func TestExporter_logDataToLoki(t *testing.T) {

lr1 := sl.LogRecords().AppendEmpty()
lr1.Body().SetStringVal("log message 1")
lr1.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer1")
lr1.Attributes().InsertString(conventions.AttributeK8SClusterName, "mycluster1")
lr1.Attributes().InsertString("severity", "debug")
lr1.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer1")
lr1.Attributes().UpsertString(conventions.AttributeK8SClusterName, "mycluster1")
lr1.Attributes().UpsertString("severity", "debug")
lr1.SetTimestamp(ts)

lr2 := sl.LogRecords().AppendEmpty()
lr2.Body().SetStringVal("log message 2")
lr2.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer2")
lr2.Attributes().InsertString(conventions.AttributeK8SClusterName, "mycluster2")
lr2.Attributes().InsertString("severity", "error")
lr2.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer2")
lr2.Attributes().UpsertString(conventions.AttributeK8SClusterName, "mycluster2")
lr2.Attributes().UpsertString("severity", "error")
lr2.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand All @@ -459,11 +459,11 @@ func TestExporter_logDataToLoki(t *testing.T) {
logs := plog.NewLogs()
ts := pcommon.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := logs.ResourceLogs().AppendEmpty()
lr.Resource().Attributes().InsertString("not.in.config", "not allowed")
lr.Resource().Attributes().UpsertString("not.in.config", "not allowed")

lri := lr.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
lri.Body().SetStringVal("log message")
lri.Attributes().InsertString("not.in.config", "not allowed")
lri.Attributes().UpsertString("not.in.config", "not allowed")
lri.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand All @@ -476,13 +476,13 @@ func TestExporter_logDataToLoki(t *testing.T) {
logs := plog.NewLogs()
ts := pcommon.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := logs.ResourceLogs().AppendEmpty()
lr.Resource().Attributes().InsertString("resource.name", "myresource")
lr.Resource().Attributes().UpsertString("resource.name", "myresource")

lri := lr.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
lri.Body().SetStringVal("log message")
lri.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lri.Attributes().InsertString("severity", "info")
lri.Attributes().InsertString("random.attribute", "random")
lri.Attributes().UpsertString(conventions.AttributeContainerName, "mycontainer")
lri.Attributes().UpsertString("severity", "info")
lri.Attributes().UpsertString("random.attribute", "random")
lri.SetTimestamp(ts)

pr, numDroppedLogs := exp.logDataToLoki(logs)
Expand Down Expand Up @@ -517,13 +517,13 @@ func TestExporter_convertAttributesToLabels(t *testing.T) {

t.Run("with attributes that match", func(t *testing.T) {
am := pcommon.NewMap()
am.InsertString(conventions.AttributeContainerName, "mycontainer")
am.InsertString(conventions.AttributeK8SClusterName, "mycluster")
am.InsertString("severity", "debug")
am.UpsertString(conventions.AttributeContainerName, "mycontainer")
am.UpsertString(conventions.AttributeK8SClusterName, "mycluster")
am.UpsertString("severity", "debug")
ram := pcommon.NewMap()
ram.InsertString("resource.name", "myresource")
ram.UpsertString("resource.name", "myresource")
// this should overwrite log attribute of the same name
ram.InsertString("severity", "info")
ram.UpsertString("severity", "info")

ls, _ := exp.convertAttributesAndMerge(am, ram)
expLs := model.LabelSet{
Expand All @@ -537,31 +537,31 @@ func TestExporter_convertAttributesToLabels(t *testing.T) {

t.Run("with attribute matches and the value is a boolean", func(t *testing.T) {
am := pcommon.NewMap()
am.InsertBool("severity", false)
am.UpsertBool("severity", false)
ram := pcommon.NewMap()
ls, _ := exp.convertAttributesAndMerge(am, ram)
require.Nil(t, ls)
})

t.Run("with attribute that matches and the value is a double", func(t *testing.T) {
am := pcommon.NewMap()
am.InsertDouble("severity", float64(0))
am.UpsertDouble("severity", float64(0))
ram := pcommon.NewMap()
ls, _ := exp.convertAttributesAndMerge(am, ram)
require.Nil(t, ls)
})

t.Run("with attribute that matches and the value is an int", func(t *testing.T) {
am := pcommon.NewMap()
am.InsertInt("severity", 0)
am.UpsertInt("severity", 0)
ram := pcommon.NewMap()
ls, _ := exp.convertAttributesAndMerge(am, ram)
require.Nil(t, ls)
})

t.Run("with attribute that matches and the value is null", func(t *testing.T) {
am := pcommon.NewMap()
am.Insert("severity", pcommon.NewValueEmpty())
am.UpsertEmpty("severity")
ram := pcommon.NewMap()
ls, _ := exp.convertAttributesAndMerge(am, ram)
require.Nil(t, ls)
Expand Down
4 changes: 2 additions & 2 deletions exporter/lokiexporter/internal/tenant/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAttributeTenantSourceSuccess(t *testing.T) {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // who's on first
logs.ResourceLogs().AppendEmpty() // what's on second
logs.ResourceLogs().At(0).Resource().Attributes().InsertString("tenant.id", "acme")
logs.ResourceLogs().At(0).Resource().Attributes().UpsertString("tenant.id", "acme")

// test
tenant, err := ts.GetTenant(context.Background(), logs)
Expand All @@ -46,7 +46,7 @@ func TestAttributeTenantSourceNotFound(t *testing.T) {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // who's on first
logs.ResourceLogs().AppendEmpty() // what's on second
logs.ResourceLogs().At(0).Resource().Attributes().InsertString("not.tenant.id", "acme")
logs.ResourceLogs().At(0).Resource().Attributes().UpsertString("not.tenant.id", "acme")

// test
tenant, err := ts.GetTenant(context.Background(), logs)
Expand Down
Loading