Skip to content

Commit

Permalink
Fix usage of CopyTo/Append by passing the destination to the function…
Browse files Browse the repository at this point in the history
… calls

Fixes couple of bugs from PR#3322, see comments in that PR.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jun 7, 2021
1 parent 8042a03 commit 5cf3315
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 531 deletions.
57 changes: 0 additions & 57 deletions internal/goldendataset/generator_commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,10 @@ package goldendataset

import (
"encoding/csv"
"io"
"os"
"path/filepath"

"github.com/spf13/cast"

"go.opentelemetry.io/collector/consumer/pdata"
)

func convertMapToAttributeMap(attrsMap map[string]interface{}) *pdata.AttributeMap {
attributeMap := pdata.NewAttributeMap()
if attrsMap == nil {
return nil
}
for key, value := range attrsMap {
attributeMap.Insert(key, convertToAttributeValue(value))
}
return &attributeMap
}

func convertToAttributeValue(value interface{}) pdata.AttributeValue {
var newValue pdata.AttributeValue
switch val := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
newValue = pdata.NewAttributeValueInt(cast.ToInt64(val))
case float32, float64:
newValue = pdata.NewAttributeValueDouble(cast.ToFloat64(val))
case bool:
newValue = pdata.NewAttributeValueBool(cast.ToBool(val))
case pdata.AttributeMap:
newValue = pdata.NewAttributeValueMap()
val.CopyTo(newValue.MapVal())
case pdata.AnyValueArray:
newValue = pdata.NewAttributeValueArray()
val.CopyTo(newValue.ArrayVal())
case pdata.AttributeValue:
newValue = val
default:
newValue = pdata.NewAttributeValueString(val.(string))
}
return newValue
}

