From 6f37d3cad3a256e8b2a68d556b9f5d369d670120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 1 Jul 2022 18:26:11 +0800 Subject: [PATCH] =?UTF-8?q?rpc=E8=AF=B7=E6=B1=82=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/rpc.go b/rpc.go index 65e150f..7bb5fc4 100644 --- a/rpc.go +++ b/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...) +}