Repository: grpc/grpc-go
File: stats/opentelemetry/e2e_test.go (Go)

Code before:
		t.Fatalf("ss.Client.FullDuplexCall failed: %f", err)
	}

	const numMessages = 3
	for i := range numMessages {
		if err := stream.Send(&testpb.StreamingOutputCallRequest{
			ResponseType: testpb.PayloadType_COMPRESSABLE,
			ResponseParameters: []*testpb.ResponseParameters{
				{Size: 10},
			},
		}); err != nil {
			t.Fatalf("stream.Send() failed at message %d: %v", i, err)
		}
	}
	stream.CloseSend()
	for {
		_, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			t.Fatalf("stream.Recv received an unexpected error: %v", err)
		}
	}

	createMessageEvents := func(name string) []trace.Event {
		var events []trace.Event
		for i := 0; i < numMessages; i++ {
			events = append(events, trace.Event{
				Name: name,
				Attributes: []attribute.KeyValue{
					attribute.Int("sequence-number", i),
					attribute.Int("message-size", 4),
				},
			})
		}
		return events
	}

	wantSpanInfos := []traceSpanInfo{
		{
			name:     "Recv.grpc.testing.TestService.FullDuplexCall",
			spanKind: oteltrace.SpanKindServer.String(),
			attributes: []attribute.KeyValue{
				attribute.Bool("Client", false),
				attribute.Bool("FailFast", false),
				attribute.Int("previous-rpc-attempts", 0),
				attribute.Bool("transparent-retry", false),
			},
			events: createMessageEvents("Inbound message"),
		},

Review comment (suggestion):
nit: wantEvents? Although it might just be better to create 2 events slices 1) wantOutboundEvents and 2) wantInboundEvents. You can just fill them up in same loop by hardcoding the name. That will be easy to understand 

Code after:
		t.Fatalf("ss.Client.FullDuplexCall failed: %f", err)
	}

	const numMessages = 3
	var wantOutboundEvents, wantInboundEvents []trace.Event
	for i := range numMessages {
		if err := stream.Send(&testpb.StreamingOutputCallRequest{}); err != nil {
			t.Fatalf("stream.Send() failed at message %d: %v", i, err)
		}
		wantOutboundEvents = append(wantOutboundEvents, trace.Event{
			Name: "Outbound message",
			Attributes: []attribute.KeyValue{
				attribute.Int("sequence-number", i),
				attribute.Int("message-size", 0),
			},
		})
		wantInboundEvents = append(wantInboundEvents, trace.Event{
			Name: "Inbound message",
			Attributes: []attribute.KeyValue{
				attribute.Int("sequence-number", i),
				attribute.Int("message-size", 0),
			},
		})
	}
	stream.CloseSend()
	_, err = stream.Recv()
	if err != io.EOF {
		t.Fatalf("stream.Recv() got unexpected err=%v; want io.EOF", err)
	}

	wantSpanInfos := []traceSpanInfo{
		{
			name:       "Sent.grpc.testing.TestService.FullDuplexCall",
			spanKind:   oteltrace.SpanKindClient.String(),
			events:     nil,
			attributes: nil,
		},
		{
			name:     "Recv.grpc.testing.TestService.FullDuplexCall",
			spanKind: oteltrace.SpanKindServer.String(),
			events:   wantInboundEvents,
			attributes: []attribute.KeyValue{
				attribute.Bool("Client", false),
				attribute.Bool("FailFast", false),
				attribute.Int("previous-rpc-attempts", 0),
				attribute.Bool("transparent-retry", false),
			},
		},
		{
			name:     "Attempt.grpc.testing.TestService.FullDuplexCall",
			spanKind: oteltrace.SpanKindInternal.String(),