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 8 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 @@ -12,6 +12,7 @@
## 💡 Enhancements 💡

- Add `doc.go` files to the consumer package and its subpackages (#3270)
- Remove the proto dependency in `goldendataset` for traces (#TBD)

## v0.27.0 Beta

Expand Down
73 changes: 68 additions & 5 deletions internal/goldendataset/generator_commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,73 @@ import (

"github.com/spf13/cast"

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

func convertMapToAttributeMap(attrsMap map[string]interface{}) pdata.AttributeMap {
attributeMap := pdata.NewAttributeMap()
if attrsMap == nil {
return attributeMap
}
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 *otlpcommon.ArrayValue:
newValue = pdata.NewAttributeValueArray()
for _, v := range val.GetValues() {
newValue.ArrayVal().Append(convertAnyValue(v))
}
case *otlpcommon.KeyValueList:
newValue = pdata.NewAttributeValueMap()
for _, kv := range val.GetValues() {
newValue.MapVal().Insert(kv.Key, convertAnyValue(kv.Value))
}
default:
newValue = pdata.NewAttributeValueString(val.(string))
}
return newValue
}

func convertAnyValue(original otlpcommon.AnyValue) pdata.AttributeValue {
IrisTuntun marked this conversation as resolved.
Show resolved Hide resolved
var newValue pdata.AttributeValue
switch val := original.GetValue().(type) {
case *otlpcommon.AnyValue_IntValue:
newValue = pdata.NewAttributeValueInt(val.IntValue)
case *otlpcommon.AnyValue_BoolValue:
newValue = pdata.NewAttributeValueBool(val.BoolValue)
case *otlpcommon.AnyValue_DoubleValue:
newValue = pdata.NewAttributeValueDouble(val.DoubleValue)
case *otlpcommon.AnyValue_StringValue:
newValue = pdata.NewAttributeValueString(val.StringValue)
case *otlpcommon.AnyValue_ArrayValue:
newValue = pdata.NewAttributeValueArray()
for _, v := range val.ArrayValue.GetValues() {
newValue.ArrayVal().Append(convertAnyValue(v))
}
case *otlpcommon.AnyValue_KvlistValue:
newValue = pdata.NewAttributeValueMap()
for _, kv := range val.KvlistValue.GetValues() {
newValue.MapVal().Insert(kv.Key, convertAnyValue(kv.Value))
}
default:
newValue = pdata.NewAttributeValueNull()
}
return newValue
}

func convertMapToAttributeKeyValues(attrsMap map[string]interface{}) []otlpcommon.KeyValue {
if attrsMap == nil {
return nil
Expand Down Expand Up @@ -94,20 +157,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)
}
30 changes: 30 additions & 0 deletions internal/goldendataset/resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package goldendataset

import (
"go.opentelemetry.io/collector/consumer/pdata"
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
"go.opentelemetry.io/collector/translator/conventions"
Expand Down Expand Up @@ -56,6 +57,35 @@ func GenerateResource(rscID PICTInputResource) otlpresource.Resource {
}
}

// generatePDataResource generates a PData Resource object with representative attributes for the
// underlying resource type specified by the rscID input parameter.
func generatePDataResource(rscID PICTInputResource) pdata.Resource {
var attrs map[string]interface{}
switch rscID {
case ResourceNil:
attrs = generateNilAttributes()
case ResourceEmpty:
attrs = generateEmptyAttributes()
case ResourceVMOnPrem:
attrs = generateOnpremVMAttributes()
case ResourceVMCloud:
attrs = generateCloudVMAttributes()
case ResourceK8sOnPrem:
attrs = generateOnpremK8sAttributes()
case ResourceK8sCloud:
attrs = generateCloudK8sAttributes()
case ResourceFaas:
attrs = generateFassAttributes()
case ResourceExec:
attrs = generateExecAttributes()
default:
attrs = generateEmptyAttributes()
}
resource := pdata.NewResource()
convertMapToAttributeMap(attrs).CopyTo(resource.Attributes())
return resource
}

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