Skip to content

Commit

Permalink
change the log which contains a stack
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Aug 28, 2020
1 parent adcf9e9 commit 9d769d9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c *baseClient) getMembers(ctx context.Context, url string) (*pdpb.GetMembe
members, err := pdpb.NewPDClient(cc).GetMembers(ctx, &pdpb.GetMembersRequest{})
if err != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", err, cc.Target(), cc.GetState().String())
return nil, errs.ErrClientGetMember.GenWithStackByArgs(attachErr)
return nil, errs.ErrClientGetMember.Wrap(attachErr).GenWithStackByCause()
}
return members, nil
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/errs/errs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package errs
import (
"bytes"
"fmt"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -89,6 +90,20 @@ func (s *testErrorSuite) TestError(c *C) {
err := errors.New("test error")
log.Error("test", ZapError(ErrInvalidTimestamp, err))
rfc = `[error="[PD:tso:ErrInvalidTimestamp] test error"]`
fmt.Println(lg.Message())
c.Assert(strings.Contains(lg.Message(), rfc), IsTrue)
}

func (s *testErrorSuite) TestErrorWithStack(c *C) {
conf := &log.Config{Level: "debug", File: log.FileLogConfig{}, DisableTimestamp: true}
lg := newZapTestLogger(conf)
log.ReplaceGlobals(lg.Logger, nil)

_, err := strconv.ParseUint("-42", 10, 64)
log.Error("test", ZapError(ErrStrconvParseInt.Wrap(err).GenWithStackByCause()))
m1 := lg.Message()
log.Error("test", zap.Error(errors.WithStack(err)))
m2 := lg.Message()
// This test is based on line number and the first log is in line 102, the second is in line 104.
// So they have the same length stack. Move this test to another place need to change the corresponding length.
c.Assert(len(m1[strings.Index(m1, "[stack="):]), Equals, len(m2[strings.Index(m2, "[stack="):]))
}
2 changes: 1 addition & 1 deletion pkg/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func EtcdKVGet(c *clientv3.Client, key string, opts ...clientv3.OpOption) (*clie
}

if err != nil {
return resp, errs.ErrEtcdKVGet.GenWithStackByArgs(err)
return resp, errs.ErrEtcdKVGet.Wrap(err).GenWithStackByCause()
}
return resp, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/grpcutil/grpcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s SecurityConfig) ToTLSConfig() (*tls.Config, error) {

tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, errs.ErrEtcdTLSConfig.GenWithStackByArgs(err)
return nil, errs.ErrEtcdTLSConfig.Wrap(err).GenWithStackByCause()
}
return tlsConfig, nil
}
Expand Down Expand Up @@ -93,11 +93,11 @@ func GetClientConn(ctx context.Context, addr string, tlsCfg *tls.Config, do ...g
}
u, err := url.Parse(addr)
if err != nil {
return nil, errs.ErrURLParse.GenWithStackByArgs(err)
return nil, errs.ErrURLParse.Wrap(err).GenWithStackByCause()
}
cc, err := grpc.DialContext(ctx, u.Host, append(do, opt)...)
if err != nil {
return nil, errs.ErrGRPCDial.GenWithStackByArgs(err)
return nil, errs.ErrGRPCDial.Wrap(err).GenWithStackByCause()
}
return cc, nil
}
2 changes: 1 addition & 1 deletion server/election/leadership.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (ls *Leadership) DeleteLeader() error {
// delete leader itself and let others start a new election again.
resp, err := ls.LeaderTxn().Then(clientv3.OpDelete(ls.leaderKey)).Commit()
if err != nil {
return errs.ErrEtcdKVDelete.GenWithStackByArgs(err)
return errs.ErrEtcdKVDelete.Wrap(err).GenWithStackByCause()
}
if !resp.Succeeded {
return errs.ErrEtcdTxn.FastGenByArgs("not leader already")
Expand Down
4 changes: 2 additions & 2 deletions server/kv/etcd_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (kv *etcdKVBase) Save(key, value string) error {
txn := NewSlowLogTxn(kv.client)
resp, err := txn.Then(clientv3.OpPut(key, value)).Commit()
if err != nil {
err = errs.ErrEtcdKVPut.GenWithStackByArgs(err)
err = errs.ErrEtcdKVPut.Wrap(err).GenWithStackByCause()
log.Error("save to etcd meet error", zap.String("key", key), zap.String("value", value), errs.ZapError(err))
return err
}
Expand All @@ -105,7 +105,7 @@ func (kv *etcdKVBase) Remove(key string) error {
txn := NewSlowLogTxn(kv.client)
resp, err := txn.Then(clientv3.OpDelete(key)).Commit()
if err != nil {
err = errs.ErrEtcdKVDelete.GenWithStackByArgs(err)
err = errs.ErrEtcdKVDelete.Wrap(err).GenWithStackByCause()
log.Error("remove from etcd meet error", zap.String("key", key), errs.ZapError(err))
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (m *Member) MoveEtcdLeader(ctx context.Context, old, new uint64) error {
defer cancel()
err := m.etcd.Server.MoveLeader(moveCtx, old, new)
if err != nil {
return errs.ErrEtcdMoveLeader.GenWithStackByArgs(err)
return errs.ErrEtcdMoveLeader.Wrap(err).GenWithStackByCause()
}
return nil
}
Expand Down Expand Up @@ -331,7 +331,7 @@ func (m *Member) GetMemberLeaderPriority(id uint64) (int, error) {
}
priority, err := strconv.ParseInt(string(res.Kvs[0].Value), 10, 32)
if err != nil {
return 0, errs.ErrStrconvParseInt.GenWithStackByArgs(err)
return 0, errs.ErrStrconvParseInt.Wrap(err).GenWithStackByCause()
}
return int(priority), nil
}
Expand Down

0 comments on commit 9d769d9

Please sign in to comment.