MSP-1777 [+] новый параметр в remote_config: logging.skip. Массив re. Если они есть в пути, то запрос не будет логироваться

This commit is contained in:
Leonid Nikolaev committed 2023-06-06 16:12:19 +03:00
1 parent 9e68e9662c
commit 716428dd58
6 files changed
+54 -20

No files matched your search

+22 -2
View File
@@ -2,6 +2,7 @@ package assembly
import (
"net/http"
"regexp"
"time"
mux2 "github.com/gorilla/mux"
@@ -64,6 +65,11 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
throttlingRepo := repository.NewThrottling(redisCli)
throttlingService := service.NewThrottling(throttlingRepo, config.Throttling)
skipRegexps, err := makeSkipRegExps(config.Logging.Skip)
if err != nil {
return nil, errors.WithMessage(err, "compile regexp")
}
mux := mux2.NewRouter()
for _, location := range locations {
var proxyFunc middleware.Handler
@@ -87,7 +93,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
handler := middleware.Chain(
proxyFunc,
middleware.RequestId(),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, skipRegexps),
middleware.ErrorHandler(l.logger),
middleware.Authenticate(authentication),
middleware.AdminAuthenticate(adminService),
@@ -100,7 +106,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
handler = middleware.Chain(
proxyFunc,
middleware.RequestId(),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, skipRegexps),
middleware.ErrorHandler(l.logger),
)
}
@@ -116,3 +122,17 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
return mux, nil
}
func makeSkipRegExps(skip []string) ([]*regexp.Regexp, error) {
ret := make([]*regexp.Regexp, 0)
for _, s := range skip {
re, err := regexp.Compile(s)
if err != nil {
return nil, errors.WithMessagef(err, "re=[%s]", s)
}
ret = append(ret, re)
}
return ret, nil
}
+17 -13
View File
@@ -1,15 +1,19 @@
{
"logging": {
"logLevel": "debug",
"requestLogEnable": true,
"bodyLogEnable": false
},
"caching": {
"authenticationDataInSec": 60,
"authorizationDataInSec": 60
},
"http": {
"maxRequestBodySizeInMb": 64,
"proxyTimeoutInSec": 60
}
"logging": {
"logLevel": "debug",
"requestLogEnable": true,
"bodyLogEnable": false,
"skip": [
"endpoint_one",
"endpoint_two"
]
},
"caching": {
"authenticationDataInSec": 60,
"authorizationDataInSec": 60
},
"http": {
"maxRequestBodySizeInMb": 64,
"proxyTimeoutInSec": 60
}
}
+1
View File
@@ -34,6 +34,7 @@ type Logging struct {
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования,логирование запросов осуществляется на уровне debug"`
RequestLogEnable bool `schema:"Включить логирование запросов"`
BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"`
Skip []string `valid:"omitempty" schema:"регулярные выражения для отключения логирования"`
}
type Caching struct {
+1 -1
View File
@@ -5,6 +5,7 @@ go 1.19
require (
github.com/go-redis/redis_rate/v10 v10.0.0
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/integration-system/isp-etp-go/v2 v2.1.3
github.com/integration-system/isp-kit v1.17.0
@@ -27,7 +28,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
+9 -1
View File
@@ -6,6 +6,7 @@ import (
"io"
"net"
"net/http"
"regexp"
"github.com/integration-system/isp-kit/http/endpoint/buffer"
"github.com/integration-system/isp-kit/log"
@@ -42,7 +43,7 @@ func (w *writerWrapper) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
}
func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool) Middleware {
func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool, skip []*regexp.Regexp) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx *request.Context) error {
if !enableRequestLogging {
@@ -50,6 +51,13 @@ func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool
}
r := ctx.Request()
for _, re := range skip {
if re.MatchString(r.URL.Path) {
return next.Handle(ctx)
}
}
var scSrc scSource
var buf *buffer.Buffer
if enableBodyLogging {
+4 -3
View File
@@ -214,9 +214,10 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
})
config := conf.Remote{
Redis: &conf.Redis{Address: redisCli.address},
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true},
Redis: &conf.Redis{Address: redisCli.address},
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true,
Skip: []string{"endpoint", "ws.service"}},
Caching: conf.Caching{AuthorizationDataInSec: 1, AuthenticationDataInSec: 1},
DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100}},
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 100}},