Skip to content

Commit

Permalink
Merge branch 'dev_2473_logging_guidelines' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjiawei committed Dec 10, 2024
2 parents d1acc30 + a1e35da commit 7b305c2
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 108 deletions.
49 changes: 0 additions & 49 deletions platform-auth-server/api/middleware/log.go

This file was deleted.

51 changes: 46 additions & 5 deletions platform-auth-server/api/routers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package api

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
"time"

"github.com/WeBankPartners/wecube-platform/platform-auth-server/api/middleware"
"github.com/WeBankPartners/wecube-platform/platform-auth-server/common/constant"
"github.com/WeBankPartners/wecube-platform/platform-auth-server/common/log"
"github.com/WeBankPartners/wecube-platform/platform-auth-server/model"

"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -35,9 +40,9 @@ func NewRouter() *gin.Engine {
router := gin.Default()

if model.Config.Log.AccessLogEnable {
router.Use(middleware.HttpLogHandle())
router.Use(HttpLogHandle())
}
router.Use(gin.CustomRecovery(middleware.RecoverHandle))
router.Use(gin.CustomRecovery(RecoverHandle))

authMap := buildAuthMap()
authoritiesFetcher := func(path string, method string) []string {
Expand All @@ -61,9 +66,8 @@ func NewRouter() *gin.Engine {
case http.MethodDelete:
group.DELETE(funcObj.Url, funcObj.HandlerFunc)
}
apiCodeMap[fmt.Sprintf("%s_%s", funcObj.Url, funcObj.Method)] = funcObj.ApiCode
apiCodeMap[fmt.Sprintf("%s_%s%s", funcObj.Method, constant.UrlPrefix, funcObj.Url)] = funcObj.ApiCode
}

return router
}

Expand All @@ -76,6 +80,43 @@ func buildAuthMap() map[string][]string {
return authMap
}

func HttpLogHandle() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
c.Request.Body.Close()
c.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
c.Set("requestBody", string(bodyBytes))
log.Logger.Info("Received request ", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.String("body", c.GetString("requestBody")))
if strings.EqualFold(model.Config.Log.Level, "debug") {
requestDump, _ := httputil.DumpRequest(c.Request, true)
log.Logger.Debug("Received request: " + string(requestDump))
}
apiCode := apiCodeMap[c.Request.Method+"_"+c.FullPath()]
c.Writer.Header().Add("Api-Code", apiCode)
c.Next()
costTime := time.Since(start).Seconds() * 1000
log.AccessLogger.Info("Got request -", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody")))
}
}

func getRemoteIp(c *gin.Context) string {
netIp := c.RemoteIP()
if len(netIp) > 0 {
return netIp
}
return c.ClientIP()
}

func RecoverHandle(c *gin.Context, err interface{}) {
var errorMessage string
if err != nil {
errorMessage = err.(error).Error()
}
log.Logger.Error("Handle error", log.Int("errorCode", 1), log.String("message", errorMessage))
c.JSON(http.StatusInternalServerError, model.ResponseWrap{ErrorCode: 1, Status: model.ResponseStatusError})
}

