MSP-1684 Add forward client id setting for app

This commit is contained in:
Синельник Тимур Романович committed 2025-09-22 10:52:22 +03:00
1 parent f5bfe7d2ba
commit 5230e9ac42
3 files changed
+23 -10

No files matched your search

+7 -2
View File
@@ -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(
+12 -6
View File
@@ -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 {
+4 -2
View File
@@ -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
}
}