Skip to content

Commit

Permalink
Replace usages of Map.Insert*
Browse files Browse the repository at this point in the history
This change replaces usages on Map.Insert* with Map.Upsert* where there is no data under the key.

This covers only part of the usages. More changes will be added in next PRs
  • Loading branch information
dmitryax committed Aug 30, 2022
1 parent d67fbce commit 2ad6830
Show file tree
Hide file tree
Showing 85 changed files with 934 additions and 1,011 deletions.
10 changes: 5 additions & 5 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ func (m *metric{{ $name.Render }}) recordDataPoint(start pcommon.Timestamp, ts p
dp.Set{{ $metric.Data.MetricValueType }}Val(val)
{{- range $metric.Attributes }}
{{- if eq (attributeInfo .).Type.Primitive "bool" }}
dp.Attributes().InsertBool("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
dp.Attributes().UpsertBool("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
{{- else if eq (attributeInfo .).Type.Primitive "int64" }}
dp.Attributes().InsertInt("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
dp.Attributes().UpsertInt("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
{{- else if eq (attributeInfo .).Type.Primitive "float64" }}
dp.Attributes().InsertDouble("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
dp.Attributes().UpsertDouble("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
{{- else if eq (attributeInfo .).Type.Primitive "[]byte" }}
dp.Attributes().InsertBytes("{{ attributeKey .}}", pcommon.NewImmutableByteSlice({{ .RenderUnexported }}AttributeValue))
dp.Attributes().UpsertBytes("{{ attributeKey .}}", pcommon.NewImmutableByteSlice({{ .RenderUnexported }}AttributeValue))
{{- else }}
dp.Attributes().InsertString("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
dp.Attributes().UpsertString("{{ attributeKey .}}", {{ .RenderUnexported }}AttributeValue)
{{- end }}
{{- end }}
}
Expand Down
8 changes: 4 additions & 4 deletions exporter/alibabacloudlogserviceexporter/logs_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func createSimpleLogData(numberOfLogs int) plog.Logs {
ts := pcommon.Timestamp(int64(i) * time.Millisecond.Nanoseconds())
logRecord := sl.LogRecords().AppendEmpty()
logRecord.Body().SetStringVal("mylog")
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().UpsertString(conventions.AttributeServiceName, "myapp")
logRecord.Attributes().UpsertString("my-label", "myapp-type")
logRecord.Attributes().UpsertString(conventions.AttributeHostName, "myhost")
logRecord.Attributes().UpsertString("custom", "custom")
logRecord.SetTimestamp(ts)
}
sl.LogRecords().AppendEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,24 @@ import (
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)

func getComplexAttributeValueMap() pcommon.Value {
mapVal := pcommon.NewValueMap()
mapValReal := mapVal.MapVal()
mapValReal.InsertBool("result", true)
mapValReal.InsertString("status", "ok")
mapValReal.InsertDouble("value", 1.3)
mapValReal.InsertInt("code", 200)
mapValReal.Insert("null", pcommon.NewValueEmpty())
arrayVal := pcommon.NewValueSlice()
arrayVal.SliceVal().AppendEmpty().SetStringVal("array")
mapValReal.Insert("array", arrayVal)

subMapVal := pcommon.NewValueMap()
subMapVal.MapVal().InsertString("data", "hello world")
mapValReal.Insert("map", subMapVal)

mapValReal.InsertString("status", "ok")
return mapVal
func fillComplexAttributeValueMap(m pcommon.Map) {
m.UpsertBool("result", true)
m.UpsertString("status", "ok")
m.UpsertDouble("value", 1.3)
m.UpsertInt("code", 200)
m.UpsertEmpty("null")
m.UpsertEmptySlice("array").AppendEmpty().SetStringVal("array")
m.UpsertEmptyMap("map").UpsertString("data", "hello world")
m.UpsertString("status", "ok")
}

func createLogData(numberOfLogs int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
rl := logs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().InsertString("resouceKey", "resourceValue")
rl.Resource().Attributes().InsertString(conventions.AttributeServiceName, "test-log-service-exporter")
rl.Resource().Attributes().InsertString(conventions.AttributeHostName, "test-host")
rl.Resource().Attributes().UpsertString("resouceKey", "resourceValue")
rl.Resource().Attributes().UpsertString(conventions.AttributeServiceName, "test-log-service-exporter")
rl.Resource().Attributes().UpsertString(conventions.AttributeHostName, "test-host")
sl := rl.ScopeLogs().AppendEmpty()
sl.Scope().SetName("collector")
sl.Scope().SetVersion("v0.1.0")
Expand All @@ -72,8 +63,7 @@ func createLogData(numberOfLogs int) plog.Logs {
case 4:
logRecord.Body().SetStringVal("4")
case 5:

logRecord.Attributes().Insert("map-value", getComplexAttributeValueMap())
fillComplexAttributeValueMap(logRecord.Attributes().UpsertEmptyMap("map-value"))
logRecord.Body().SetStringVal("log contents")
case 6:
arrayVal := pcommon.NewValueSlice()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func TestMetricDataToLogService(t *testing.T) {
md.ResourceMetrics().AppendEmpty() // Add an empty ResourceMetrics
rm := md.ResourceMetrics().AppendEmpty()

rm.Resource().Attributes().InsertString("labelB", "valueB")
rm.Resource().Attributes().InsertString("labelA", "valueA")
rm.Resource().Attributes().InsertString("a", "b")
rm.Resource().Attributes().UpsertString("labelB", "valueB")
rm.Resource().Attributes().UpsertString("labelA", "valueA")
rm.Resource().Attributes().UpsertString("a", "b")
sms := rm.ScopeMetrics()
sms.AppendEmpty() // Add an empty ScopeMetrics
sm := sms.AppendEmpty()
Expand All @@ -53,7 +53,7 @@ func TestMetricDataToLogService(t *testing.T) {
intGauge := intGaugeMetric.Gauge()
intGaugeDataPoints := intGauge.DataPoints()
intGaugeDataPoint := intGaugeDataPoints.AppendEmpty()
intGaugeDataPoint.Attributes().InsertString("innerLabel", "innerValue")
intGaugeDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
intGaugeDataPoint.SetIntVal(10)
intGaugeDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))

Expand All @@ -63,7 +63,7 @@ func TestMetricDataToLogService(t *testing.T) {
doubleGauge := doubleGaugeMetric.Gauge()
doubleGaugeDataPoints := doubleGauge.DataPoints()
doubleGaugeDataPoint := doubleGaugeDataPoints.AppendEmpty()
doubleGaugeDataPoint.Attributes().InsertString("innerLabel", "innerValue")
doubleGaugeDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
doubleGaugeDataPoint.SetDoubleVal(10.1)
doubleGaugeDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))

Expand All @@ -73,7 +73,7 @@ func TestMetricDataToLogService(t *testing.T) {
intSum := intSumMetric.Sum()
intSumDataPoints := intSum.DataPoints()
intSumDataPoint := intSumDataPoints.AppendEmpty()
intSumDataPoint.Attributes().InsertString("innerLabel", "innerValue")
intSumDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
intSumDataPoint.SetIntVal(11)
intSumDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))

Expand All @@ -83,7 +83,7 @@ func TestMetricDataToLogService(t *testing.T) {
doubleSum := doubleSumMetric.Sum()
doubleSumDataPoints := doubleSum.DataPoints()
doubleSumDataPoint := doubleSumDataPoints.AppendEmpty()
doubleSumDataPoint.Attributes().InsertString("innerLabel", "innerValue")
doubleSumDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
doubleSumDataPoint.SetDoubleVal(10.1)
doubleSumDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))

Expand All @@ -93,7 +93,7 @@ func TestMetricDataToLogService(t *testing.T) {
doubleHistogram := doubleHistogramMetric.Histogram()
doubleHistogramDataPoints := doubleHistogram.DataPoints()
doubleHistogramDataPoint := doubleHistogramDataPoints.AppendEmpty()
doubleHistogramDataPoint.Attributes().InsertString("innerLabel", "innerValue")
doubleHistogramDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
doubleHistogramDataPoint.SetCount(2)
doubleHistogramDataPoint.SetSum(10.1)
doubleHistogramDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))
Expand All @@ -109,7 +109,7 @@ func TestMetricDataToLogService(t *testing.T) {
doubleSummaryDataPoint.SetCount(2)
doubleSummaryDataPoint.SetSum(10.1)
doubleSummaryDataPoint.SetTimestamp(pcommon.Timestamp(100_000_000))
doubleSummaryDataPoint.Attributes().InsertString("innerLabel", "innerValue")
doubleSummaryDataPoint.Attributes().UpsertString("innerLabel", "innerValue")
quantileVal := doubleSummaryDataPoint.QuantileValues().AppendEmpty()
quantileVal.SetValue(10.2)
quantileVal.SetQuantile(0.9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func constructSpanData() ptrace.Traces {

func fillResource(resource pcommon.Resource) {
attrs := resource.Attributes()
attrs.InsertString(conventions.AttributeServiceName, "signup_aggregator")
attrs.InsertString(conventions.AttributeHostName, "xxx.et15")
attrs.InsertString(conventions.AttributeContainerName, "signup_aggregator")
attrs.InsertString(conventions.AttributeContainerImageName, "otel/signupaggregator")
attrs.InsertString(conventions.AttributeContainerImageTag, "v1")
attrs.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
attrs.InsertString(conventions.AttributeCloudAccountID, "999999998")
attrs.InsertString(conventions.AttributeCloudRegion, "us-west-2")
attrs.InsertString(conventions.AttributeCloudAvailabilityZone, "us-west-1b")
attrs.UpsertString(conventions.AttributeServiceName, "signup_aggregator")
attrs.UpsertString(conventions.AttributeHostName, "xxx.et15")
attrs.UpsertString(conventions.AttributeContainerName, "signup_aggregator")
attrs.UpsertString(conventions.AttributeContainerImageName, "otel/signupaggregator")
attrs.UpsertString(conventions.AttributeContainerImageTag, "v1")
attrs.UpsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
attrs.UpsertString(conventions.AttributeCloudAccountID, "999999998")
attrs.UpsertString(conventions.AttributeCloudRegion, "us-west-2")
attrs.UpsertString(conventions.AttributeCloudAvailabilityZone, "us-west-1b")
}

func fillHTTPClientSpan(span ptrace.Span) {
Expand All @@ -131,11 +131,11 @@ func fillHTTPClientSpan(span ptrace.Span) {
event := span.Events().AppendEmpty()
event.SetName("event")
event.SetTimestamp(1024)
event.Attributes().InsertString("key", "value")
event.Attributes().UpsertString("key", "value")

link := span.Links().AppendEmpty()
link.SetTraceState("link:state")
link.Attributes().InsertString("link", "true")
link.Attributes().UpsertString("link", "true")

status := span.Status()
status.SetCode(1)
Expand Down Expand Up @@ -169,11 +169,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
22 changes: 11 additions & 11 deletions exporter/awscloudwatchlogsexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func BenchmarkLogToCWLog(b *testing.B) {

func testResource() pcommon.Resource {
resource := pcommon.NewResource()
resource.Attributes().InsertString("host", "abc123")
resource.Attributes().InsertInt("node", 5)
resource.Attributes().UpsertString("host", "abc123")
resource.Attributes().UpsertInt("node", 5)
return resource
}

Expand All @@ -130,8 +130,8 @@ func testLogRecord() plog.LogRecord {
record.SetSeverityText("debug")
record.SetDroppedAttributesCount(4)
record.Body().SetStringVal("hello world")
record.Attributes().InsertInt("key1", 1)
record.Attributes().InsertString("key2", "attr2")
record.Attributes().UpsertInt("key1", 1)
record.Attributes().UpsertString("key2", "attr2")
record.SetTraceID(pcommon.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}))
record.SetSpanID(pcommon.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
record.FlagsStruct().SetIsSampled(true)
Expand All @@ -145,8 +145,8 @@ func testLogRecordWithoutTrace() plog.LogRecord {
record.SetSeverityText("debug")
record.SetDroppedAttributesCount(4)
record.Body().SetStringVal("hello world")
record.Attributes().InsertInt("key1", 1)
record.Attributes().InsertString("key2", "attr2")
record.Attributes().UpsertInt("key1", 1)
record.Attributes().UpsertString("key2", "attr2")
record.SetTimestamp(1609719139000000)
return record
}
Expand Down Expand Up @@ -182,11 +182,11 @@ func TestAttrValue(t *testing.T) {
value: func() pcommon.Value {
mAttr := pcommon.NewValueMap()
m := mAttr.MapVal()
m.InsertString("key1", "value1")
m.Insert("key2", pcommon.NewValueEmpty())
m.InsertBool("key3", true)
m.InsertInt("key4", 4)
m.InsertDouble("key5", 5.6)
m.UpsertString("key1", "value1")
m.Upsert("key2", pcommon.NewValueEmpty())
m.UpsertBool("key3", true)
m.UpsertInt("key4", 4)
m.UpsertDouble("key5", 5.6)
return mAttr
}(),
want: map[string]interface{}{
Expand Down
19 changes: 9 additions & 10 deletions exporter/awsxrayexporter/awsxray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,15 @@ func constructW3CFormatTraceSpanData(ispans ptrace.ScopeSpans) {

func constructResource() pcommon.Resource {
resource := pcommon.NewResource()
attrs := pcommon.NewMap()
attrs.InsertString(conventions.AttributeServiceName, "signup_aggregator")
attrs.InsertString(conventions.AttributeContainerName, "signup_aggregator")
attrs.InsertString(conventions.AttributeContainerImageName, "otel/signupaggregator")
attrs.InsertString(conventions.AttributeContainerImageTag, "v1")
attrs.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
attrs.InsertString(conventions.AttributeCloudAccountID, "999999998")
attrs.InsertString(conventions.AttributeCloudRegion, "us-west-2")
attrs.InsertString(conventions.AttributeCloudAvailabilityZone, "us-west-1b")
attrs.CopyTo(resource.Attributes())
attrs := resource.Attributes()
attrs.UpsertString(conventions.AttributeServiceName, "signup_aggregator")
attrs.UpsertString(conventions.AttributeContainerName, "signup_aggregator")
attrs.UpsertString(conventions.AttributeContainerImageName, "otel/signupaggregator")
attrs.UpsertString(conventions.AttributeContainerImageTag, "v1")
attrs.UpsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
attrs.UpsertString(conventions.AttributeCloudAccountID, "999999998")
attrs.UpsertString(conventions.AttributeCloudRegion, "us-west-2")
attrs.UpsertString(conventions.AttributeCloudAvailabilityZone, "us-west-1b")
return resource
}

Expand Down
24 changes: 9 additions & 15 deletions exporter/awsxrayexporter/internal/translator/cause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,22 @@ func TestCauseWithExceptions(t *testing.T) {

event1 := span.Events().AppendEmpty()
event1.SetName(ExceptionEventName)
attributes := pcommon.NewMap()
attributes.InsertString(conventions.AttributeExceptionType, "java.lang.IllegalStateException")
attributes.InsertString(conventions.AttributeExceptionMessage, "bad state")
attributes.InsertString(conventions.AttributeExceptionStacktrace, `java.lang.IllegalStateException: state is not legal
event1.Attributes().UpsertString(conventions.AttributeExceptionType, "java.lang.IllegalStateException")
event1.Attributes().UpsertString(conventions.AttributeExceptionMessage, "bad state")
event1.Attributes().UpsertString(conventions.AttributeExceptionStacktrace, `java.lang.IllegalStateException: state is not legal
at io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.recordException(RecordEventsReadableSpanTest.java:626)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Caused by: java.lang.IllegalArgumentException: bad argument`)
attributes.CopyTo(event1.Attributes())

event2 := span.Events().AppendEmpty()
event2.SetName(ExceptionEventName)
attributes = pcommon.NewMap()
attributes.InsertString(conventions.AttributeExceptionType, "EmptyError")
attributes.CopyTo(event2.Attributes())
event2.Attributes().UpsertString(conventions.AttributeExceptionType, "EmptyError")

filtered, _ := makeHTTP(span)

res := pcommon.NewResource()
res.Attributes().InsertString(conventions.AttributeTelemetrySDKLanguage, "java")
res.Attributes().UpsertString(conventions.AttributeTelemetrySDKLanguage, "java")
isError, isFault, isThrottle, filteredResult, cause := makeCause(span, filtered, res)

assert.True(t, isFault)
Expand Down Expand Up @@ -97,16 +93,14 @@ Caused by: java.lang.IllegalArgumentException: bad argument`

event1 := span.Events().AppendEmpty()
event1.SetName(ExceptionEventName)
attributes := pcommon.NewMap()
attributes.InsertString(conventions.AttributeExceptionType, "java.lang.IllegalStateException")
attributes.InsertString(conventions.AttributeExceptionMessage, "bad state")
attributes.InsertString(conventions.AttributeExceptionStacktrace, exceptionStack)
attributes.CopyTo(event1.Attributes())
event1.Attributes().UpsertString(conventions.AttributeExceptionType, "java.lang.IllegalStateException")
event1.Attributes().UpsertString(conventions.AttributeExceptionMessage, "bad state")
event1.Attributes().UpsertString(conventions.AttributeExceptionStacktrace, exceptionStack)

filtered, _ := makeHTTP(span)

res := pcommon.NewResource()
res.Attributes().InsertString(conventions.AttributeTelemetrySDKLanguage, "java")
res.Attributes().UpsertString(conventions.AttributeTelemetrySDKLanguage, "java")
isError, isFault, isThrottle, filteredResult, cause := makeCause(span, filtered, res)

assert.False(t, isFault)
Expand Down
2 changes: 1 addition & 1 deletion exporter/azuremonitorexporter/trace_to_envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func TestRPCClientSpanToRemoteDependencyData(t *testing.T) {
// test RPC error using the new rpc.grpc.status_code attribute
span.Status().SetCode(ptrace.StatusCodeError)
span.Status().SetMessage("Resource exhausted")
spanAttributes.InsertInt(attributeRPCGRPCStatusCode, 8)
spanAttributes.UpsertInt(attributeRPCGRPCStatusCode, 8)

envelope, _ = spanToEnvelope(defaultResource, defaultInstrumentationLibrary, span, zap.NewNop())
data = envelope.Data.(*contracts.Data).BaseData.(*contracts.RemoteDependencyData)
Expand Down
6 changes: 3 additions & 3 deletions exporter/datadogexporter/internal/metrics/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func TestTagsMetrics(t *testing.T) {
conventions.AttributeAWSECSLaunchtype: conventions.AttributeAWSECSLaunchtypeFargate,
})
baseAttrs.CopyTo(rm.Resource().Attributes())
rm.Resource().Attributes().InsertString(conventions.AttributeAWSECSTaskARN, "task-arn-1")
rm.Resource().Attributes().UpsertString(conventions.AttributeAWSECSTaskARN, "task-arn-1")

rm = rms.AppendEmpty()
baseAttrs.CopyTo(rm.Resource().Attributes())
rm.Resource().Attributes().InsertString(conventions.AttributeAWSECSTaskARN, "task-arn-2")
rm.Resource().Attributes().UpsertString(conventions.AttributeAWSECSTaskARN, "task-arn-2")

rm = rms.AppendEmpty()
baseAttrs.CopyTo(rm.Resource().Attributes())
rm.Resource().Attributes().InsertString(conventions.AttributeAWSECSTaskARN, "task-arn-3")
rm.Resource().Attributes().UpsertString(conventions.AttributeAWSECSTaskARN, "task-arn-3")

logger, _ := zap.NewProduction()
tr := newTranslator(t, logger)
Expand Down
5 changes: 1 addition & 4 deletions exporter/datadogexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,6 @@ func genTraces(traceID pcommon.TraceID, attrs map[string]interface{}) ptrace.Tra
if attrs == nil {
return traces
}
pcommon.NewMapFromRaw(attrs).Range(func(k string, v pcommon.Value) bool {
rspans.Resource().Attributes().Insert(k, v)
return true
})
pcommon.NewMapFromRaw(attrs).CopyTo(rspans.Resource().Attributes())
return traces
}
Loading

0 comments on commit 2ad6830

Please sign in to comment.