Skip to content

Commit

Permalink
otelgrpc: Use net.Listen in TestStatsHandler (#4538)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Nov 9, 2023
1 parent 23bd4ed commit 016b46f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Expand Up @@ -16,13 +16,13 @@ package test

import (
"context"
"net"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/test/bufconn"

"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -47,9 +47,9 @@ func TestStatsHandler(t *testing.T) {
serverMetricReader := metric.NewManualReader()
serverMP := metric.NewMeterProvider(metric.WithReader(serverMetricReader))

listener := bufconn.Listen(bufSize)
defer listener.Close()
err := newGrpcTest(
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "failed to open port")
err = newGrpcTest(
listener,
[]grpc.DialOption{
grpc.WithStatsHandler(otelgrpc.NewClientHandler(
Expand Down
27 changes: 13 additions & 14 deletions instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go
Expand Up @@ -46,8 +46,6 @@ var wantInstrumentationScope = instrumentation.Scope{
Version: otelgrpc.Version(),
}

const bufSize = 2048

// newGrpcTest creats a grpc server, starts it, and executes all the calls, closes everything down.
func newGrpcTest(listener net.Listener, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) error {
grpcServer := grpc.NewServer(sOpt...)
Expand Down Expand Up @@ -108,18 +106,19 @@ func TestInterceptors(t *testing.T) {

listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "failed to open port")
err = newGrpcTest(listener, []grpc.DialOption{
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(
otelgrpc.WithTracerProvider(clientUnaryTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(
otelgrpc.WithTracerProvider(clientStreamTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
},
err = newGrpcTest(listener,
[]grpc.DialOption{
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(
otelgrpc.WithTracerProvider(clientUnaryTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(
otelgrpc.WithTracerProvider(clientStreamTP),
otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents),
)),
},
[]grpc.ServerOption{
//nolint:staticcheck // Interceptors are deprecated and will be removed in the next release.
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(
Expand Down

0 comments on commit 016b46f

Please sign in to comment.