rpc请求增加日志记录
This commit is contained in:
parent
54aef081d9
commit
6f37d3cad3
52
rpc.go
52
rpc.go
@ -17,6 +17,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
@ -266,26 +267,60 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
responseBody []byte
|
||||
code, message, data string
|
||||
)
|
||||
|
||||
// 日志数据
|
||||
logDataList := []zap.Field{
|
||||
zap.String("service_flag", serviceFlag),
|
||||
zap.String("service_api_flag", apiFlag),
|
||||
zap.Any("input_param", parameter),
|
||||
}
|
||||
defer func() {
|
||||
// 记录请求日志
|
||||
r.loggerRequest(ctx, logDataList...)
|
||||
}()
|
||||
if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err {
|
||||
logDataList = append(logDataList, zap.Any("read_api_config_fail", err.Error()))
|
||||
return err
|
||||
}
|
||||
logDataList = append(logDataList, zap.Any("api_config", apiConfig))
|
||||
|
||||
// 完整的请求地址
|
||||
fullURL, body = r.getFullURLAndBody(serviceConfig, apiConfig, parameter)
|
||||
|
||||
logDataList = append(logDataList, zap.String("full_utl", fullURL))
|
||||
// 获取客户端
|
||||
client = r.GetHttpClient(apiConfig.Header, apiConfig.Timeout)
|
||||
var bodyReader io.Reader
|
||||
if nil != body {
|
||||
logDataList = append(logDataList, zap.String("request_body", string(body)))
|
||||
bodyReader = bytes.NewReader(body)
|
||||
}
|
||||
|
||||
requestStartTime := time.Now().UnixNano() / 1e6
|
||||
requestFinishTime := int64(0)
|
||||
|
||||
logDataList = append(logDataList, zap.Int64("start_time", requestStartTime))
|
||||
if response, err = client.Do(apiConfig.Method, fullURL, apiConfig.Header, bodyReader); nil != err {
|
||||
requestFinishTime = time.Now().UnixNano() / 1e6
|
||||
logDataList = append(logDataList, zap.Int64("finish_time", requestFinishTime))
|
||||
logDataList = append(logDataList, zap.Int64("used_time", requestFinishTime-requestStartTime))
|
||||
logDataList = append(logDataList, zap.String("request_fail_reason", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
requestFinishTime = time.Now().UnixNano() / 1e6
|
||||
logDataList = append(logDataList, zap.Int64("finish_time", requestFinishTime))
|
||||
logDataList = append(logDataList, zap.Int64("used_time", requestFinishTime-requestStartTime))
|
||||
logDataList = append(logDataList, zap.Any("response_header", response.Header))
|
||||
logDataList = append(logDataList, zap.Any("response_http_code", response.StatusCode))
|
||||
|
||||
if responseBody, err = io.ReadAll(response.Body); nil != err {
|
||||
logDataList = append(logDataList, zap.String("read_body_fail_reason", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
logDataList = append(logDataList, zap.Any("response_body", string(responseBody)))
|
||||
|
||||
responseFieldCfg := r.getCodeAndMessageAndDataField(serviceConfig, apiConfig)
|
||||
successHttpCodeList, successBusinessCodeList := r.getSuccessHttpCodeAndSuccessBusinessCode(serviceConfig, apiConfig)
|
||||
if !r.httpCodeIsSuccess(response.StatusCode, successHttpCodeList) {
|
||||
@ -296,7 +331,9 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
||||
if !r.codeIsSuccess(code, successBusinessCodeList) {
|
||||
return fmt.Errorf("业务状态码异常 : %v -> %v", code, message)
|
||||
}
|
||||
return parseResponseBody(response.Header.Get("Content-type"), []byte(data), receiver)
|
||||
err = parseResponseBody(response.Header.Get("Content-Type"), []byte(data), receiver)
|
||||
logDataList = append(logDataList, zap.Any("response_body_parse_fail_reason", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
// GetHttpClient 获取client实例
|
||||
@ -493,3 +530,16 @@ func (r *request) getCodeAndMessageAndDataField(serviceInfo *Service, apiInfo *A
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// loggerRequest 记录请求日志
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:54 2022/7/1
|
||||
func (r *request) loggerRequest(ctx *gin.Context, dataList ...zap.Field) {
|
||||
if nil == r.logger {
|
||||
// 未设置日志实例
|
||||
return
|
||||
}
|
||||
r.logger.Info("API接口请求日志记录", dataList...)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user