Skip to content

Commit 8042a03

Browse files
IrisTuntunAneurysm9bogdandrutu
authored
Remove the proto dependency in goldendataset for traces (#3322)
* Remove the proto dependency in goldendataset for traces * revert changelog * update changelog * Update consumer/pdata/common.go Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * 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 <a9@aneurysm9.com> Co-authored-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent 6cbf7e3 commit 8042a03

File tree

8 files changed

+195
-229
lines changed

8 files changed

+195
-229
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Add `doc.go` files to the consumer package and its subpackages (#3270)
2121
- Automate triggering of doc-update on release (#3234)
2222
- Enable Dependabot for Github Actions (#3312)
23+
- Remove the proto dependency in `goldendataset` for traces (#3322)
2324
- Add telemetry for dropped data due to exporter sending queue overflow (#3328)
2425

2526
## v0.27.0 Beta

internal/goldendataset/generator_commons.go

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,41 @@ import (
2222

2323
"github.com/spf13/cast"
2424

25-
"go.opentelemetry.io/collector/internal/data"
26-
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
25+
"go.opentelemetry.io/collector/consumer/pdata"
2726
)
2827

29-
func convertMapToAttributeKeyValues(attrsMap map[string]interface{}) []otlpcommon.KeyValue {
28+
func convertMapToAttributeMap(attrsMap map[string]interface{}) *pdata.AttributeMap {
29+
attributeMap := pdata.NewAttributeMap()
3030
if attrsMap == nil {
3131
return nil
3232
}
33-
attrList := make([]otlpcommon.KeyValue, len(attrsMap))
34-
index := 0
3533
for key, value := range attrsMap {
36-
attrList[index] = constructAttributeKeyValue(key, value)
37-
index++
34+
attributeMap.Insert(key, convertToAttributeValue(value))
3835
}
39-
return attrList
36+
return &attributeMap
4037
}
4138

42-
func constructAttributeKeyValue(key string, value interface{}) otlpcommon.KeyValue {
43-
var attr otlpcommon.KeyValue
39+
func convertToAttributeValue(value interface{}) pdata.AttributeValue {
40+
var newValue pdata.AttributeValue
4441
switch val := value.(type) {
4542
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
46-
attr = otlpcommon.KeyValue{
47-
Key: key,
48-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_IntValue{IntValue: cast.ToInt64(val)}},
49-
}
43+
newValue = pdata.NewAttributeValueInt(cast.ToInt64(val))
5044
case float32, float64:
51-
attr = otlpcommon.KeyValue{
52-
Key: key,
53-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_DoubleValue{DoubleValue: cast.ToFloat64(val)}},
54-
}
45+
newValue = pdata.NewAttributeValueDouble(cast.ToFloat64(val))
5546
case bool:
56-
attr = otlpcommon.KeyValue{
57-
Key: key,
58-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_BoolValue{BoolValue: cast.ToBool(val)}},
59-
}
60-
case *otlpcommon.ArrayValue:
61-
attr = otlpcommon.KeyValue{
62-
Key: key,
63-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: val}},
64-
}
65-
case *otlpcommon.KeyValueList:
66-
attr = otlpcommon.KeyValue{
67-
Key: key,
68-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_KvlistValue{KvlistValue: val}},
69-
}
47+
newValue = pdata.NewAttributeValueBool(cast.ToBool(val))
48+
case pdata.AttributeMap:
49+
newValue = pdata.NewAttributeValueMap()
50+
val.CopyTo(newValue.MapVal())
51+
case pdata.AnyValueArray:
52+
newValue = pdata.NewAttributeValueArray()
53+
val.CopyTo(newValue.ArrayVal())
54+
case pdata.AttributeValue:
55+
newValue = val
7056
default:
71-
attr = otlpcommon.KeyValue{
72-
Key: key,
73-
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: val.(string)}},
74-
}
57+
newValue = pdata.NewAttributeValueString(val.(string))
7558
}
76-
return attr
59+
return newValue
7760
}
7861

