Skip to content

Commit

Permalink
Remove unnecessary proto marshaler tests, avoid using InternalRep in …
Browse files Browse the repository at this point in the history
…otlprecv (#3386)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jun 12, 2021
1 parent 2569435 commit 3050c8e
Showing 1 changed file with 13 additions and 107 deletions.
120 changes: 13 additions & 107 deletions receiver/otlpreceiver/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"testing"
"time"

"github.com/gogo/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
spb "google.golang.org/genproto/googleapis/rpc/status"
Expand All @@ -46,12 +45,7 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal"
"go.opentelemetry.io/collector/internal/data"
collectortrace "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
otlptrace "go.opentelemetry.io/collector/internal/data/protogen/trace/v1"
"go.opentelemetry.io/collector/internal/internalconsumertest"
"go.opentelemetry.io/collector/internal/pdatagrpc"
"go.opentelemetry.io/collector/internal/testdata"
Expand Down Expand Up @@ -97,39 +91,19 @@ var traceJSON = []byte(`
]
}`)

var resourceSpansOtlp = otlptrace.ResourceSpans{
Resource: otlpresource.Resource{
Attributes: []otlpcommon.KeyValue{
{
Key: conventions.AttributeHostName,
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "testHost"}},
},
},
},
InstrumentationLibrarySpans: []*otlptrace.InstrumentationLibrarySpans{
{
Spans: []*otlptrace.Span{
{
TraceId: data.NewTraceID([16]byte{0x5B, 0x8E, 0xFF, 0xF7, 0x98, 0x3, 0x81, 0x3, 0xD2, 0x69, 0xB6, 0x33, 0x81, 0x3F, 0xC6, 0xC}),
SpanId: data.NewSpanID([8]byte{0xEE, 0xE1, 0x9B, 0x7E, 0xC3, 0xC1, 0xB1, 0x73}),
Name: "testSpan",
StartTimeUnixNano: 1544712660000000000,
EndTimeUnixNano: 1544712661000000000,
Attributes: []otlpcommon.KeyValue{
{
Key: "attr1",
Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_IntValue{IntValue: 55}},
},
},
},
},
},
},
}

var traceOtlp = pdata.TracesFromInternalRep(internal.TracesFromOtlp(&collectortrace.ExportTraceServiceRequest{
ResourceSpans: []*otlptrace.ResourceSpans{&resourceSpansOtlp},
}))
var traceOtlp = func() pdata.Traces {
td := pdata.NewTraces()
rs := td.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().UpsertString(conventions.AttributeHostName, "testHost")
span := rs.InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty()
span.SetTraceID(pdata.NewTraceID([16]byte{0x5B, 0x8E, 0xFF, 0xF7, 0x98, 0x3, 0x81, 0x3, 0xD2, 0x69, 0xB6, 0x33, 0x81, 0x3F, 0xC6, 0xC}))
span.SetSpanID(pdata.NewSpanID([8]byte{0xEE, 0xE1, 0x9B, 0x7E, 0xC3, 0xC1, 0xB1, 0x73}))
span.SetName("testSpan")
span.SetStartTimestamp(1544712660000000000)
span.SetEndTimestamp(1544712661000000000)
span.Attributes().UpsertInt("attr1", 55)
return td
}()

func TestJsonHttp(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -239,74 +213,6 @@ func testHTTPJSONRequest(t *testing.T, url string, sink *internalconsumertest.Er
}
require.Len(t, allTraces, 0)
}

}

func TestJsonMarshaling(t *testing.T) {
m := jsonpb.Marshaler{}
json, err := m.MarshalToString(&resourceSpansOtlp)
assert.NoError(t, err)

var resourceSpansOtlp2 otlptrace.ResourceSpans
err = jsonpb.UnmarshalString(json, &resourceSpansOtlp2)
assert.NoError(t, err)

assert.EqualValues(t, resourceSpansOtlp, resourceSpansOtlp2)
}

func TestJsonUnmarshaling(t *testing.T) {
var resourceSpansOtlp2 otlptrace.ResourceSpans
err := jsonpb.UnmarshalString(`
{
"instrumentation_library_spans": [
{
"spans": [
{
}
]
}
]
}`, &resourceSpansOtlp2)
assert.NoError(t, err)
assert.EqualValues(t, data.TraceID{}, resourceSpansOtlp2.InstrumentationLibrarySpans[0].Spans[0].TraceId)

tests := []struct {
name string
json string
bytes [16]byte
}{
{
name: "empty string trace id",
json: `""`,
bytes: [16]byte{},
},
{
name: "zero bytes trace id",
json: `"00000000000000000000000000000000"`,
bytes: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var resourceSpansOtlp2 otlptrace.ResourceSpans
jsonStr := fmt.Sprintf(`
{
"instrumentation_library_spans": [
{
"spans": [
{
"trace_id": %v
}
]
}
]
}`, test.json)
err := jsonpb.UnmarshalString(jsonStr, &resourceSpansOtlp2)
assert.NoError(t, err)
assert.EqualValues(t, data.NewTraceID(test.bytes), resourceSpansOtlp2.InstrumentationLibrarySpans[0].Spans[0].TraceId)
})
}
}

func TestProtoHttp(t *testing.T) {
Expand Down

0 comments on commit 3050c8e

Please sign in to comment.