Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor error log for manager api #689

Merged
merged 22 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ const (
)

var (
ENV string
basePath string
Schema gjson.Result
DagLibPath = "/go/manager-api/dag-to-lua/"
ServerHost = "127.0.0.1"
ServerPort = 80
ETCDEndpoints = "127.0.0.1:2379"
ENV string
basePath string
Schema gjson.Result
DagLibPath = "/go/manager-api/dag-to-lua/"
ServerHost = "127.0.0.1"
ServerPort = 80
ETCDEndpoints = "127.0.0.1:2379"
ErrorLogLevel = "warn"
ErrorLogPath = "./logs/error.log"
LogRotateInterval int64 = 86400 //second
LogRotateMaxSize int64 = 100000000 //byte
LogRotateMaxAge int64 = 2592000 //second
)

func init() {
Expand All @@ -68,6 +73,7 @@ func setConf() {
if serverPort != 0 {
ServerPort = serverPort
}

serverHost := configuration.Get("conf.listen.host").String()
if serverHost != "" {
ServerHost = serverHost
Expand All @@ -78,12 +84,38 @@ func setConf() {
if dagLibPath != "" {
DagLibPath = dagLibPath
}

//etcd
eTCDEndpoints := configuration.Get("conf.etcd.endpoints").String()
if eTCDEndpoints != "" {
ETCDEndpoints = eTCDEndpoints
}

//log
logLevel := configuration.Get("conf.log.error_log.level").String()
membphis marked this conversation as resolved.
Show resolved Hide resolved
if logLevel != "" {
ErrorLogLevel = logLevel
}

errorLogPath := configuration.Get("conf.log.error_log.file_path").String()
if errorLogPath != "" {
ErrorLogPath = errorLogPath
}

rotateInterval := configuration.Get("conf.log.rotate.interval").Int()
if rotateInterval > 0 {
LogRotateInterval = rotateInterval
}

rotateMaxSize := configuration.Get("conf.log.rotate.max_size").Int()
if rotateMaxSize > 0 {
LogRotateMaxSize = rotateMaxSize
}

rotateMaxAge := configuration.Get("conf.log.rotate.max_age").Int()
if rotateMaxAge > 0 {
LogRotateMaxAge = rotateMaxAge
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions api/conf/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
"dag-lib-path": "",
"etcd": {
"endpoints": "127.0.0.1:2379"
},
"log": {
"error_log": {
"level": "warn",
"file_path": "./logs/error.log"
},
"rotate": {
membphis marked this conversation as resolved.
Show resolved Hide resolved
"interval": 86400,
"max_size": 100000000,
"max_age": 2592000
}
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
}
},
"authentication": {
Expand Down
106 changes: 51 additions & 55 deletions api/filter/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,61 @@ package filter

import (
"bytes"
"io/ioutil"
"time"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet/log"
"github.com/sirupsen/logrus"
)

func RequestLogHandler() gin.HandlerFunc {
return func(c *gin.Context) {
start, host, remoteIP, path, method := time.Now(), c.Request.Host, c.ClientIP(), c.Request.URL.Path, c.Request.Method
var val interface{}
if method == "GET" {
val = c.Request.URL.Query()
} else {
val, _ = c.GetRawData()

// set RequestBody back
c.Request.Body = ioutil.NopCloser(bytes.NewReader(val.([]byte)))
}
c.Set("requestBody", val)
uuid, _ := c.Get("X-Request-Id")

param, _ := c.Get("requestBody")

switch paramType := param.(type) {
case []byte:
param = string(param.([]byte))
log.Infof("type of param: %#v", paramType)
default:
}

blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
c.Next()
latency := time.Since(start) / 1000000
statusCode := c.Writer.Status()
respBody := blw.body.String()
if uuid == "" {
uuid = c.Writer.Header().Get("X-Request-Id")
}
var errs []string
for _, err := range c.Errors {
errs = append(errs, err.Error())
}
logger.WithFields(logrus.Fields{
"requestId": uuid,
"latency": latency,
"remoteIp": remoteIP,
"method": method,
"path": path,
"statusCode": statusCode,
"host": host,
"params": param,
"respBody": respBody,
"errMsg": errs,
}).Info("")
}
}
//func RequestLogHandler() gin.HandlerFunc {
membphis marked this conversation as resolved.
Show resolved Hide resolved
// //return func(c *gin.Context) {
// // start, host, remoteIP, path, method := time.Now(), c.Request.Host, c.ClientIP(), c.Request.URL.Path, c.Request.Method
// // var val interface{}
// // if method == "GET" {
// // val = c.Request.URL.Query()
// // } else {
// // val, _ = c.GetRawData()
// //
// // // set RequestBody back
// // c.Request.Body = ioutil.NopCloser(bytes.NewReader(val.([]byte)))
// // }
// // c.Set("requestBody", val)
// // uuid, _ := c.Get("X-Request-Id")
// //
// // param, _ := c.Get("requestBody")
// //
// // switch paramType := param.(type) {
// // case []byte:
// // param = string(param.([]byte))
// // log.Infof("type of param: %#v", paramType)
// // default:
// // }
// //
// // blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
// // c.Writer = blw
// // c.Next()
// // latency := time.Since(start) / 1000000
// // statusCode := c.Writer.Status()
// // respBody := blw.body.String()
// // if uuid == "" {
// // uuid = c.Writer.Header().Get("X-Request-Id")
// // }
// // var errs []string
// // for _, err := range c.Errors {
// // errs = append(errs, err.Error())
// // }
// // logger.With(logrus.Fields{
// // "requestId": uuid,
// // "latency": latency,
// // "remoteIp": remoteIP,
// // "method": method,
// // "path": path,
// // "statusCode": statusCode,
// // "host": host,
// // "params": param,
// // "respBody": respBody,
// // "errMsg": errs,
// // }).Info("")
// //}
//}

type bodyLogWriter struct {
gin.ResponseWriter
Expand Down
15 changes: 7 additions & 8 deletions api/filter/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import (

"github.com/apisix/manager-api/log"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

var (
logger = log.GetLogger()
dunno = []byte("???")
centerDot = []byte("·")
dot = []byte(".")
Expand All @@ -41,12 +39,13 @@ func RecoverHandler() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
uuid := c.Writer.Header().Get("X-Request-Id")
logger.WithFields(logrus.Fields{
"uuid": uuid,
})
fmt.Println("err;", err)
//uuid := c.Writer.Header().Get("X-Request-Id")
stack := stack(3)
logger.Errorf("[Recovery] %s panic recovered:\n\n%s\n%s", timeFormat(time.Now()), err, stack)
fmt.Printf("[Recovery] %s panic recovered:\n\n%s\n%s", timeFormat(time.Now()), err, stack)

//log.With(zap.String("uuid", uuid))
log.Errorf("[Recovery] %s panic recovered:\n\n%s\n%s", timeFormat(time.Now()), err, stack)
c.AbortWithStatus(http.StatusInternalServerError)
}
}()
Expand All @@ -58,7 +57,7 @@ func WrapGo(f func(...interface{}), args ...interface{}) {
defer func() {
if err := recover(); err != nil {
stack := stack(3)
logger.Errorf("[Recovery] %s panic recovered:\n\n%s\n%s", timeFormat(time.Now()), err, stack)
log.Errorf("[Recovery] %s panic recovered:\n\n%s\n%s", timeFormat(time.Now()), err, stack)
}
}()
f(args...)
Expand Down
3 changes: 3 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.3 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/satori/go.uuid v1.2.0
github.com/shiningrush/droplet v0.2.1
github.com/shiningrush/droplet/wrapper/gin v0.2.0
Expand Down
8 changes: 8 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.3 h1:qqOPU7y+TM8Y803I8fG9c/DyKG3xH/xkng6keC1015Q=
github.com/lestrrat-go/strftime v1.0.3/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand All @@ -97,6 +102,9 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
4 changes: 3 additions & 1 deletion api/internal/core/store/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package store

import (
"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/log"
)

type Query struct {
Expand Down Expand Up @@ -88,7 +89,7 @@ func NewQuery(sort *Sort, filter *Filter, pagination *Pagination) *Query {

func NewSort(sortRaw []string) *Sort {
if sortRaw == nil || len(sortRaw)%2 == 1 {
// Empty sort list or invalid (odd) length
log.Info("empty sort for query")
return NoSort
}
list := []SortBy{}
Expand Down Expand Up @@ -117,6 +118,7 @@ func NewSort(sortRaw []string) *Sort {

func NewFilter(filterRaw []string) *Filter {
if filterRaw == nil || len(filterRaw)%2 == 1 {
log.Info("empty filter for query")
return NoFilter
}
list := []FilterBy{}
Expand Down
Loading