7962
func loadPictOutputFile(fileName string) ([][]string, error) {
@@ -94,20 +77,20 @@ func loadPictOutputFile(fileName string) ([][]string, error) {
9477
return reader.ReadAll()
9578
}
9679

97-
func generateTraceID(random io.Reader) data.TraceID {
80+
func generatePDataTraceID(random io.Reader) pdata.TraceID {
9881
var r [16]byte
9982
_, err := random.Read(r[:])
10083
if err != nil {
10184
panic(err)
10285
}
103-
return data.NewTraceID(r)
86+
return pdata.NewTraceID(r)
10487
}
10588

106-
func generateSpanID(random io.Reader) data.SpanID {
89+
func generatePDataSpanID(random io.Reader) pdata.SpanID {
10790
var r [8]byte
10891
_, err := random.Read(r[:])
10992
if err != nil {
11093
panic(err)
11194
}
112-
return data.NewSpanID(r)
95+
return pdata.NewSpanID(r)
11396
}

internal/goldendataset/resource_generator.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
package goldendataset
1616

1717
import (
18-
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
19-
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
18+
"go.opentelemetry.io/collector/consumer/pdata"
2019
"go.opentelemetry.io/collector/translator/conventions"
2120
)
2221

23-
// GenerateResource generates a OTLP Resource object with representative attributes for the
22+
// GenerateResource generates a PData Resource object with representative attributes for the
2423
// underlying resource type specified by the rscID input parameter.
25-
func GenerateResource(rscID PICTInputResource) otlpresource.Resource {
24+
func GenerateResource(rscID PICTInputResource) pdata.Resource {
2625
var attrs map[string]interface{}
2726
switch rscID {
2827
case ResourceNil:
@@ -44,16 +43,12 @@ func GenerateResource(rscID PICTInputResource) otlpresource.Resource {
4443
default:
4544
attrs = generateEmptyAttributes()
4645
}
47-
var dropped uint32
48-
if len(attrs) < 10 {
49-
dropped = 0
50-
} else {
51-
dropped = uint32(len(attrs) % 4)
52-
}
53-
return otlpresource.Resource{
54-
Attributes: convertMapToAttributeKeyValues(attrs),
55-
DroppedAttributesCount: dropped,
46+
resource := pdata.NewResource()
47+
attributeMap := convertMapToAttributeMap(attrs)
48+
if attributeMap != nil {
49+
attributeMap.CopyTo(resource.Attributes())
5650
}
51+
return resource
5752
}
5853

5954
func generateNilAttributes() map[string]interface{} {
@@ -70,12 +65,10 @@ func generateOnpremVMAttributes() map[string]interface{} {
7065
attrMap[conventions.AttributeServiceName] = "customers"
7166
attrMap[conventions.AttributeServiceNamespace] = "production"
7267
attrMap[conventions.AttributeServiceVersion] = "semver:0.7.3"
73-
subMap := make(map[string]interface{})
74-
subMap["public"] = "tc-prod9.internal.example.com"
75-
subMap["internal"] = "172.18.36.18"
76-
attrMap[conventions.AttributeHostName] = &otlpcommon.KeyValueList{
77-
Values: convertMapToAttributeKeyValues(subMap),
78-
}
68+
subMap := pdata.NewAttributeMap()
69+
subMap.InsertString("public", "tc-prod9.internal.example.com")
70+
subMap.InsertString("internal", "172.18.36.18")
71+
attrMap[conventions.AttributeHostName] = subMap
7972
attrMap[conventions.AttributeHostImageID] = "661ADFA6-E293-4870-9EFA-1AA052C49F18"
8073
attrMap[conventions.AttributeTelemetrySDKLanguage] = conventions.AttributeSDKLangValueJava
8174
attrMap[conventions.AttributeTelemetrySDKName] = "opentelemetry"
@@ -153,13 +146,11 @@ func generateFassAttributes() map[string]interface{} {
153146
func generateExecAttributes() map[string]interface{} {
154147
attrMap := make(map[string]interface{})
155148
attrMap[conventions.AttributeProcessExecutableName] = "otelcol"
156-
parts := make([]otlpcommon.AnyValue, 3)
157-
parts[0] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "otelcol"}}
158-
parts[1] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "--config=/etc/otel-collector-config.yaml"}}
159-
parts[2] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "--mem-ballast-size-mib=683"}}
160-
attrMap[conventions.AttributeProcessCommandLine] = &otlpcommon.ArrayValue{
161-
Values: parts,
162-
}
149+
parts := pdata.NewAttributeValueArray()
150+
parts.ArrayVal().Append(pdata.NewAttributeValueString("otelcol"))
151+
parts.ArrayVal().Append(pdata.NewAttributeValueString("--config=/etc/otel-collector-config.yaml"))
152+
parts.ArrayVal().Append(pdata.NewAttributeValueString("--mem-ballast-size-mib=683"))
153+
attrMap["conventions.AttributeProcessCommandLine"] = parts
163154
attrMap[conventions.AttributeProcessExecutablePath] = "/usr/local/bin/otelcol"
164155
attrMap[conventions.AttributeProcessID] = 2020
165156
attrMap[conventions.AttributeProcessOwner] = "otel"

internal/goldendataset/resource_generator_test.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,17 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21-
22-
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
2321
)
2422

2523
func TestGenerateResource(t *testing.T) {
2624
resourceIds := []PICTInputResource{ResourceNil, ResourceEmpty, ResourceVMOnPrem, ResourceVMCloud, ResourceK8sOnPrem,
2725
ResourceK8sCloud, ResourceFaas, ResourceExec}
2826
for _, rscID := range resourceIds {
2927
rsc := GenerateResource(rscID)
30-
if rscID == ResourceNil {
31-
assert.Nil(t, rsc.Attributes)
28+
if rscID == ResourceNil || rscID == ResourceEmpty {
29+
assert.Equal(t, 0, rsc.Attributes().Len())
3230
} else {
33-
assert.NotNil(t, rsc.Attributes)
34-
}
35-
// test marshal/unmarshal
36-
bytes, err := rsc.Marshal()
37-
if err != nil {
38-
assert.Fail(t, err.Error())
39-
}
40-
if len(bytes) > 0 {
41-
copy := &otlpresource.Resource{}
42-
err = copy.Unmarshal(bytes)
43-
if err != nil {
44-
assert.Fail(t, err.Error())
45-
}
46-
assert.EqualValues(t, len(rsc.Attributes), len(copy.Attributes))
31+
assert.True(t, rsc.Attributes().Len() > 0)
4732
}
4833
}
4934
}

0 commit comments

Comments
 (0)