func init() {
httpHandlerFuncList = append(httpHandlerFuncList, // contract instance
&handlerFuncObj{Url: constant.UriLogin, Method: http.MethodPost, HandlerFunc: Login,
Expand Down
2 changes: 2 additions & 0 deletions platform-auth-server/api/support/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/WeBankPartners/wecube-platform/platform-auth-server/common/log"
"github.com/WeBankPartners/wecube-platform/platform-auth-server/model"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -44,6 +45,7 @@ func ReturnErrorWithHttpCode(c *gin.Context, err error, httpCode int) {
}
bodyBytes, _ := json.Marshal(errorResponse)
c.Set("responseBody", string(bodyBytes))
c.Writer.Header().Add("Error-Code", strconv.Itoa(errorResponse.ErrorCode))
c.JSON(httpCode, errorResponse)
}

Expand Down
5 changes: 0 additions & 5 deletions platform-auth-server/common/exterror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,3 @@ func buildErrMessage(templateMessage string, params []interface{}) (message stri
func IsBusinessErrorCode(errorCode int) bool {
return strings.HasPrefix(fmt.Sprintf("%d", errorCode), "2")
}

func IsCustomError(err error) bool {
_, ok := err.(CustomError)
return ok
}
1 change: 1 addition & 0 deletions platform-core/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func httpLogHandle() gin.HandlerFunc {
c.Set(models.ContextRequestBody, string(bodyBytes))
}
apiCode := apiCodeMap[c.Request.Method+"_"+c.FullPath()]
c.Writer.Header().Add("Api-Code", apiCode)
log.AccessLogger.Info(fmt.Sprintf("[%s] [%s] ->", requestId, transactionId), log.String("uri", c.Request.RequestURI), log.String("serviceCode", apiCode), log.String("method", c.Request.Method), log.String("sourceIp", getRemoteIp(c)), log.String(models.ContextOperator, c.GetString(models.ContextOperator)), log.String(models.ContextRequestBody, c.GetString(models.ContextRequestBody)))
c.Next()
costTime := time.Since(start).Seconds() * 1000
Expand Down
2 changes: 2 additions & 0 deletions platform-core/api/middleware/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/WeBankPartners/wecube-platform/platform-core/models"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)

const AcceptLanguageHeader = "Accept-Language"
Expand Down Expand Up @@ -84,6 +85,7 @@ func ReturnError(c *gin.Context, err error) {
c.Set(models.ContextErrorKey, errorKey)
c.Set(models.ContextErrorCode, errorCode)
c.Set(models.ContextErrorMessage, errorMessage)
c.Writer.Header().Add("Error-Code", strconv.Itoa(errorCode))
c.JSON(http.StatusOK, returnObj)
}

Expand Down
47 changes: 46 additions & 1 deletion platform-gateway/api/middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func HttpLogHandle() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
var errCode string
if strings.EqualFold(model.Config.Log.Level, "debug") && c.Request.RequestURI != "/platform/v1/packages" && !strings.HasSuffix(c.Request.RequestURI, "/packages/upload") {
bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
c.Request.Body.Close()
Expand All @@ -29,7 +30,51 @@ func HttpLogHandle() gin.HandlerFunc {
}
c.Next()
costTime := time.Now().Sub(start).Seconds() * 1000
log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody")))
apiCode := c.Writer.Header().Get("Api-Code")
// 业务错误码
var subCode, subErrorCode string
errCode = c.Writer.Header().Get("Error-Code")
if strings.HasPrefix(c.Request.RequestURI, "/platform") {
subCode = model.Config.SubSystemCode.Core
apiCode = fmt.Sprintf("platform_%s", apiCode)
// platform-core 错误码,以1开头表示技术类错误,其他则是业务类错误,本身就是8位,不需要扩展
if strings.HasPrefix(errCode, "1") {
// 技术错误
subErrorCode = subCode + fmt.Sprintf("T%s", errCode)
} else {
// 业务错误
subErrorCode = subCode + fmt.Sprintf("B%s", errCode)
}
} else if strings.HasPrefix(c.Request.RequestURI, "/auth") {
subCode = model.Config.SubSystemCode.Auth
apiCode = fmt.Sprintf("auth_%s", apiCode)
// platform-auth 错误码,以3开头表示业务类错误,其他则是技术类错误,本身4位,需要前面扩展4个0
if strings.HasPrefix(errCode, "3") {
// 业务错误
subErrorCode = subCode + fmt.Sprintf("B0000%s", errCode)
} else {
// 非业务错误
subErrorCode = subCode + fmt.Sprintf("T0000%s", errCode)
}
} else {
subCode = model.Config.SubSystemCode.Plugin
apiCode = fmt.Sprintf("plugin_%s", apiCode)
}
if c.Writer.Status() == 200 {
// 状态码 200 需要区分是否为业务错误
if strings.TrimSpace(errCode) == "" {
// errCode 为空,表示请求正常,对应errCode=0
errCode = "0"
} else {
errCode = subErrorCode
}
} else {
// 状态码 不为200 都是技术错误,T表示技术类错误
errCode = model.Config.SubSystemCode.Core + fmt.Sprintf("T00000%d", c.Writer.Status())
}
log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("url", c.Request.RequestURI), log.String("apiCode", apiCode), log.String("method", c.Request.Method),
log.Int("code", c.Writer.Status()), log.String("errCode", errCode), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime),
log.String("body", c.GetString("responseBody")))

}
}
Expand Down
32 changes: 0 additions & 32 deletions platform-gateway/api/middleware/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/WeBankPartners/wecube-platform/platform-gateway/api/support"
"github.com/WeBankPartners/wecube-platform/platform-gateway/common/log"
"github.com/WeBankPartners/wecube-platform/platform-gateway/common/network"
"github.com/WeBankPartners/wecube-platform/platform-gateway/model"
"github.com/gin-gonic/gin"
"math/rand"
Expand Down Expand Up @@ -78,27 +77,6 @@ func Redirect() gin.HandlerFunc {
break
}
}
//validIdx := -1
//for i := range rules {
// targetUrl := rules[i].TargetPath + uri
// invoke := support.RedirectInvoke{
// TargetUrl: targetUrl,
// }
// err := invoke.Do(c)
// if err != nil {
// log.Logger.Warn("failed to request", log.String("targetUrl", targetUrl), log.Error(err))
// } else {
// validIdx = i
// break
// }
//}
//
//if validIdx > 0 {
// tmp := rules[validIdx]
// rules[validIdx] = rules[0]
// rules[0] = tmp
// redirectRuleMap.Store(requestKey, rules)
//}

