Skip to content

Commit

Permalink
Use grpc.NewClient instead of grpc.Dial and grpc.DialContext
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Apr 7, 2024
1 parent a18d743 commit 5fdddf2
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func benchmark(b *testing.B, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) {

ctx := context.Background()
dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
conn, err := grpc.DialContext(
ctx,
conn, err := grpc.NewClient(
"bufnet",
append([]grpc.DialOption{
grpc.WithContextDialer(dial),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
}()

var conn *grpc.ClientConn
conn, err = grpc.Dial("127.0.0.1:7777", grpc.WithTransportCredentials(insecure.NewCredentials()),
conn, err = grpc.NewClient("127.0.0.1:7777", grpc.WithTransportCredentials(insecure.NewCredentials()),

Check warning on line 35 in instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go#L35

Added line #L35 was not covered by tests
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func ExampleNewClientHandler() {
_, _ = grpc.Dial("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
}

func ExampleNewServerHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
)

// UnaryClientInterceptor returns a grpc.UnaryClientInterceptor suitable
// for use in a grpc.Dial call.
// for use in a grpc.NewClient call.
//
// Deprecated: Use [NewClientHandler] instead.
func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (w *clientStream) CloseSend() error {
return err
}

func wrapClientStream(ctx context.Context, s grpc.ClientStream, desc *grpc.StreamDesc, span trace.Span, cfg *config) *clientStream {
func wrapClientStream(s grpc.ClientStream, desc *grpc.StreamDesc, span trace.Span, cfg *config) *clientStream {
return &clientStream{
ClientStream: s,
span: span,
Expand All @@ -208,7 +208,7 @@ func (w *clientStream) endSpan(err error) {
}

// StreamClientInterceptor returns a grpc.StreamClientInterceptor suitable
// for use in a grpc.Dial call.
// for use in a grpc.NewClient call.
//
// Deprecated: Use [NewClientHandler] instead.
func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
Expand Down Expand Up @@ -259,7 +259,7 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
span.End()
return s, err
}
stream := wrapClientStream(ctx, s, desc, span, cfg)
stream := wrapClientStream(s, desc, span, cfg)
return stream, nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var wantInstrumentationScope = instrumentation.Scope{
Version: otelgrpc.Version(),
}

// newGrpcTest creats a grpc server, starts it, and returns the client, closes everything down during test cleanup.
// newGrpcTest creates a grpc server, starts it, and returns the client, closes everything down during test cleanup.
func newGrpcTest(t testing.TB, listener net.Listener, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) pb.TestServiceClient {
grpcServer := grpc.NewServer(sOpt...)
pb.RegisterTestServiceServer(grpcServer, test.NewTestServer())
Expand All @@ -47,17 +47,15 @@ func newGrpcTest(t testing.TB, listener net.Listener, cOpt []grpc.DialOption, sO
grpcServer.Stop()
assert.NoError(t, <-errCh)
})
ctx := context.Background()

cOpt = append(cOpt, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cOpt = append(cOpt, grpc.WithTransportCredentials(insecure.NewCredentials()))

if l, ok := listener.(interface{ Dial() (net.Conn, error) }); ok {
dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
cOpt = append(cOpt, grpc.WithContextDialer(dial))
}

conn, err := grpc.DialContext(
ctx,
conn, err := grpc.NewClient(
listener.Addr().String(),
cOpt...,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func ctxDialer() func(context.Context, string) (net.Conn, error) {
}

func TestUnaryClientInterceptor(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906",
clientConn, err := grpc.NewClient("fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
Expand Down Expand Up @@ -377,11 +374,8 @@ func newMockClientStream(opts clientStreamOpts) *mockClientStream {
}

func createInterceptedStreamClient(t *testing.T, method string, opts clientStreamOpts) (grpc.ClientStream, *tracetest.SpanRecorder) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

mockStream := newMockClientStream(opts)
clientConn, err := grpc.DialContext(ctx, "fake:8906",
clientConn, err := grpc.NewClient("fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
Expand Down Expand Up @@ -632,10 +626,7 @@ func TestStreamClientInterceptorOnUnidirectionalClientServerStream(t *testing.T)
func TestStreamClientInterceptorCancelContext(t *testing.T) {
defer goleak.VerifyNone(t)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906",
clientConn, err := grpc.NewClient("fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
Expand Down Expand Up @@ -696,10 +687,7 @@ func TestStreamClientInterceptorCancelContext(t *testing.T) {
func TestStreamClientInterceptorWithError(t *testing.T) {
defer goleak.VerifyNone(t)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

clientConn, err := grpc.DialContext(ctx, "fake:8906",
clientConn, err := grpc.NewClient("fake:8906",
grpc.WithContextDialer(ctxDialer()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

// Set up a connection to the server with the OpenCensus
// stats handler to enable tracing.
conn, err := grpc.Dial(address, grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(address, grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), grpc.WithTransportCredentials(insecure.NewCredentials()))

Check warning on line 32 in propagators/opencensus/examples/opencensus_client/client.go

View check run for this annotation

Codecov / codecov/patch

propagators/opencensus/examples/opencensus_client/client.go#L32

Added line #L32 was not covered by tests
if err != nil {
log.Fatalf("Cannot connect: %v", err)
}
Expand Down

0 comments on commit 5fdddf2

Please sign in to comment.