diff --git a/assembly/locator.go b/assembly/locator.go index 3fdb664..3479cb2 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -89,9 +89,13 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha return nil, errors.Errorf("not supported protocol %s", location.Protocol) } + forwardReqIdByAppId := make(map[int]bool, len(config.ForwardReqIdClientSettings)) + for _, setting := range config.ForwardReqIdClientSettings { + forwardReqIdByAppId[setting.ApplicationId] = setting.ForwardRequestId + } + handler := middleware.Chain( proxyFunc, - middleware.RequestId(config.EnableClientRequestIdForwarding), middleware.Logger( l.logger, config.Logging.RequestLogEnable, enableBodyLog, @@ -105,17 +109,18 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha middleware.AdminAuthorize(l.routes, adminService), middleware.Throttling(throttlingService), middleware.DailyLimit(dailyLimitService), + middleware.RequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId), ) if location.SkipAuth { handler = middleware.Chain( proxyFunc, - middleware.RequestId(config.EnableClientRequestIdForwarding), middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, config.Logging.SkipBodyLoggingEndpointPrefixes, config.Logging.EnableForceUnescapingUnicode, ), middleware.ErrorHandler(l.logger), + middleware.RequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId), ) } entrypoint := middleware.Entrypoint( diff --git a/conf/remote.go b/conf/remote.go index 382686c..960c3f4 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -17,12 +17,18 @@ func init() { } type Remote struct { - Http Http `schema:"Настройки HTTP"` - Logging Logging `schema:"Настройки логирования"` - Caching Caching `schema:"Настройки кеширования"` - DailyLimits []DailyLimit `schema:"Настройки суточных ограничений,сбрасываются раз в сутки в 00:00"` - Throttling []Throttling `schema:"Настройки пропускной способности"` - EnableClientRequestIdForwarding bool `schema:"Включить проброс requestId из заголовка запроса"` + Http Http `schema:"Настройки HTTP"` + Logging Logging `schema:"Настройки логирования"` + Caching Caching `schema:"Настройки кеширования"` + DailyLimits []DailyLimit `schema:"Настройки суточных ограничений,сбрасываются раз в сутки в 00:00"` + Throttling []Throttling `schema:"Настройки пропускной способности"` + EnableClientRequestIdForwarding bool `schema:"Включить проброс requestId из заголовка запроса"` + ForwardReqIdClientSettings []ForwardReqIdClientSettings `schema:"Настройки проброcа requestId для приложений"` +} + +type ForwardReqIdClientSettings struct { + ApplicationId int `schema:"Идентификатор приложения"` + ForwardRequestId bool `schema:"Включить проброс requestId из заголовка запроса"` } type Http struct { diff --git a/middleware/request_id.go b/middleware/request_id.go index 0861fba..7ba4ebd 100644 --- a/middleware/request_id.go +++ b/middleware/request_id.go @@ -13,7 +13,7 @@ const ( requestIdHeader = "x-request-id" ) -func RequestId(forwardClientRequestId bool) Middleware { +func RequestId(forwardClientRequestId bool, forwardReqIdByApp map[int]bool) Middleware { return func(next Handler) Handler { return HandlerFunc(func(ctx *request.Context) error { requestId := requestid.Next() @@ -22,7 +22,9 @@ func RequestId(forwardClientRequestId bool) Middleware { logFields := make([]log.Field, 0) if clientRequestId != "" { logFields = append(logFields, log.String("clientRequestId", clientRequestId)) - if forwardClientRequestId { + + authData, _ := ctx.GetAuthData() + if forwardClientRequestId || forwardReqIdByApp[authData.ApplicationId] { requestId = clientRequestId } }