Files
open-im-server/pkg/common/trace_log/ctx.go
T

228 lines
6.8 KiB
Go
Raw Normal View History

2022-12-30 18:46:02 +08:00
package trace_log
import (
"Open_IM/pkg/common/constant"
2023-01-12 15:55:44 +08:00
"Open_IM/pkg/utils"
2022-12-30 18:46:02 +08:00
"context"
2023-01-12 15:55:44 +08:00
"github.com/sirupsen/logrus"
2023-01-11 19:26:17 +08:00
"google.golang.org/grpc/status"
2023-01-12 15:55:44 +08:00
"runtime"
2023-01-11 19:26:17 +08:00
"strings"
//"errors"
2022-12-30 18:46:02 +08:00
"fmt"
"github.com/gin-gonic/gin"
2022-12-30 19:46:26 +08:00
"net/http"
2022-12-30 18:46:02 +08:00
)
const TraceLogKey = "trace_log"
func NewCtx(c *gin.Context, api string) context.Context {
2023-01-03 14:08:29 +08:00
req := &ApiInfo{ApiName: api, GinCtx: c, Funcs: &[]FuncInfo{}}
return context.WithValue(c, TraceLogKey, req)
}
2023-01-16 16:21:39 +08:00
func NewCtx1(c *gin.Context, api string) context.Context {
req := &ApiInfo{ApiName: api, GinCtx: c, OperationID: c.GetHeader("operationID"), Funcs: &[]FuncInfo{}}
2023-01-12 10:07:08 +08:00
return context.WithValue(c, TraceLogKey, req)
}
2023-01-04 11:06:28 +08:00
func NewRpcCtx(c context.Context, rpc string, operationID string) context.Context {
req := &ApiInfo{ApiName: rpc, Funcs: &[]FuncInfo{}}
ctx := context.WithValue(c, TraceLogKey, req)
SetOperationID(ctx, operationID)
return ctx
}
2023-01-03 14:08:29 +08:00
func SetOperationID(ctx context.Context, operationID string) {
ctx.Value(TraceLogKey).(*ApiInfo).OperationID = operationID
2022-12-30 18:46:02 +08:00
}
2023-01-09 16:37:33 +08:00
func GetOperationID(ctx context.Context) string {
return ctx.Value(TraceLogKey).(*ApiInfo).OperationID
}
//func ShowLog(ctx context.Context) {
// t := ctx.Value(TraceLogKey).(*ApiInfo)
// if ctx.Value(TraceLogKey).(*ApiInfo).GinCtx != nil {
// log.Info(t.OperationID, "api: ", t.ApiName)
// } else {
// log.Info(t.OperationID, "rpc: ", t.ApiName)
// }
// for _, v := range *t.Funcs {
// if v.Err != nil {
// log.Error(t.OperationID, "func: ", v.FuncName, " args: ", v.Args, v.Err.Error())
// } else {
// switch v.LogLevel {
// case logrus.InfoLevel:
// log.Info(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
// case logrus.DebugLevel:
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
// case logrus.WarnLevel:
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
// }
// }
// }
//}
2022-12-30 18:46:02 +08:00
2023-01-03 14:08:29 +08:00
func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
2023-01-12 15:55:44 +08:00
SetCtxInfo(ctx, funcName, err, args)
2023-01-11 19:26:17 +08:00
e := Unwrap(err)
switch t := e.(type) {
case *constant.ErrInfo:
2023-01-12 16:16:50 +08:00
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, baseResp{ErrCode: t.ErrCode, ErrMsg: t.ErrMsg, ErrDtl: t.DetailErrMsg})
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": t.ErrCode, "errMsg": t.ErrMsg, "errDtl": t.DetailErrMsg})
2022-12-30 19:46:26 +08:00
return
default:
2023-01-12 16:04:12 +08:00
s, ok := status.FromError(e)
2023-01-11 19:26:17 +08:00
if !ok {
2023-01-12 16:16:50 +08:00
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: constant.ErrDefaultOther.ErrCode, ErrMsg: err.Error(), ErrDtl: fmt.Sprintf("%+v", err)})
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": err.Error(), "errDtl": fmt.Sprintf("%+v", err)})
2023-01-11 19:26:17 +08:00
return
}
var details []string
if err != e {
details = append(details, fmt.Sprintf("%+v", err))
}
for _, s := range s.Details() {
details = append(details, fmt.Sprintf("%+v", s))
}
2023-01-12 16:16:50 +08:00
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: int32(s.Code()), ErrMsg: s.Message(), ErrDtl: strings.Join(details, "\n")})
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": s.Code(), "errMsg": s.Message(), "errDtl": strings.Join(details, "\n")})
2022-12-30 19:46:26 +08:00
return
2022-12-30 18:46:02 +08:00
}
}
2023-01-12 16:16:50 +08:00
type baseResp struct {
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
ErrDtl string `json:"errDtl"`
Data interface{} `json:"data"`
}
2023-01-11 19:26:17 +08:00
//func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
2023-01-12 15:55:44 +08:00
// SetCtxInfo(ctx, funcName, err, args)
2023-01-11 19:26:17 +08:00
// e := new(constant.ErrInfo)
// switch {
// case errors.As(err, &e):
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": e.ErrCode, "errMsg": e.ErrMsg})
// return
// default:
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": constant.ErrDefaultOther.ErrMsg, "errDtl": err.Error()})
// return
// }
//}
func Unwrap(err error) error {
for err != nil {
unwrap, ok := err.(interface {
Unwrap() error
})
if !ok {
break
}
err = unwrap.Unwrap()
}
return err
}
2022-12-30 18:46:02 +08:00
type ApiInfo struct {
ApiName string
OperationID string
2023-01-03 14:08:29 +08:00
Funcs *[]FuncInfo
2022-12-30 19:46:26 +08:00
GinCtx *gin.Context
}
type FuncInfo struct {
FuncName string
2023-01-12 10:48:02 +08:00
Args Args
2022-12-30 19:46:26 +08:00
Err error
2023-01-12 15:55:44 +08:00
LogLevel logrus.Level
File string
2022-12-30 18:46:02 +08:00
}
2023-01-12 10:48:02 +08:00
type Args map[string]interface{}
2023-01-12 11:14:37 +08:00
func (a Args) String() string {
var s string
var hasElement bool
for k, v := range a {
if !hasElement {
s += "{"
hasElement = true
}
2023-01-12 11:23:07 +08:00
s += fmt.Sprintf("%s: %v", k, v)
2023-01-12 11:14:37 +08:00
}
if hasElement {
s += "}"
}
return s
}
2023-01-12 15:55:44 +08:00
func SetCtxDebug(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.DebugLevel, err, args)
}
func SetCtxInfo(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.InfoLevel, err, args)
}
func SetCtxWarn(ctx context.Context, funcName string, err error, args ...interface{}) {
SetContextInfo(ctx, funcName, logrus.WarnLevel, err, args)
}
func SetContextInfo(ctx context.Context, funcName string, logLevel logrus.Level, err error, args ...interface{}) {
2023-01-03 14:08:29 +08:00
t := ctx.Value(TraceLogKey).(*ApiInfo)
2022-12-30 19:46:26 +08:00
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
argsHandle(args, funcInfo.Args)
2023-01-06 13:41:50 +08:00
funcInfo.FuncName = funcName
2022-12-30 19:46:26 +08:00
funcInfo.Err = err
2023-01-12 15:55:44 +08:00
funcInfo.LogLevel = logLevel
2023-01-12 17:49:36 +08:00
_, file, line, _ := runtime.Caller(3)
2023-01-12 16:31:44 +08:00
var s string
2023-01-12 15:55:44 +08:00
i := strings.SplitAfter(file, "/")
if len(i) > 3 {
2023-01-12 17:49:36 +08:00
s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line)
2023-01-12 15:55:44 +08:00
}
2023-01-12 16:31:44 +08:00
funcInfo.File = s
2023-01-03 14:08:29 +08:00
*t.Funcs = append(*t.Funcs, funcInfo)
}
2023-01-04 11:06:28 +08:00
func SetRpcReqInfo(ctx context.Context, funcName string, req string) {
t := ctx.Value(TraceLogKey).(*ApiInfo)
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
var args []interface{}
args = append(args, " rpc req ", req)
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
*t.Funcs = append(*t.Funcs, funcInfo)
}
func SetRpcRespInfo(ctx context.Context, funcName string, resp string) {
t := ctx.Value(TraceLogKey).(*ApiInfo)
var funcInfo FuncInfo
funcInfo.Args = make(map[string]interface{})
var args []interface{}
args = append(args, " rpc resp ", resp)
argsHandle(args, funcInfo.Args)
funcInfo.FuncName = funcName
*t.Funcs = append(*t.Funcs, funcInfo)
}
2023-01-03 14:08:29 +08:00
func SetSuccess(ctx context.Context, funcName string, data interface{}) {
2023-01-12 15:55:44 +08:00
SetCtxInfo(ctx, funcName, nil, "data", data)
2023-01-12 10:48:02 +08:00
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "errDtl": "", "data": data})
2022-12-30 18:46:02 +08:00
}
func argsHandle(args []interface{}, fields map[string]interface{}) {
for i := 0; i < len(args); i += 2 {
if i+1 < len(args) {
2023-01-03 14:08:29 +08:00
fields[fmt.Sprintf("%v", args[i])] = fmt.Sprintf("%+v", args[i+1])
2022-12-30 18:46:02 +08:00
} else {
fields[fmt.Sprintf("%v", args[i])] = ""
}
}
}