Skip to content

Commit

Permalink
add test for test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Sep 2, 2022
1 parent 6393b35 commit c861cfa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Expand Up @@ -19,6 +19,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.31.0 // indirect
go.opentelemetry.io/otel/trace v1.9.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
Expand Down
2 changes: 2 additions & 0 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/go.sum
Expand Up @@ -76,6 +76,8 @@ go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9s
go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A=
go.opentelemetry.io/otel/sdk v1.9.0 h1:LNXp1vrr83fNXTHgU8eO89mhzxb/bbWAsHG6fNf3qWo=
go.opentelemetry.io/otel/sdk v1.9.0/go.mod h1:AEZc8nt5bd2F7BC24J5R0mrjYnpEgYHyTcM/vrSple4=
go.opentelemetry.io/otel/sdk/metric v0.31.0 h1:2sZx4R43ZMhJdteKAlKoHvRgrMp53V1aRxvEf5lCq8Q=
go.opentelemetry.io/otel/sdk/metric v0.31.0/go.mod h1:fl0SmNnX9mN9xgU6OLYLMBMrNAsaZQi7qBwprwO3abk=
go.opentelemetry.io/otel/trace v1.9.0 h1:oZaCNJUjWcg60VXWee8lJKlqhPbXAPB51URuR47pQYc=
go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTNGg6VODnOiPo=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
Expand Down
52 changes: 50 additions & 2 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go
Expand Up @@ -30,6 +30,7 @@ import (

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metrictest"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
Expand Down Expand Up @@ -84,18 +85,20 @@ func TestInterceptors(t *testing.T) {

serverUnarySR := tracetest.NewSpanRecorder()
serverUnaryTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverUnarySR))
serverUnaryMP, serverUnaryMetricExporter := metrictest.NewTestMeterProvider()

serverStreamSR := tracetest.NewSpanRecorder()
serverStreamTP := trace.NewTracerProvider(trace.WithSpanProcessor(serverStreamSR))
serverStreamMP, serverStreamMetricExporter := metrictest.NewTestMeterProvider()

assert.NoError(t, doCalls(
[]grpc.DialOption{
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(otelgrpc.WithTracerProvider(clientUnaryTP))),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(clientStreamTP))),
},
[]grpc.ServerOption{
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(serverUnaryTP))),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(serverStreamTP))),
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(serverUnaryTP), otelgrpc.WithMeterProvider(serverUnaryMP))),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(serverStreamTP), otelgrpc.WithMeterProvider(serverStreamMP))),
},
))

Expand All @@ -109,10 +112,12 @@ func TestInterceptors(t *testing.T) {

t.Run("UnaryServerSpans", func(t *testing.T) {
checkUnaryServerSpans(t, serverUnarySR.Ended())
checkUnaryServerRecords(t, serverUnaryMetricExporter)
})

t.Run("StreamServerSpans", func(t *testing.T) {
checkStreamServerSpans(t, serverStreamSR.Ended())
checkStreamServerRecords(t, serverStreamMetricExporter)
})
}

Expand Down Expand Up @@ -622,3 +627,46 @@ func assertEvents(t *testing.T, expected, actual []trace.Event) bool {

return !failed
}

func checkUnaryServerRecords(t *testing.T, exporter *metrictest.Exporter) {
assert.NoError(t, exporter.Collect(context.Background()))
records := exporter.GetRecords()
assert.Equal(t, 2, len(records))

for _, record := range records {
method := getRPCMethod(record.Attributes)
assert.NotEmpty(t, method)
assert.ElementsMatch(t, []attribute.KeyValue{
semconv.RPCMethodKey.String(method),
semconv.RPCServiceKey.String("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
}, record.Attributes)
}
}

func checkStreamServerRecords(t *testing.T, exporter *metrictest.Exporter) {
assert.NoError(t, exporter.Collect(context.Background()))
records := exporter.GetRecords()
assert.Equal(t, 3, len(records))

for _, record := range records {
method := getRPCMethod(record.Attributes)
assert.NotEmpty(t, method)
assert.ElementsMatch(t, []attribute.KeyValue{
semconv.RPCMethodKey.String(method),
semconv.RPCServiceKey.String("grpc.testing.TestService"),
otelgrpc.RPCSystemGRPC,
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
}, record.Attributes)
}
}

func getRPCMethod(attrs []attribute.KeyValue) string {
for _, kvs := range attrs {
if kvs.Key == semconv.RPCMethodKey {
return kvs.Value.AsString()
}
}
return ""
}

0 comments on commit c861cfa

Please sign in to comment.