# Conflicts: # pkg/common/db/controller/storage.gotest-errcode
commit
188dc6d322
@ -1,88 +0,0 @@
|
|||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"OpenIM/pkg/common/tracelog"
|
|
||||||
"context"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ShowLog(ctx context.Context) {
|
|
||||||
t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos)
|
|
||||||
OperationID := tracelog.GetOperationID(ctx)
|
|
||||||
for _, v := range *t.Funcs {
|
|
||||||
if v.Err != nil {
|
|
||||||
ctxLogger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": ctxLogger.Pid,
|
|
||||||
"FilePath": v.File,
|
|
||||||
}).Errorln("func: ", v.FuncName, " args: ", v.Args, v.Err.Error())
|
|
||||||
} else {
|
|
||||||
switch v.LogLevel {
|
|
||||||
case logrus.InfoLevel:
|
|
||||||
ctxLogger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": ctxLogger.Pid,
|
|
||||||
"FilePath": v.File,
|
|
||||||
}).Infoln("func: ", v.FuncName, " args: ", v.Args)
|
|
||||||
case logrus.DebugLevel:
|
|
||||||
ctxLogger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": ctxLogger.Pid,
|
|
||||||
"FilePath": v.File,
|
|
||||||
}).Debugln("func: ", v.FuncName, " args: ", v.Args)
|
|
||||||
case logrus.WarnLevel:
|
|
||||||
ctxLogger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": ctxLogger.Pid,
|
|
||||||
"FilePath": v.File,
|
|
||||||
}).Warnln("func: ", v.FuncName, " args: ", v.Args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func InfoWithCtx(ctx context.Context, args ...interface{}) {
|
|
||||||
t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos)
|
|
||||||
OperationID := tracelog.GetOperationID(ctx)
|
|
||||||
for _, v := range *t.Funcs {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Infoln(v.Args, args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func DebugWithCtx(ctx context.Context, args ...interface{}) {
|
|
||||||
t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos)
|
|
||||||
OperationID := tracelog.GetOperationID(ctx)
|
|
||||||
for _, v := range *t.Funcs {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Debugln(v.Args, args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ErrorWithCtx(ctx context.Context, args ...interface{}) {
|
|
||||||
t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos)
|
|
||||||
OperationID := tracelog.GetOperationID(ctx)
|
|
||||||
for _, v := range *t.Funcs {
|
|
||||||
if v.Err != nil {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Errorln(v.Err, v.Args, args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WarnWithCtx(ctx context.Context, args ...interface{}) {
|
|
||||||
t := ctx.Value(tracelog.TraceLogKey).(*tracelog.FuncInfos)
|
|
||||||
OperationID := tracelog.GetOperationID(ctx)
|
|
||||||
for _, v := range *t.Funcs {
|
|
||||||
logger.WithFields(logrus.Fields{
|
|
||||||
"OperationID": OperationID,
|
|
||||||
"PID": logger.Pid,
|
|
||||||
}).Warnln(v.Args, args)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
** description("").
|
|
||||||
** copyright('tuoyun,www.tuoyun.net').
|
|
||||||
** author("fg,Gordon@tuoyun.net").
|
|
||||||
** time(2021/2/22 11:52).
|
|
||||||
*/
|
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
TimeOffset = 8 * 3600 //8 hour offset
|
|
||||||
HalfOffset = 12 * 3600 //Half-day hourly offset
|
|
||||||
)
|
|
||||||
|
|
||||||
//Get the current timestamp
|
|
||||||
func GetCurrentTimestamp() int64 {
|
|
||||||
return time.Now().Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get the current 0 o'clock timestamp
|
|
||||||
func GetCurDayZeroTimestamp() int64 {
|
|
||||||
timeStr := time.Now().Format("2006-01-02")
|
|
||||||
t, _ := time.Parse("2006-01-02", timeStr)
|
|
||||||
return t.Unix() - TimeOffset
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get the timestamp at 12 o'clock on the day
|
|
||||||
func GetCurDayHalfTimestamp() int64 {
|
|
||||||
return GetCurDayZeroTimestamp() + HalfOffset
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get the formatted time at 0 o'clock of the day, the format is "2006-01-02_00-00-00"
|
|
||||||
func GetCurDayZeroTimeFormat() string {
|
|
||||||
return time.Unix(GetCurDayZeroTimestamp(), 0).Format("2006-01-02_15-04-05")
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get the formatted time at 12 o'clock of the day, the format is "2006-01-02_12-00-00"
|
|
||||||
func GetCurDayHalfTimeFormat() string {
|
|
||||||
return time.Unix(GetCurDayZeroTimestamp()+HalfOffset, 0).Format("2006-01-02_15-04-05")
|
|
||||||
}
|
|
||||||
func GetTimeStampByFormat(datetime string) string {
|
|
||||||
timeLayout := "2006-01-02 15:04:05" //转化所需模板
|
|
||||||
loc, _ := time.LoadLocation("Local") //获取时区
|
|
||||||
tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
|
|
||||||
timestamp := tmp.Unix() //转化为时间戳 类型是int64
|
|
||||||
return strconv.FormatInt(timestamp, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TimeStringFormatTimeUnix(timeFormat string, timeSrc string) int64 {
|
|
||||||
tm, _ := time.Parse(timeFormat, timeSrc)
|
|
||||||
return tm.Unix()
|
|
||||||
}
|
|
@ -1,131 +0,0 @@
|
|||||||
package mw
|
|
||||||
|
|
||||||
import (
|
|
||||||
"OpenIM/pkg/common/constant"
|
|
||||||
"OpenIM/pkg/common/log"
|
|
||||||
"OpenIM/pkg/common/mw/specialerror"
|
|
||||||
"OpenIM/pkg/errs"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/codes"
|
|
||||||
"google.golang.org/grpc/metadata"
|
|
||||||
"google.golang.org/grpc/status"
|
|
||||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
||||||
"math"
|
|
||||||
"runtime/debug"
|
|
||||||
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
const OperationID = "operationID"
|
|
||||||
const OpUserID = "opUserID"
|
|
||||||
|
|
||||||
func rpcString(v interface{}) string {
|
|
||||||
if s, ok := v.(interface{ String() string }); ok {
|
|
||||||
return s.String()
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%+v", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
||||||
var operationID string
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
if opts := md.Get(OperationID); len(opts) != 1 || opts[0] == "" {
|
|
||||||
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
|
|
||||||
} else {
|
|
||||||
operationID = opts[0]
|
|
||||||
}
|
|
||||||
var opUserID string
|
|
||||||
if opts := md.Get(OpUserID); len(opts) == 1 {
|
|
||||||
opUserID = opts[0]
|
|
||||||
}
|
|
||||||
ctx = context.WithValue(ctx, OperationID, operationID)
|
|
||||||
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
|
||||||
resp, err = handler(ctx, req)
|
|
||||||
if err == nil {
|
|
||||||
log.Info(operationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
|
|
||||||
unwrap := errs.Unwrap(err)
|
|
||||||
codeErr := specialerror.ErrCode(unwrap)
|
|
||||||
if codeErr == nil {
|
|
||||||
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
|
|
||||||
codeErr = errs.ErrInternalServer
|
|
||||||
}
|
|
||||||
var stack string
|
|
||||||
if unwrap != err {
|
|
||||||
stack = fmt.Sprintf("%+v", err)
|
|
||||||
log.ZError(ctx, "rpc error stack:", err)
|
|
||||||
}
|
|
||||||
code := codeErr.Code()
|
|
||||||
if code <= 0 || code > math.MaxUint32 {
|
|
||||||
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
|
|
||||||
code = errs.ServerInternalError
|
|
||||||
}
|
|
||||||
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
|
||||||
if errs.Unwrap(err) != err {
|
|
||||||
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
|
|
||||||
grpcStatus = details
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.ZInfo(ctx, "rpc resp", "funcName", funcName, "Resp", rpcString(resp))
|
|
||||||
return nil, grpcStatus.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
|
||||||
if ctx == nil {
|
|
||||||
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
opUserID, ok := ctx.Value(constant.OpUserID).(string)
|
|
||||||
if ok {
|
|
||||||
md.Append(constant.OpUserID, opUserID)
|
|
||||||
}
|
|
||||||
log.Info(operationID, "OpUserID", "RPC", method, "Req", rpcString(req))
|
|
||||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
|
||||||
if err == nil {
|
|
||||||
log.Info(operationID, "Resp", rpcString(reply))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
log.Info(operationID, "rpc error:", err.Error())
|
|
||||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
|
||||||
if !ok {
|
|
||||||
return errs.ErrInternalServer.Wrap(err.Error())
|
|
||||||
}
|
|
||||||
sta := rpcErr.GRPCStatus()
|
|
||||||
if sta.Code() == 0 {
|
|
||||||
return errs.NewCodeError(errs.ServerInternalError, err.Error()).Wrap()
|
|
||||||
}
|
|
||||||
if details := sta.Details(); len(details) > 0 {
|
|
||||||
if v, ok := details[0].(*wrapperspb.StringValue); ok {
|
|
||||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
func GrpcServer() grpc.ServerOption {
|
|
||||||
return grpc.UnaryInterceptor(rpcServerInterceptor)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GrpcClient() grpc.DialOption {
|
|
||||||
return grpc.WithUnaryInterceptor(rpcClientInterceptor)
|
|
||||||
}
|
|
@ -0,0 +1,54 @@
|
|||||||
|
package mw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"OpenIM/pkg/common/constant"
|
||||||
|
"OpenIM/pkg/common/log"
|
||||||
|
"OpenIM/pkg/errs"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GrpcClient() grpc.DialOption {
|
||||||
|
return grpc.WithUnaryInterceptor(rpcClientInterceptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rpcClientInterceptor(ctx context.Context, method string, req, resp interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
||||||
|
if ctx == nil {
|
||||||
|
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
|
||||||
|
}
|
||||||
|
log.ZInfo(ctx, "rpc req", "req", "funcName", method, rpcString(req))
|
||||||
|
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||||
|
if !ok {
|
||||||
|
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
|
||||||
|
return errs.ErrArgs.Wrap("ctx missing operationID")
|
||||||
|
}
|
||||||
|
md := metadata.Pairs(constant.OperationID, operationID)
|
||||||
|
opUserID, ok := ctx.Value(constant.OpUserID).(string)
|
||||||
|
if ok {
|
||||||
|
md.Append(constant.OpUserID, opUserID)
|
||||||
|
}
|
||||||
|
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, resp, cc, opts...)
|
||||||
|
if err == nil {
|
||||||
|
log.ZInfo(ctx, "rpc resp", "funcName", method, rpcString(resp))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
log.ZError(ctx, "rpc resp error", err)
|
||||||
|
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||||
|
if !ok {
|
||||||
|
return errs.ErrInternalServer.Wrap(err.Error())
|
||||||
|
}
|
||||||
|
sta := rpcErr.GRPCStatus()
|
||||||
|
if sta.Code() == 0 {
|
||||||
|
return errs.NewCodeError(errs.ServerInternalError, err.Error()).Wrap()
|
||||||
|
}
|
||||||
|
if details := sta.Details(); len(details) > 0 {
|
||||||
|
if v, ok := details[0].(*wrapperspb.StringValue); ok {
|
||||||
|
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package mw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"OpenIM/pkg/common/log"
|
||||||
|
"OpenIM/pkg/common/mw/specialerror"
|
||||||
|
"OpenIM/pkg/errs"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
"math"
|
||||||
|
"runtime/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
const OperationID = "operationID"
|
||||||
|
const OpUserID = "opUserID"
|
||||||
|
|
||||||
|
func rpcString(v interface{}) string {
|
||||||
|
if s, ok := v.(interface{ String() string }); ok {
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%+v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
var operationID string
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, string(debug.Stack()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
funcName := info.FullMethod
|
||||||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
|
||||||
|
}
|
||||||
|
if opts := md.Get(OperationID); len(opts) != 1 || opts[0] == "" {
|
||||||
|
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
|
||||||
|
} else {
|
||||||
|
operationID = opts[0]
|
||||||
|
}
|
||||||
|
var opUserID string
|
||||||
|
if opts := md.Get(OpUserID); len(opts) == 1 {
|
||||||
|
opUserID = opts[0]
|
||||||
|
}
|
||||||
|
ctx = context.WithValue(ctx, OperationID, operationID)
|
||||||
|
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
||||||
|
log.ZInfo(ctx, "rpc req", "funcName", funcName, "req", rpcString(req))
|
||||||
|
resp, err = handler(ctx, req)
|
||||||
|
if err == nil {
|
||||||
|
log.ZInfo(ctx, "rpc resp", "funcName", funcName, "resp", rpcString(resp))
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
unwrap := errs.Unwrap(err)
|
||||||
|
codeErr := specialerror.ErrCode(unwrap)
|
||||||
|
if codeErr == nil {
|
||||||
|
log.ZError(ctx, "rpc InternalServer error", err, "req", req)
|
||||||
|
codeErr = errs.ErrInternalServer
|
||||||
|
}
|
||||||
|
code := codeErr.Code()
|
||||||
|
if code <= 0 || code > math.MaxUint32 {
|
||||||
|
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
|
||||||
|
code = errs.ServerInternalError
|
||||||
|
}
|
||||||
|
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
||||||
|
//if unwrap != err {
|
||||||
|
// stack := fmt.Sprintf("%+v", err)
|
||||||
|
// if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
|
||||||
|
// grpcStatus = details
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
log.ZWarn(ctx, "rpc resp", unwrap, "funcName", funcName)
|
||||||
|
return nil, grpcStatus.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GrpcServer() grpc.ServerOption {
|
||||||
|
return grpc.UnaryInterceptor(rpcServerInterceptor)
|
||||||
|
}
|
Loading…
Reference in new issue