MSP-4211 Add remote setting for client request id forwarding

This commit is contained in:
Синельник Тимур Романович committed 2024-10-11 14:32:17 +03:00
1 parent f9b2632335
commit 875d3985bd
6 files changed
+39 -28

No files matched your search

+5 -4
View File
@@ -4,8 +4,6 @@ import (
"net/http"
"time"
mux2 "github.com/gorilla/mux"
"github.com/redis/go-redis/v9"
"isp-gate-service/conf"
"isp-gate-service/middleware"
"isp-gate-service/proxy"
@@ -13,6 +11,9 @@ import (
"isp-gate-service/routes"
"isp-gate-service/service"
mux2 "github.com/gorilla/mux"
"github.com/redis/go-redis/v9"
"github.com/integration-system/isp-kit/grpc/client"
"github.com/integration-system/isp-kit/lb"
"github.com/integration-system/isp-kit/log"
@@ -91,7 +92,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
handler := middleware.Chain(
proxyFunc,
middleware.RequestId(),
middleware.RequestId(config.EnableClientRequestIdForwarding),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog,
config.Logging.SkipBodyLoggingEndpointPrefixes),
middleware.ErrorHandler(l.logger),
@@ -105,7 +106,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
if location.SkipAuth {
handler = middleware.Chain(
proxyFunc,
middleware.RequestId(),
middleware.RequestId(config.EnableClientRequestIdForwarding),
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog,
config.Logging.SkipBodyLoggingEndpointPrefixes),
middleware.ErrorHandler(l.logger),
+7 -6
View File
@@ -18,12 +18,13 @@ func init() {
}
type Remote struct {
Redis *Redis `schema:"Настройки Redis,обязательно, если используется механизм суточных ограничений или ограничений пропускной способности"`
Http Http `schema:"Настройки HTTP"`
Logging Logging `schema:"Настройки логирования"`
Caching Caching `schema:"Настройки кеширования"`
DailyLimits []DailyLimit `schema:"Настройки суточных ограничений,сбрасываются раз в сутки в 00:00"`
Throttling []Throttling `schema:"Настройки пропускной способности"`
Redis *Redis `schema:"Настройки Redis,обязательно, если используется механизм суточных ограничений или ограничений пропускной способности"`
Http Http `schema:"Настройки HTTP"`
Logging Logging `schema:"Настройки логирования"`
Caching Caching `schema:"Настройки кеширования"`
DailyLimits []DailyLimit `schema:"Настройки суточных ограничений,сбрасываются раз в сутки в 00:00"`
Throttling []Throttling `schema:"Настройки пропускной способности"`
EnableClientRequestIdForwarding bool `schema:"Включить проброс requestId из заголовка запроса"`
}
type Http struct {
+14 -7
View File
@@ -3,28 +3,35 @@ package middleware
import (
"strings"
"isp-gate-service/request"
"github.com/integration-system/isp-kit/log"
"github.com/integration-system/isp-kit/requestid"
"isp-gate-service/request"
)
const (
requestIdHeader = "x-request-id"
)
func RequestId() Middleware {
func RequestId(forwardClientRequestId bool) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx *request.Context) error {
requestId := strings.TrimSpace(ctx.Request().Header.Get(requestIdHeader))
if requestId == "" {
requestId = requestid.Next()
requestId := requestid.Next()
clientRequestId := strings.TrimSpace(ctx.Request().Header.Get(requestIdHeader))
logFields := make([]log.Field, 0)
if clientRequestId != "" {
logFields = append(logFields, log.String("clientRequestId", clientRequestId))
if forwardClientRequestId {
requestId = clientRequestId
}
}
logFields = append(logFields, log.String("requestId", requestId))
context := requestid.ToContext(ctx.Context(), requestId)
context = log.ToContext(context, log.String("requestId", requestId))
context = log.ToContext(context, logFields...)
ctx.SetContext(context)
return next.Handle(ctx)
})
}
+2 -2
View File
@@ -102,9 +102,9 @@ func setHttpHeaders(ctx *request.Context, header http.Header, skipAuth bool) err
header.Set(grpc.ServiceIdHeader, strconv.Itoa(authData.ServiceId))
header.Set(grpc.ApplicationIdHeader, strconv.Itoa(authData.ApplicationId))
if ctx.IsAdminAuthenticated() {
header.Set(xAdminIdHeader, strconv.Itoa(ctx.AdminId()))
header.Set(xAdminIdHeader, strconv.Itoa(ctx.AdminId())) //nolint:canonicalheader
} else {
header.Del(xAdminIdHeader)
header.Del(xAdminIdHeader) //nolint:canonicalheader
}
}
return nil
+2 -2
View File
@@ -29,7 +29,7 @@ func NewDailyLimit(repo DailyLimitRepo, configs []conf.DailyLimit) DailyLimit {
}
func (s DailyLimit) IncrementAndCheck(ctx context.Context, applicationId int) (bool, error) {
max, ok := s.limits[applicationId]
limit, ok := s.limits[applicationId]
if !ok {
return true, nil
}
@@ -39,5 +39,5 @@ func (s DailyLimit) IncrementAndCheck(ctx context.Context, applicationId int) (b
return false, errors.WithMessage(err, "increment")
}
return newValue <= max, nil
return newValue <= limit, nil
}
+9 -7
View File
@@ -9,16 +9,17 @@ import (
"strconv"
"testing"
"isp-gate-service/assembly"
"isp-gate-service/conf"
"isp-gate-service/domain"
"isp-gate-service/routes"
etp "github.com/integration-system/isp-etp-go/v2"
etpcli "github.com/integration-system/isp-etp-go/v2/client"
"github.com/integration-system/isp-kit/cluster"
"github.com/integration-system/isp-kit/http/httpcli"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"isp-gate-service/assembly"
"isp-gate-service/conf"
"isp-gate-service/domain"
"isp-gate-service/routes"
"github.com/google/uuid"
"github.com/integration-system/isp-kit/grpc"
@@ -287,9 +288,10 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
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}},
Caching: conf.Caching{AuthorizationDataInSec: 1, AuthenticationDataInSec: 1},
DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100}},
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 100}},
EnableClientRequestIdForwarding: true,
}
systemService, systemCli := grpct.NewMock(test)