Skip to content

Commit

Permalink
otelgrpc/test: add TestNoopStatsHandler testcase
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Nov 27, 2023
1 parent e752691 commit 4b2f3d0
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/interop"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand Down Expand Up @@ -90,6 +91,65 @@ func TestStatsHandler(t *testing.T) {
})
}

type noopHandler struct {
handler stats.Handler
}

func (h *noopHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
return ctx
}
func (h *noopHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
h.handler.HandleRPC(ctx, rs)
}
func (h *noopHandler) TagConn(ctx context.Context, ci *stats.ConnTagInfo) context.Context {
return ctx
}
func (h *noopHandler) HandleConn(context.Context, stats.ConnStats) {}

func TestNoopStatsHandler(t *testing.T) {
clientSR := tracetest.NewSpanRecorder()
clientTP := trace.NewTracerProvider(trace.WithSpanProcessor(clientSR))

serverSR := tracetest.NewSpanRecorder()
serverTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverSR))

listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "failed to open port")
client := newGrpcTest(t, listener,
[]grpc.DialOption{
grpc.WithStatsHandler(&noopHandler{
handler: otelgrpc.NewClientHandler(
otelgrpc.WithTracerProvider(clientTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)},
),
},
[]grpc.ServerOption{
grpc.StatsHandler(&noopHandler{
otelgrpc.NewServerHandler(
otelgrpc.WithTracerProvider(serverTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)},
),
},
)
doCalls(client)

t.Run("ClientSpans", func(t *testing.T) {
spans := clientSR.Ended()
if len(spans) != 0 {
t.Fatalf("got %d but want 0", len(spans))
}
})

t.Run("ServerSpans", func(t *testing.T) {
spans := serverSR.Ended()
if len(spans) != 0 {
t.Fatalf("got %d but want 0", len(spans))
}
})
}

func checkClientSpans(t *testing.T, spans []trace.ReadOnlySpan) {
require.Len(t, spans, 5)

Expand Down

0 comments on commit 4b2f3d0

Please sign in to comment.