Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
withchao 2 years ago
commit b8a1b6644b

@ -5,7 +5,6 @@ import (
"OpenIM/pkg/common/cmd"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/mw"
"context"
"errors"
"fmt"
@ -34,7 +33,6 @@ func run(port int) error {
return err
}
log.NewPrivateLog(constant.LogFileName)
zk.AddOption(mw.GrpcClient())
router := api.NewGinRouter(zk)
var address string
if config.Config.Api.ListenIP != "" {

@ -6,7 +6,6 @@ import (
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache"
"OpenIM/pkg/common/db/controller"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tokenverify"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/discoveryregistry"
@ -38,19 +37,12 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
}
func (s *authServer) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
log.Info("", "rpc come UserToken")
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
log.Error("2222", "ctx missing operationID", operationID)
}
resp := pbAuth.UserTokenResp{}
if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil {
log.Info("", "UserToken err:", err.Error())
return nil, err
}
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
if err != nil {
log.Info("", "Create Token err:", err.Error())
return nil, err
}
resp.Token = token
@ -114,7 +106,6 @@ func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID
for _, v := range conns {
client := msggateway.NewMsgGatewayClient(v)
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
log.NewInfo(operationID, "KickUserOffline ", client, kickReq.String())
_, err := client.KickUserOffline(ctx, kickReq)
return utils.Wrap(err, "")
}

@ -272,6 +272,7 @@ const (
const OperationID = "operationID"
const OpUserID = "opUserID"
const ConnID = "connID"
const (
UnreliableNotification = 1

@ -85,7 +85,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
c := zap.NewProductionEncoderConfig()
c.EncodeTime = zapcore.ISO8601TimeEncoder
c.EncodeDuration = zapcore.SecondsDurationEncoder
c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
//c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
fileEncoder := zapcore.NewJSONEncoder(c)
fileEncoder.AddInt("PID", os.Getpid())
writer, err := l.getWriter()
@ -99,7 +99,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
}
}
if config.Config.Log.Stderr {
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), zapcore.Lock(os.Stdout), zapcore.DebugLevel))
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel))
}
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
@ -117,10 +117,6 @@ func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
return zapcore.AddSync(logf), nil
}
func (l *ZapLogger) GetConsoleWriter() {
return
}
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
return l.zap
}
@ -154,12 +150,16 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} {
operationID := tracelog.GetOperationID(ctx)
opUserID := tracelog.GetOpUserID(ctx)
connID := tracelog.GetConnID(ctx)
if opUserID != "" {
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
}
if operationID != "" {
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
}
if connID != "" {
keysAndValues = append([]interface{}{constant.ConnID, tracelog.GetConnID(ctx)}, keysAndValues...)
}
return keysAndValues
}

@ -4,7 +4,6 @@ import (
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/mw/specialerror"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/errs"
"context"
"fmt"
@ -15,6 +14,8 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"
"math"
"runtime/debug"
"errors"
)
const OperationID = "operationID"
@ -31,11 +32,12 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
var operationID string
defer func() {
if r := recover(); r != nil {
log.NewError(operationID, info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, "stack:", string(debug.Stack()))
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, string(debug.Stack()))
}
}()
log.Info("", "rpc come here,in rpc call")
funcName := info.FullMethod
log.ZInfo(ctx, "rpc req", "funcName", funcName, "req", rpcString(req))
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
@ -49,8 +51,6 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
if opts := md.Get(OpUserID); len(opts) == 1 {
opUserID = opts[0]
}
log.Info(operationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
ctx = context.WithValue(ctx, OperationID, operationID)
ctx = context.WithValue(ctx, OpUserID, opUserID)
resp, err = handler(ctx, req)
@ -58,21 +58,21 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
log.Info(operationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
return resp, nil
}
log.Info(operationID, "rpc error:", err.Error())
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
unwrap := errs.Unwrap(err)
codeErr := specialerror.ErrCode(unwrap)
if codeErr == nil {
log.Error(operationID, "rpc InternalServer:", err.Error())
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
codeErr = errs.ErrInternalServer
}
var stack string
if unwrap != err {
stack = fmt.Sprintf("%+v", err)
log.Info(operationID, "rpc error stack:", stack)
log.ZError(ctx, "rpc error stack:", err)
}
code := codeErr.Code()
if code <= 0 || code > math.MaxUint32 {
log.Error(operationID, "rpc UnknownCode:", code, "err:", err.Error())
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
code = errs.ServerInternalError
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
@ -81,6 +81,7 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
grpcStatus = details
}
}
log.ZInfo(ctx, "rpc resp", "funcName", funcName, "Resp", rpcString(resp))
return nil, grpcStatus.Err()
}
@ -90,6 +91,7 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
}
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
log.ZError(ctx, "ctx missing operationID", errors.New("ctx missing operationID"))
return errs.ErrArgs.Wrap("ctx missing operationID")
}
md := metadata.Pairs(constant.OperationID, operationID)

@ -54,6 +54,16 @@ func GetOpUserID(ctx context.Context) string {
return ""
}
func GetConnID(ctx context.Context) string {
if ctx.Value(constant.ConnID) != "" {
s, ok := ctx.Value(constant.ConnID).(string)
if ok {
return s
}
}
return ""
}
func Unwrap(err error) error {
for err != nil {
unwrap, ok := err.(interface {

Loading…
Cancel
Save