Skip to content

Commit

Permalink
Remove the proto dependency in goldendataset for traces (open-telemet…
Browse files Browse the repository at this point in the history
…ry#3322)

* Remove the proto dependency in goldendataset for traces

* revert changelog

* update changelog

* Update consumer/pdata/common.go

Co-authored-by: Anthony Mirabella <[email protected]>

* Fix common.go where there is a missing "}"

* Add functions to convert proto keyvalues to AttributeMap

* further remove proto dependency

* Add PR number to CHANGELOG

* update resource_generator_test and resource_to_oc_test

* Return nil instead of empty objects for some generator functions.

Co-authored-by: Anthony Mirabella <[email protected]>
Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
3 people committed Jun 7, 2021
1 parent 6cbf7e3 commit 8042a03
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 229 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Add `doc.go` files to the consumer package and its subpackages (#3270)
- Automate triggering of doc-update on release (#3234)
- Enable Dependabot for Github Actions (#3312)
- Remove the proto dependency in `goldendataset` for traces (#3322)
- Add telemetry for dropped data due to exporter sending queue overflow (#3328)

## v0.27.0 Beta
Expand Down
65 changes: 24 additions & 41 deletions internal/goldendataset/generator_commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,41 @@ import (

"github.com/spf13/cast"

"go.opentelemetry.io/collector/internal/data"
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
"go.opentelemetry.io/collector/consumer/pdata"
)

func convertMapToAttributeKeyValues(attrsMap map[string]interface{}) []otlpcommon.KeyValue {
func convertMapToAttributeMap(attrsMap map[string]interface{}) *pdata.AttributeMap {
attributeMap := pdata.NewAttributeMap()
if attrsMap == nil {
return nil
}
attrList := make([]otlpcommon.KeyValue, len(attrsMap))
index := 0
for key, value := range attrsMap {
attrList[index] = constructAttributeKeyValue(key, value)
index++
attributeMap.Insert(key, convertToAttributeValue(value))
}
return attrList
return &attributeMap
}

func constructAttributeKeyValue(key string, value interface{}) otlpcommon.KeyValue {
var attr otlpcommon.KeyValue
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:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_IntValue{IntValue: cast.ToInt64(val)}},
}
newValue = pdata.NewAttributeValueInt(cast.ToInt64(val))
case float32, float64:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_DoubleValue{DoubleValue: cast.ToFloat64(val)}},
}
newValue = pdata.NewAttributeValueDouble(cast.ToFloat64(val))
case bool:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_BoolValue{BoolValue: cast.ToBool(val)}},
}
case *otlpcommon.ArrayValue:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: val}},
}
case *otlpcommon.KeyValueList:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: val}},
}
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:
attr = otlpcommon.KeyValue{
Key: key,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: val.(string)}},
}
newValue = pdata.NewAttributeValueString(val.(string))
}
return attr
return newValue
}

func loadPictOutputFile(fileName string) ([][]string, error) {
Expand All @@ -94,20 +77,20 @@ func loadPictOutputFile(fileName string) ([][]string, error) {
return reader.ReadAll()
}

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

func generateSpanID(random io.Reader) data.SpanID {
func generatePDataSpanID(random io.Reader) pdata.SpanID {
var r [8]byte
_, err := random.Read(r[:])
if err != nil {
panic(err)
}
return data.NewSpanID(r)
return pdata.NewSpanID(r)
}
43 changes: 17 additions & 26 deletions internal/goldendataset/resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
package goldendataset

import (
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/conventions"
)

// GenerateResource generates a OTLP Resource object with representative attributes for the
// GenerateResource generates a PData Resource object with representative attributes for the
// underlying resource type specified by the rscID input parameter.
func GenerateResource(rscID PICTInputResource) otlpresource.Resource {
func GenerateResource(rscID PICTInputResource) pdata.Resource {
var attrs map[string]interface{}
switch rscID {
case ResourceNil:
Expand All @@ -44,16 +43,12 @@ func GenerateResource(rscID PICTInputResource) otlpresource.Resource {
default:
attrs = generateEmptyAttributes()
}
var dropped uint32
if len(attrs) < 10 {
dropped = 0
} else {
dropped = uint32(len(attrs) % 4)
}
return otlpresource.Resource{
Attributes: convertMapToAttributeKeyValues(attrs),
DroppedAttributesCount: dropped,
resource := pdata.NewResource()
attributeMap := convertMapToAttributeMap(attrs)
if attributeMap != nil {
attributeMap.CopyTo(resource.Attributes())
}
return resource
}

func generateNilAttributes() map[string]interface{} {
Expand All @@ -70,12 +65,10 @@ func generateOnpremVMAttributes() map[string]interface{} {
attrMap[conventions.AttributeServiceName] = "customers"
attrMap[conventions.AttributeServiceNamespace] = "production"
attrMap[conventions.AttributeServiceVersion] = "semver:0.7.3"
subMap := make(map[string]interface{})
subMap["public"] = "tc-prod9.internal.example.com"
subMap["internal"] = "172.18.36.18"
attrMap[conventions.AttributeHostName] = &otlpcommon.KeyValueList{
Values: convertMapToAttributeKeyValues(subMap),
}
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"
Expand Down Expand Up @@ -153,13 +146,11 @@ func generateFassAttributes() map[string]interface{} {
func generateExecAttributes() map[string]interface{} {
attrMap := make(map[string]interface{})
attrMap[conventions.AttributeProcessExecutableName] = "otelcol"
parts := make([]otlpcommon.AnyValue, 3)
parts[0] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "otelcol"}}
parts[1] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "--config=/etc/otel-collector-config.yaml"}}
parts[2] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "--mem-ballast-size-mib=683"}}
attrMap[conventions.AttributeProcessCommandLine] = &otlpcommon.ArrayValue{
Values: parts,
}
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"
Expand Down
21 changes: 3 additions & 18 deletions internal/goldendataset/resource_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,17 @@ import (
"testing"

"github.com/stretchr/testify/assert"

otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
)

func TestGenerateResource(t *testing.T) {
resourceIds := []PICTInputResource{ResourceNil, ResourceEmpty, ResourceVMOnPrem, ResourceVMCloud, ResourceK8sOnPrem,
ResourceK8sCloud, ResourceFaas, ResourceExec}
for _, rscID := range resourceIds {
rsc := GenerateResource(rscID)
if rscID == ResourceNil {
assert.Nil(t, rsc.Attributes)
if rscID == ResourceNil || rscID == ResourceEmpty {
assert.Equal(t, 0, rsc.Attributes().Len())
} else {
assert.NotNil(t, rsc.Attributes)
}
// test marshal/unmarshal
bytes, err := rsc.Marshal()
if err != nil {
assert.Fail(t, err.Error())
}
if len(bytes) > 0 {
copy := &otlpresource.Resource{}
err = copy.Unmarshal(bytes)
if err != nil {
assert.Fail(t, err.Error())
}
assert.EqualValues(t, len(rsc.Attributes), len(copy.Attributes))
assert.True(t, rsc.Attributes().Len() > 0)
}
}
}
Loading

0 comments on commit 8042a03

Please sign in to comment.