This commit is contained in:
withchao 2023-01-12 10:42:27 +08:00
parent 94eeb3abb2
commit 59c477424c
2 changed files with 16 additions and 7 deletions

View File

@ -3,6 +3,8 @@ package common
import ( import (
"Open_IM/pkg/common/trace_log" "Open_IM/pkg/common/trace_log"
"Open_IM/pkg/getcdv3" "Open_IM/pkg/getcdv3"
utils2 "Open_IM/pkg/utils"
"fmt"
utils "github.com/OpenIMSDK/open_utils" utils "github.com/OpenIMSDK/open_utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
@ -10,15 +12,15 @@ import (
) )
func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string, tokenFunc func(token string, operationID string) (string, error)) { func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string, tokenFunc func(token string, operationID string) (string, error)) {
logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
operationID := c.GetHeader("operationID") operationID := c.GetHeader("operationID")
nCtx := trace_log.NewCtx1(c, rpcFuncName, operationID) nCtx := trace_log.NewCtx1(c, rpcFuncName, operationID)
//trace_log.SetOperationID(nCtx, operationID)
defer trace_log.ShowLog(nCtx) defer trace_log.ShowLog(nCtx)
if err := c.BindJSON(apiReq); err != nil { if err := c.BindJSON(apiReq); err != nil {
trace_log.WriteErrorResponse(nCtx, "BindJSON", err) trace_log.WriteErrorResponse(nCtx, "BindJSON", err)
return return
} }
trace_log.SetContextInfo(nCtx, "BindJSON", nil, "params", apiReq) trace_log.SetContextInfo(nCtx, logFuncName, nil, "apiReq", apiReq)
etcdConn, err := getcdv3.GetConn(nCtx, rpcName) etcdConn, err := getcdv3.GetConn(nCtx, rpcName)
if err != nil { if err != nil {
trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err) trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err)
@ -41,6 +43,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
trace_log.WriteErrorResponse(nCtx, "CopyStructFields_RpcReq", err) trace_log.WriteErrorResponse(nCtx, "CopyStructFields_RpcReq", err)
return return
} }
trace_log.SetContextInfo(nCtx, logFuncName, nil, "opUserID", opUserID, "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
md := metadata.Pairs("operationID", operationID, "opUserID", opUserID) md := metadata.Pairs("operationID", operationID, "opUserID", opUserID)
respArr := rpc.Call([]reflect.Value{ respArr := rpc.Call([]reflect.Value{
reflect.ValueOf(metadata.NewOutgoingContext(c, md)), // context.Context reflect.ValueOf(metadata.NewOutgoingContext(c, md)), // context.Context
@ -48,20 +51,26 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
}) // respArr => (apiResp, error) }) // respArr => (apiResp, error)
if !respArr[1].IsNil() { // rpc err != nil if !respArr[1].IsNil() { // rpc err != nil
err := respArr[1].Interface().(error) err := respArr[1].Interface().(error)
trace_log.WriteErrorResponse(nCtx, rpcFuncName, err, "rpc req", rpcReqPtr.Interface()) trace_log.WriteErrorResponse(nCtx, rpcFuncName, err, "callRpcResp", "error")
return return
} }
rpcResp := respArr[0].Elem() rpcResp := respArr[0].Elem()
trace_log.SetContextInfo(nCtx, rpcFuncName, nil, "rpc req", rpcReqPtr.Interface(), "resp", rpcResp.Interface()) trace_log.SetContextInfo(nCtx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
if apiResp != nil { if apiResp != nil {
if err := utils.CopyStructFields(apiResp, rpcResp.Interface()); err != nil { if err := utils.CopyStructFields(apiResp, rpcResp.Interface()); err != nil {
trace_log.WriteErrorResponse(nCtx, "CopyStructFields_RpcResp", err) trace_log.SetContextInfo(nCtx, "CopyStructFields_RpcResp", err, "apiResp", fmt.Sprintf("%T", apiResp), "rpcResp", fmt.Sprintf("%T", rpcResp.Interface()))
return
} }
} }
trace_log.SetSuccess(nCtx, rpcFuncName, apiResp) trace_log.SetSuccess(nCtx, rpcFuncName, apiResp)
} }
func rpcString(v interface{}) string {
if s, ok := v.(interface{ String() string }); ok {
return s.String()
}
return fmt.Sprintf("%+v", v)
}
//func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, fn interface{}, rpcFuncName string, tokenFunc func(token string, operationID string) (string, error)) { //func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, fn interface{}, rpcFuncName string, tokenFunc func(token string, operationID string) (string, error)) {
// nCtx := trace_log.NewCtx(c, rpcFuncName) // nCtx := trace_log.NewCtx(c, rpcFuncName)
// defer trace_log.ShowLog(nCtx) // defer trace_log.ShowLog(nCtx)

View File

@ -77,7 +77,7 @@ func WriteErrorResponse(ctx context.Context, funcName string, err error, args ..
for _, s := range s.Details() { for _, s := range s.Details() {
details = append(details, fmt.Sprintf("%+v", s)) details = append(details, fmt.Sprintf("%+v", s))
} }
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": s.Code(), "errMsg": s.String(), "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")})
return return
} }
} }