From 701f02f90aac03f6ddf958e77fbc55a66aa92ecd Mon Sep 17 00:00:00 2001 From: Denis Issoupov Date: Mon, 13 Jul 2020 09:05:06 -0700 Subject: [PATCH] 12125: panic: zap.Logger is nil in Embed client --- clientv3/client.go | 8 +++++++- clientv3/client_test.go | 13 +++++++++++++ etcdserver/api/v3client/v3client.go | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/clientv3/client.go b/clientv3/client.go index ed4a98677..3e33e956c 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -112,7 +112,7 @@ func New(cfg Config) (*Client, error) { // service interface implementations and do not need connection management. func NewCtxClient(ctx context.Context) *Client { cctx, cancel := context.WithCancel(ctx) - return &Client{ctx: cctx, cancel: cancel} + return &Client{ctx: cctx, cancel: cancel, lg: zap.NewNop()} } // NewFromURL creates a new etcdv3 client from a URL. @@ -125,6 +125,12 @@ func NewFromURLs(urls []string) (*Client, error) { return New(Config{Endpoints: urls}) } +// WithLogger sets a logger +func (c *Client) WithLogger(lg *zap.Logger) *Client { + c.lg = lg + return c +} + // Close shuts down the client's etcd connections. func (c *Client) Close() error { c.cancel() diff --git a/clientv3/client_test.go b/clientv3/client_test.go index e4daf0abd..232aa441c 100644 --- a/clientv3/client_test.go +++ b/clientv3/client_test.go @@ -166,3 +166,16 @@ func TestCloseCtxClient(t *testing.T) { t.Errorf("failed to Close the client. %v", err) } } + +func TestWithLogger(t *testing.T) { + ctx := context.Background() + c := NewCtxClient(ctx) + if c.lg == nil { + t.Errorf("unexpected nil in *zap.Logger") + } + + c.WithLogger(nil) + if c.lg != nil { + t.Errorf("WithLogger should modify *zap.Logger") + } +} diff --git a/etcdserver/api/v3client/v3client.go b/etcdserver/api/v3client/v3client.go index f2a401224..f93474fe7 100644 --- a/etcdserver/api/v3client/v3client.go +++ b/etcdserver/api/v3client/v3client.go @@ -29,6 +29,10 @@ import ( // to the etcd server through its api/v3rpc function interfaces. func New(s *etcdserver.EtcdServer) *clientv3.Client { c := clientv3.NewCtxClient(context.Background()) + lg := s.Logger() + if lg != nil { + c.WithLogger(lg) + } kvc := adapter.KvServerToKvClient(v3rpc.NewQuotaKVServer(s)) c.KV = clientv3.NewKVFromKVClient(kvc, c)