if len(rules) > 0 {
} else {
Expand All @@ -111,16 +89,6 @@ func Redirect() gin.HandlerFunc {
}
}

func isRemoteRejected(err error) bool {
if network.IsNetworkTimeout(err) {
return true
}
if network.IsConnReset(err) {
return true
}
return false
}

func AddRedirectRule(context string, rules []RedirectRule) {
log.Logger.Info("add redirect route context: " + context)

Expand Down
19 changes: 3 additions & 16 deletions platform-gateway/api/support/redirect_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error {

log.Logger.Info(fmt.Sprintf("Redirecting request to downstream system: [Method: %s] [URL: %s] [ContentLength: %d]", c.Request.Method, invoke.TargetUrl, c.Request.ContentLength))
cloneRequest := c.Request.Clone(c.Request.Context()) // deep copy original request

/* if invoke.RequestHandler != nil {
log.Logger.Info(fmt.Sprintf("Start to handle request [%s]-[%s]", c.GetHeader(constant.TransactionId), c.GetHeader(constant.RequestId)))
if err := invoke.RequestHandler(cloneRequest, c); err != nil {
ReturnError(c, exterror.Catch(exterror.New().ServerHandleError, fmt.Errorf("failed handle request: %s", err.Error())))
return
}
}
*/
newRequest, _ := http.NewRequest(cloneRequest.Method, invoke.TargetUrl, cloneRequest.Body)
newRequest.Header = cloneRequest.Header
// pass through content length
Expand All @@ -44,7 +35,6 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error {
newRequest.Header.Set("X-Forwarded-For", clientIp)
}
newRequest.URL.RawQuery = cloneRequest.URL.RawQuery
//auth.SetRequestSourceAuth(newRequest, config.Config.Auth.Source.AppId, config.Config.Auth.Source.PrivateKeyBytes)

client := &http.Client{
Timeout: 30 * time.Minute,
Expand All @@ -70,15 +60,12 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error {
responseDump, _ := httputil.DumpResponse(response, true)
log.Logger.Debug(fmt.Sprintf("Response from downstream system: %s [body size]: %d", string(responseDump), len(respBody)))
}

/* if response.StatusCode >= 400 {
ReturnError(c, fmt.Errorf("error from downstream system: %s", response.Status))
return
}*/

respHeader := c.Writer.Header()
for k, v := range response.Header {
respHeader[k] = v
if (k == "Api-Code" || k == "Error-Code") && len(v) > 0 {
c.Writer.Header().Add(k, v[0])
}
}
c.Data(response.StatusCode, response.Header.Get("Content-Type"), respBody)

Expand Down
5 changes: 5 additions & 0 deletions platform-gateway/build/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
],
"proxy_config": {
"timeout":[#HTTP_TIMEOUT_MINUTE]
},
"sub_system_code": {
"auth":"[#WECUBE_SUB_SYSTEM_AUTH_CODE]",
"core":"[#WECUBE_SUB_SYSTEM_CORE_CODE]",
"plugin":"[#WECUBE_SUB_SYSTEM_PLUGIN_CODE]"
}
}
3 changes: 3 additions & 0 deletions platform-gateway/build/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ sed -i "s~\[#GATEWAY_ROUTE_CONFIG_URI\]~$GATEWAY_ROUTE_CONFIG_URI~g" /app/platfo
sed -i "s~\[#WECUBE_CORE_ADDRESS\]~$WECUBE_CORE_ADDRESS~g" /app/platform-gateway/config/default.json
sed -i "s~\[#AUTH_SERVER_ADDRESS\]~$AUTH_SERVER_ADDRESS~g" /app/platform-gateway/config/default.json
sed -i "s~\[#HTTP_TIMEOUT_MINUTE\]~$HTTP_TIMEOUT_MINUTE~g" /app/platform-gateway/config/default.json
sed -i "s~\[#WECUBE_SUB_SYSTEM_AUTH_CODE\]~$WECUBE_SUB_SYSTEM_AUTH_CODE~g" /app/platform-gateway/config/default.json
sed -i "s~\[#WECUBE_SUB_SYSTEM_CORE_CODE\]~$WECUBE_SUB_SYSTEM_CORE_CODE~g" /app/platform-gateway/config/default.json
sed -i "s~\[#WECUBE_SUB_SYSTEM_PLUGIN_CODE\]~$WECUBE_SUB_SYSTEM_PLUGIN_CODE~g" /app/platform-gateway/config/default.json

exec ./platform-gateway
3 changes: 3 additions & 0 deletions platform-gateway/docker-compose.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ services:
- WECUBE_CORE_ADDRESS=[#WECUBE_CORE_ADDRESS]
- AUTH_SERVER_ADDRESS=[#AUTH_SERVER_ADDRESS]
- HTTP_TIMEOUT_MINUTE=[#HTTP_TIMEOUT_MINUTE]
- WECUBE_SUB_SYSTEM_AUTH_CODE=[#WECUBE_SUB_SYSTEM_AUTH_CODE]
- WECUBE_SUB_SYSTEM_CORE_CODE=[#WECUBE_SUB_SYSTEM_CORE_CODE]
- WECUBE_SUB_SYSTEM_PLUGIN_CODE=[#WECUBE_SUB_SYSTEM_PLUGIN_CODE]
7 changes: 7 additions & 0 deletions platform-gateway/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type RedirectRoute struct {
TargetPath string `json:"target_path"`
}

type SubSystemCode struct {
Auth string `json:"auth"`
Core string `json:"core"`
Plugin string `json:"plugin"`
}

type GlobalConfig struct {
ServerAddress string `json:"server_address"`
ServerPort string `json:"server_port"`
Expand All @@ -51,6 +57,7 @@ type GlobalConfig struct {
Remote RemoteServiceConfig `json:"remote_service"`
ProxyConfig ProxyConfig `json:"proxy_config"`
RedirectRoutes []RedirectRoute `json:"redirect_routes"`
SubSystemCode SubSystemCode `json:"sub_system_code"`
}

var (
Expand Down

0 comments on commit 7b305c2

Please sign in to comment.