From 716428dd583d4c96f1e23d15641ed143785c05ee Mon Sep 17 00:00:00 2001 From: Leonid Nikolaev Date: Tue, 6 Jun 2023 16:12:19 +0300 Subject: [PATCH] =?UTF-8?q?MSP-1777=20[+]=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20=D0=B2=20?= =?UTF-8?q?remote=5Fconfig:=20logging.skip.=20=D0=9C=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B2=20re.=20=D0=95=D1=81=D0=BB=D0=B8=20=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=B2=20=D0=BF=D1=83=D1=82?= =?UTF-8?q?=D0=B8,=20=D1=82=D0=BE=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81?= =?UTF-8?q?=20=D0=BD=D0=B5=20=D0=B1=D1=83=D0=B4=D0=B5=D1=82=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly/locator.go | 24 ++++++++++++++++++++++-- conf/default_remote_config.json | 30 +++++++++++++++++------------- conf/remote.go | 1 + go.mod | 2 +- middleware/logger.go | 10 +++++++++- tests/acceptance_test.go | 7 ++++--- 6 files changed, 54 insertions(+), 20 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index 5d78667..c9a5b86 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -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 +} diff --git a/conf/default_remote_config.json b/conf/default_remote_config.json index 35dbcd3..e21b9b9 100644 --- a/conf/default_remote_config.json +++ b/conf/default_remote_config.json @@ -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 + } } diff --git a/conf/remote.go b/conf/remote.go index 5e10c2a..beb2136 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -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 { diff --git a/go.mod b/go.mod index d1814e8..614d15f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/middleware/logger.go b/middleware/logger.go index 75b6233..d37ba0c 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -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 { diff --git a/tests/acceptance_test.go b/tests/acceptance_test.go index 7157e07..464b60c 100644 --- a/tests/acceptance_test.go +++ b/tests/acceptance_test.go @@ -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}},