diff --git a/.version b/.version index 4d0dcda..6aba2b2 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -4.1.2 +4.2.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index d520486..b948536 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### v4.2.0 +* новый параметр в remote_config: `skipBodyLoggingEndpointPrefixes`. Массив re. Если они есть в пути, то запрос не будет логироваться ### v4.1.2 * исправлена опция логирования тела запроса (при настройке ws могла отключаться) ### v4.1.1 diff --git a/assembly/locator.go b/assembly/locator.go index 5d78667..506c574 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -87,7 +87,8 @@ 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, + config.Logging.SkipBodyLoggingEndpointPrefixes), middleware.ErrorHandler(l.logger), middleware.Authenticate(authentication), middleware.AdminAuthenticate(adminService), @@ -100,7 +101,8 @@ 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, + config.Logging.SkipBodyLoggingEndpointPrefixes), middleware.ErrorHandler(l.logger), ) } diff --git a/conf/default_remote_config.json b/conf/default_remote_config.json index 35dbcd3..8860eb2 100644 --- a/conf/default_remote_config.json +++ b/conf/default_remote_config.json @@ -1,15 +1,16 @@ { - "logging": { - "logLevel": "debug", - "requestLogEnable": true, - "bodyLogEnable": false - }, - "caching": { - "authenticationDataInSec": 60, - "authorizationDataInSec": 60 - }, - "http": { - "maxRequestBodySizeInMb": 64, - "proxyTimeoutInSec": 60 - } + "logging": { + "logLevel": "debug", + "requestLogEnable": true, + "bodyLogEnable": false, + "skipBodyLoggingEndpointPrefixes": [] + }, + "caching": { + "authenticationDataInSec": 60, + "authorizationDataInSec": 60 + }, + "http": { + "maxRequestBodySizeInMb": 64, + "proxyTimeoutInSec": 60 + } } diff --git a/conf/remote.go b/conf/remote.go index 5e10c2a..bb1f630 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -31,9 +31,10 @@ type Http struct { } type Logging struct { - LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования,логирование запросов осуществляется на уровне debug"` - RequestLogEnable bool `schema:"Включить логирование запросов"` - BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"` + LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования,логирование запросов осуществляется на уровне debug"` + RequestLogEnable bool `schema:"Включить логирование запросов"` + BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"` + SkipBodyLoggingEndpointPrefixes []string `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..792ac1c 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -6,6 +6,7 @@ import ( "io" "net" "net/http" + "strings" "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 []string) Middleware { return func(next Handler) Handler { return HandlerFunc(func(ctx *request.Context) error { if !enableRequestLogging { @@ -50,6 +51,7 @@ func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool } r := ctx.Request() + var scSrc scSource var buf *buffer.Buffer if enableBodyLogging { @@ -89,6 +91,16 @@ func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool log.Int("application_id", authData.ApplicationId), log.Int("admin_id", ctx.AdminId()), } + + if enableBodyLogging { + for _, sskip := range skip { + if strings.HasPrefix(ctx.Endpoint(), sskip) { + enableBodyLogging = false + break + } + } + } + if enableBodyLogging { fields = append(fields, log.ByteString("request", buf.RequestBody()), diff --git a/tests/acceptance_test.go b/tests/acceptance_test.go index 7157e07..01c05f1 100644 --- a/tests/acceptance_test.go +++ b/tests/acceptance_test.go @@ -138,7 +138,7 @@ func (s *HappyPathTestSuite) TestHttpProxy() { require.EqualValues(req.Id, resp.Id) } -func (s *HappyPathTestSuite) TestWsProxy() { +func (s *HappyPathTestSuite) TestWsProxy() { // nolint: funlen test, require := test.New(s.T()) config, redisCli, systemCli, adminCli := s.commonDependencies(test) @@ -179,6 +179,7 @@ func (s *HappyPathTestSuite) TestWsProxy() { "x-request-id": {requestId}, }, }) + requestUrl, err := url.Parse(srv.URL) require.NoError(err) requestUrl.Path = "ws/service" @@ -214,9 +215,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, + SkipBodyLoggingEndpointPrefixes: []string{"endpoint"}}, Caching: conf.Caching{AuthorizationDataInSec: 1, AuthenticationDataInSec: 1}, DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100}}, Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 100}},