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

Remove the proto dependency in goldendataset for traces #3322

Merged
merged 16 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## 💡 Enhancements 💡

- Add `doc.go` files to the consumer package and its subpackages (#3270)
- Remove the proto dependency in `goldendataset` for traces (#3322)
- Enable Dependabot for Github Actions (#3312)

## v0.27.0 Beta
Expand Down
67 changes: 25 additions & 42 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
return attributeMap
}
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)
}
42 changes: 15 additions & 27 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,9 @@ 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()
convertMapToAttributeMap(attrs).CopyTo(resource.Attributes())
return resource
}

func generateNilAttributes() map[string]interface{} {
Expand All @@ -70,12 +62,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 +143,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug, transformed from the constant conventions.AttributeProcessCommandLine to "conventions.AttributeProcessCommandLine". Fixed in #3377.

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