diff --git a/grpc-proxy/proxy.go b/grpc-proxy/proxy.go index ad43bee..fd62871 100644 --- a/grpc-proxy/proxy.go +++ b/grpc-proxy/proxy.go @@ -34,7 +34,7 @@ type server struct { func New(configurators ...Configurator) (*server, error) { logger := logrus.New() s := &server{ - connPool: internal.NewConnPool(), + connPool: internal.NewConnPool(logger), logger: logger, } s.serverOptions = []grpc.ServerOption{ diff --git a/grpc-replay/grpc-replay.go b/grpc-replay/grpc-replay.go index 4586a97..67ce48a 100644 --- a/grpc-replay/grpc-replay.go +++ b/grpc-replay/grpc-replay.go @@ -8,6 +8,7 @@ import ( "github.com/bradleyjkemp/grpc-tools/internal/codec" "github.com/bradleyjkemp/grpc-tools/internal/marker" _ "github.com/bradleyjkemp/grpc-tools/internal/versionflag" + "github.com/sirupsen/logrus" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -98,7 +99,7 @@ RPC: return nil } -var cachedConns = internal.NewConnPool() +var cachedConns = internal.NewConnPool(logrus.New()) func getConnection(md metadata.MD) (*grpc.ClientConn, error) { // if no destination override set then auto-detect from the metadata diff --git a/internal/connPool.go b/internal/connPool.go index 796ed5d..923f947 100644 --- a/internal/connPool.go +++ b/internal/connPool.go @@ -3,6 +3,7 @@ package internal import ( "context" "fmt" + "github.com/sirupsen/logrus" "google.golang.org/grpc" "sync" ) @@ -10,11 +11,13 @@ import ( type ConnPool struct { sync.Mutex conns map[string]*grpc.ClientConn + logger logrus.FieldLogger } -func NewConnPool() *ConnPool { +func NewConnPool(logger logrus.FieldLogger) *ConnPool { return &ConnPool{ conns: map[string]*grpc.ClientConn{}, + logger: logger.WithField("", "connpool"), } } @@ -34,11 +37,14 @@ func (c *ConnPool) addConn(destination string, conn *grpc.ClientConn) { func (c *ConnPool) GetClientConn(ctx context.Context, destination string, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) { conn, ok := c.getConn(destination) if ok { + c.logger.Debugf("Returning cached connection to %s", destination) return conn, nil } + c.logger.Debugf("Dialing new connection to %s", destination) conn, err := grpc.DialContext(ctx, destination, dialOptions...) if err != nil { + c.logger.WithError(err).Debugf("Failed dialing to %s", destination) return nil, fmt.Errorf("failed dialing %s: %v", destination, err) }