func loadPictOutputFile(fileName string) ([][]string, error) {
file, err := os.Open(filepath.Clean(fileName))
if err != nil {
Expand All @@ -76,21 +37,3 @@ func loadPictOutputFile(fileName string) ([][]string, error) {

return reader.ReadAll()
}

func generatePDataTraceID(random io.Reader) pdata.TraceID {
var r [16]byte
_, err := random.Read(r[:])
if err != nil {
panic(err)
}
return pdata.NewTraceID(r)
}

func generatePDataSpanID(random io.Reader) pdata.SpanID {
var r [8]byte
_, err := random.Read(r[:])
if err != nil {
panic(err)
}
return pdata.NewSpanID(r)
}
3 changes: 0 additions & 3 deletions internal/goldendataset/pict_tracing_input_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const (
type PICTInputResource string

const (
ResourceNil PICTInputResource = "Nil"
ResourceEmpty PICTInputResource = "Empty"
ResourceVMOnPrem PICTInputResource = "VMOnPrem"
ResourceVMCloud PICTInputResource = "VMCloud"
Expand Down Expand Up @@ -112,7 +111,6 @@ const (
type PICTInputAttributes string

const (
SpanAttrNil PICTInputAttributes = "Nil"
SpanAttrEmpty PICTInputAttributes = "Empty"
SpanAttrDatabaseSQL PICTInputAttributes = "DatabaseSQL"
SpanAttrDatabaseNoSQL PICTInputAttributes = "DatabaseNoSQL"
Expand All @@ -135,7 +133,6 @@ const (
type PICTInputSpanChild string

const (
SpanChildCountNil PICTInputSpanChild = "Nil"
SpanChildCountEmpty PICTInputSpanChild = "Empty"
SpanChildCountOne PICTInputSpanChild = "One"
SpanChildCountTwo PICTInputSpanChild = "Two"
Expand Down
190 changes: 80 additions & 110 deletions internal/goldendataset/resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,140 +22,110 @@ import (
// GenerateResource generates a PData Resource object with representative attributes for the
// underlying resource type specified by the rscID input parameter.
func GenerateResource(rscID PICTInputResource) pdata.Resource {
var attrs map[string]interface{}
resource := pdata.NewResource()
switch rscID {
case ResourceNil:
attrs = generateNilAttributes()
case ResourceEmpty:
attrs = generateEmptyAttributes()
break
case ResourceVMOnPrem:
attrs = generateOnpremVMAttributes()
appendOnpremVMAttributes(resource.Attributes())
case ResourceVMCloud:
attrs = generateCloudVMAttributes()
appendCloudVMAttributes(resource.Attributes())
case ResourceK8sOnPrem:
attrs = generateOnpremK8sAttributes()
appendOnpremK8sAttributes(resource.Attributes())
case ResourceK8sCloud:
attrs = generateCloudK8sAttributes()
appendCloudK8sAttributes(resource.Attributes())
case ResourceFaas:
attrs = generateFassAttributes()
appendFassAttributes(resource.Attributes())
case ResourceExec:
attrs = generateExecAttributes()
default:
attrs = generateEmptyAttributes()
}
resource := pdata.NewResource()
attributeMap := convertMapToAttributeMap(attrs)
if attributeMap != nil {
attributeMap.CopyTo(resource.Attributes())
appendExecAttributes(resource.Attributes())
}
return resource
}

func generateNilAttributes() map[string]interface{} {
return nil
}

func generateEmptyAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
return attrMap
}

func generateOnpremVMAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeServiceName] = "customers"
attrMap[conventions.AttributeServiceNamespace] = "production"
attrMap[conventions.AttributeServiceVersion] = "semver:0.7.3"
subMap := pdata.NewAttributeMap()
subMap.InsertString("public", "tc-prod9.internal.example.com")
subMap.InsertString("internal", "172.18.36.18")
attrMap[conventions.AttributeHostName] = subMap
attrMap[conventions.AttributeHostImageID] = "661ADFA6-E293-4870-9EFA-1AA052C49F18"
attrMap[conventions.AttributeTelemetrySDKLanguage] = conventions.AttributeSDKLangValueJava
attrMap[conventions.AttributeTelemetrySDKName] = "opentelemetry"
attrMap[conventions.AttributeTelemetrySDKVersion] = "0.3.0"
return attrMap
func appendOnpremVMAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeServiceName, "customers")
attrMap.UpsertString(conventions.AttributeServiceNamespace, "production")
attrMap.UpsertString(conventions.AttributeServiceVersion, "semver:0.7.3")
subMap := pdata.NewAttributeValueMap()
subMap.MapVal().InsertString("public", "tc-prod9.internal.example.com")
subMap.MapVal().InsertString("internal", "172.18.36.18")
attrMap.Upsert(conventions.AttributeHostName, subMap)
attrMap.UpsertString(conventions.AttributeHostImageID, "661ADFA6-E293-4870-9EFA-1AA052C49F18")
attrMap.UpsertString(conventions.AttributeTelemetrySDKLanguage, conventions.AttributeSDKLangValueJava)
attrMap.UpsertString(conventions.AttributeTelemetrySDKName, "opentelemetry")
attrMap.UpsertString(conventions.AttributeTelemetrySDKVersion, "0.3.0")
}

func generateCloudVMAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeServiceName] = "shoppingcart"
attrMap[conventions.AttributeServiceName] = "customers"
attrMap[conventions.AttributeServiceNamespace] = "production"
attrMap[conventions.AttributeServiceVersion] = "semver:0.7.3"
attrMap[conventions.AttributeTelemetrySDKLanguage] = conventions.AttributeSDKLangValueJava
attrMap[conventions.AttributeTelemetrySDKName] = "opentelemetry"
attrMap[conventions.AttributeTelemetrySDKVersion] = "0.3.0"
attrMap[conventions.AttributeHostID] = "57e8add1f79a454bae9fb1f7756a009a"
attrMap[conventions.AttributeHostName] = "env-check"
attrMap[conventions.AttributeHostImageID] = "5.3.0-1020-azure"
attrMap[conventions.AttributeHostType] = "B1ms"
attrMap[conventions.AttributeCloudProvider] = "azure"
attrMap[conventions.AttributeCloudAccount] = "2f5b8278-4b80-4930-a6bb-d86fc63a2534"
attrMap[conventions.AttributeCloudRegion] = "South Central US"
return attrMap
func appendCloudVMAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeServiceName, "shoppingcart")
attrMap.UpsertString(conventions.AttributeServiceName, "customers")
attrMap.UpsertString(conventions.AttributeServiceNamespace, "production")
attrMap.UpsertString(conventions.AttributeServiceVersion, "semver:0.7.3")
attrMap.UpsertString(conventions.AttributeTelemetrySDKLanguage, conventions.AttributeSDKLangValueJava)
attrMap.UpsertString(conventions.AttributeTelemetrySDKName, "opentelemetry")
attrMap.UpsertString(conventions.AttributeTelemetrySDKVersion, "0.3.0")
attrMap.UpsertString(conventions.AttributeHostID, "57e8add1f79a454bae9fb1f7756a009a")
attrMap.UpsertString(conventions.AttributeHostName, "env-check")
attrMap.UpsertString(conventions.AttributeHostImageID, "5.3.0-1020-azure")
attrMap.UpsertString(conventions.AttributeHostType, "B1ms")
attrMap.UpsertString(conventions.AttributeCloudProvider, "azure")
attrMap.UpsertString(conventions.AttributeCloudAccount, "2f5b8278-4b80-4930-a6bb-d86fc63a2534")
attrMap.UpsertString(conventions.AttributeCloudRegion, "South Central US")
}

func generateOnpremK8sAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeContainerName] = "cert-manager"
attrMap[conventions.AttributeContainerImage] = "quay.io/jetstack/cert-manager-controller:v0.14.2"
attrMap[conventions.AttributeK8sCluster] = "docker-desktop"
attrMap[conventions.AttributeK8sNamespace] = "cert-manager"
attrMap[conventions.AttributeK8sDeployment] = "cm-1-cert-manager"
attrMap[conventions.AttributeK8sPod] = "cm-1-cert-manager-6448b4949b-t2jtd"
attrMap[conventions.AttributeHostName] = "docker-desktop"
return attrMap
func appendOnpremK8sAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeContainerName, "cert-manager")
attrMap.UpsertString(conventions.AttributeContainerImage, "quay.io/jetstack/cert-manager-controller:v0.14.2")
attrMap.UpsertString(conventions.AttributeK8sCluster, "docker-desktop")
attrMap.UpsertString(conventions.AttributeK8sNamespace, "cert-manager")
attrMap.UpsertString(conventions.AttributeK8sDeployment, "cm-1-cert-manager")
attrMap.UpsertString(conventions.AttributeK8sPod, "cm-1-cert-manager-6448b4949b-t2jtd")
attrMap.UpsertString(conventions.AttributeHostName, "docker-desktop")
}

