From 5230e9ac42562bd78ac27db23a273653f9312c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Mon, 22 Sep 2025 10:35:33 +0300 Subject: [PATCH] MSP-1684 Add forward client id setting for app --- assembly/locator.go | 9 +++++++-- conf/remote.go | 18 ++++++++++++------ middleware/request_id.go | 6 ++++-- 3 files changed, 23 insertions(+), 10 deletions(-) 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 } }