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

@ -6,7 +6,6 @@ import (
"OpenIM/pkg/common/constant" "OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache" "OpenIM/pkg/common/db/cache"
"OpenIM/pkg/common/db/controller" "OpenIM/pkg/common/db/controller"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tokenverify" "OpenIM/pkg/common/tokenverify"
"OpenIM/pkg/common/tracelog" "OpenIM/pkg/common/tracelog"
"OpenIM/pkg/discoveryregistry" "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) { 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{} resp := pbAuth.UserTokenResp{}
if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil { if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil {
log.Info("", "UserToken err:", err.Error())
return nil, err return nil, err
} }
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID))) token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
if err != nil { if err != nil {
log.Info("", "Create Token err:", err.Error())
return nil, err return nil, err
} }
resp.Token = token resp.Token = token
@ -114,7 +106,6 @@ func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID
for _, v := range conns { for _, v := range conns {
client := msggateway.NewMsgGatewayClient(v) client := msggateway.NewMsgGatewayClient(v)
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID} kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
log.NewInfo(operationID, "KickUserOffline ", client, kickReq.String())
_, err := client.KickUserOffline(ctx, kickReq) _, err := client.KickUserOffline(ctx, kickReq)
return utils.Wrap(err, "") return utils.Wrap(err, "")
} }

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

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

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

@ -54,6 +54,16 @@ func GetOpUserID(ctx context.Context) string {
return "" 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 { func Unwrap(err error) error {
for err != nil { for err != nil {
unwrap, ok := err.(interface { unwrap, ok := err.(interface {

Loading…
Cancel
Save