func generateCloudK8sAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeContainerName] = "otel-collector"
attrMap[conventions.AttributeContainerImage] = "otel/opentelemetry-collector-contrib"
attrMap[conventions.AttributeContainerTag] = "0.4.0"
attrMap[conventions.AttributeK8sCluster] = "erp-dev"
attrMap[conventions.AttributeK8sNamespace] = "monitoring"
attrMap[conventions.AttributeK8sDeployment] = "otel-collector"
attrMap[conventions.AttributeK8sDeploymentUID] = "4D614B27-EDAF-409B-B631-6963D8F6FCD4"
attrMap[conventions.AttributeK8sReplicaSet] = "otel-collector-2983fd34"
attrMap[conventions.AttributeK8sReplicaSetUID] = "EC7D59EF-D5B6-48B7-881E-DA6B7DD539B6"
attrMap[conventions.AttributeK8sPod] = "otel-collector-6484db5844-c6f9m"
attrMap[conventions.AttributeK8sPodUID] = "FDFD941E-2A7A-4945-B601-88DD486161A4"
attrMap[conventions.AttributeHostID] = "ec2e3fdaffa294348bdf355156b94cda"
attrMap[conventions.AttributeHostName] = "10.99.118.157"
attrMap[conventions.AttributeHostImageID] = "ami-011c865bf7da41a9d"
attrMap[conventions.AttributeHostType] = "m5.xlarge"
attrMap[conventions.AttributeCloudProvider] = "aws"
attrMap[conventions.AttributeCloudAccount] = "12345678901"
attrMap[conventions.AttributeCloudRegion] = "us-east-1"
attrMap[conventions.AttributeCloudAvailabilityZone] = "us-east-1c"
return attrMap
func appendCloudK8sAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeContainerName, "otel-collector")
attrMap.UpsertString(conventions.AttributeContainerImage, "otel/opentelemetry-collector-contrib")
attrMap.UpsertString(conventions.AttributeContainerTag, "0.4.0")
attrMap.UpsertString(conventions.AttributeK8sCluster, "erp-dev")
attrMap.UpsertString(conventions.AttributeK8sNamespace, "monitoring")
attrMap.UpsertString(conventions.AttributeK8sDeployment, "otel-collector")
attrMap.UpsertString(conventions.AttributeK8sDeploymentUID, "4D614B27-EDAF-409B-B631-6963D8F6FCD4")
attrMap.UpsertString(conventions.AttributeK8sReplicaSet, "otel-collector-2983fd34")
attrMap.UpsertString(conventions.AttributeK8sReplicaSetUID, "EC7D59EF-D5B6-48B7-881E-DA6B7DD539B6")
attrMap.UpsertString(conventions.AttributeK8sPod, "otel-collector-6484db5844-c6f9m")
attrMap.UpsertString(conventions.AttributeK8sPodUID, "FDFD941E-2A7A-4945-B601-88DD486161A4")
attrMap.UpsertString(conventions.AttributeHostID, "ec2e3fdaffa294348bdf355156b94cda")
attrMap.UpsertString(conventions.AttributeHostName, "10.99.118.157")
attrMap.UpsertString(conventions.AttributeHostImageID, "ami-011c865bf7da41a9d")
attrMap.UpsertString(conventions.AttributeHostType, "m5.xlarge")
attrMap.UpsertString(conventions.AttributeCloudProvider, "aws")
attrMap.UpsertString(conventions.AttributeCloudAccount, "12345678901")
attrMap.UpsertString(conventions.AttributeCloudRegion, "us-east-1")
attrMap.UpsertString(conventions.AttributeCloudAvailabilityZone, "us-east-1c")
}

func generateFassAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeFaasID] = "https://us-central1-dist-system-demo.cloudfunctions.net/env-vars-print"
attrMap[conventions.AttributeFaasName] = "env-vars-print"
attrMap[conventions.AttributeFaasVersion] = "semver:1.0.0"
attrMap[conventions.AttributeCloudProvider] = "gcp"
attrMap[conventions.AttributeCloudAccount] = "opentelemetry"
attrMap[conventions.AttributeCloudRegion] = "us-central1"
attrMap[conventions.AttributeCloudAvailabilityZone] = "us-central1-a"
return attrMap
func appendFassAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeFaasID, "https://us-central1-dist-system-demo.cloudfunctions.net/env-vars-print")
attrMap.UpsertString(conventions.AttributeFaasName, "env-vars-print")
attrMap.UpsertString(conventions.AttributeFaasVersion, "semver:1.0.0")
attrMap.UpsertString(conventions.AttributeCloudProvider, "gcp")
attrMap.UpsertString(conventions.AttributeCloudAccount, "opentelemetry")
attrMap.UpsertString(conventions.AttributeCloudRegion, "us-central1")
attrMap.UpsertString(conventions.AttributeCloudAvailabilityZone, "us-central1-a")
}

func generateExecAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeProcessExecutableName] = "otelcol"
func appendExecAttributes(attrMap pdata.AttributeMap) {
attrMap.UpsertString(conventions.AttributeProcessExecutableName, "otelcol")
parts := pdata.NewAttributeValueArray()
parts.ArrayVal().Append(pdata.NewAttributeValueString("otelcol"))
parts.ArrayVal().Append(pdata.NewAttributeValueString("--config=/etc/otel-collector-config.yaml"))
parts.ArrayVal().Append(pdata.NewAttributeValueString("--mem-ballast-size-mib=683"))
attrMap["conventions.AttributeProcessCommandLine"] = parts
attrMap[conventions.AttributeProcessExecutablePath] = "/usr/local/bin/otelcol"
attrMap[conventions.AttributeProcessID] = 2020
attrMap[conventions.AttributeProcessOwner] = "otel"
attrMap[conventions.AttributeOSType] = "LINUX"
attrMap[conventions.AttributeOSDescription] =
"Linux ubuntu 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux"
return attrMap
attrMap.Upsert(conventions.AttributeProcessCommandLine, parts)
attrMap.UpsertString(conventions.AttributeProcessExecutablePath, "/usr/local/bin/otelcol")
attrMap.UpsertInt(conventions.AttributeProcessID, 2020)
attrMap.UpsertString(conventions.AttributeProcessOwner, "otel")
attrMap.UpsertString(conventions.AttributeOSType, "LINUX")
attrMap.UpsertString(conventions.AttributeOSDescription,
"Linux ubuntu 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux")
}
5 changes: 2 additions & 3 deletions internal/goldendataset/resource_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
)

func TestGenerateResource(t *testing.T) {
resourceIds := []PICTInputResource{ResourceNil, ResourceEmpty, ResourceVMOnPrem, ResourceVMCloud, ResourceK8sOnPrem,
ResourceK8sCloud, ResourceFaas, ResourceExec}
resourceIds := []PICTInputResource{ResourceEmpty, ResourceVMOnPrem, ResourceVMCloud, ResourceK8sOnPrem, ResourceK8sCloud, ResourceFaas, ResourceExec}
for _, rscID := range resourceIds {
rsc := GenerateResource(rscID)
if rscID == ResourceNil || rscID == ResourceEmpty {
if rscID == ResourceEmpty {
assert.Equal(t, 0, rsc.Attributes().Len())
} else {
assert.True(t, rsc.Attributes().Len() > 0)
Expand Down
Loading

0 comments on commit 5cf3315

Please sign in to comment.