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 unnecessary proto marshaler tests, avoid using InternalRep in otlprecv #3386

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Changes from all 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
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"
"go.uber.org/zap"
Expand All @@ -47,12 +46,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 @@ -98,39 +92,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 @@ -240,74 +214,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) {
Comment on lines -246 to -258
Copy link
Member

Choose a reason for hiding this comment

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

These are important tests that verify JSON marshaling. Why delete these?

Copy link
Member Author

@bogdandrutu bogdandrutu Jun 9, 2021

Choose a reason for hiding this comment

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

We are testing that the jsonpb (from a third-party library) does the right thing. Unit-tests should be for our code and we should not write unit-tests to test third-party libraries unless a specific bug is covered.

Copy link
Member Author

Choose a reason for hiding this comment

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

Friendly ping @tigrannajaryan

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