add RetryAttempts to AccessLog in JSON format
This commit is contained in:
parent
23cdb37165
commit
dae7e7a80a
11 changed files with 130 additions and 90 deletions
|
@ -908,9 +908,10 @@ func (server *Server) loadConfig(configurations types.Configurations, globalConf
|
|||
}
|
||||
|
||||
if globalConfiguration.Retry != nil {
|
||||
retryListener := middlewares.NewMetricsRetryListener(server.metricsRegistry, frontend.Backend)
|
||||
lb = registerRetryMiddleware(lb, globalConfiguration, config, frontend.Backend, retryListener)
|
||||
countServers := len(config.Backends[frontend.Backend].Servers)
|
||||
lb = server.buildRetryMiddleware(lb, globalConfiguration, countServers, frontend.Backend)
|
||||
}
|
||||
|
||||
if server.metricsRegistry.IsEnabled() {
|
||||
n.Use(middlewares.NewMetricsWrapper(server.metricsRegistry, frontend.Backend))
|
||||
}
|
||||
|
@ -1187,20 +1188,21 @@ func stopMetricsClients() {
|
|||
metrics.StopStatsd()
|
||||
}
|
||||
|
||||
func registerRetryMiddleware(
|
||||
httpHandler http.Handler,
|
||||
globalConfig configuration.GlobalConfiguration,
|
||||
config *types.Configuration,
|
||||
backend string,
|
||||
listener middlewares.RetryListener,
|
||||
) http.Handler {
|
||||
retries := len(config.Backends[backend].Servers)
|
||||
if globalConfig.Retry.Attempts > 0 {
|
||||
retries = globalConfig.Retry.Attempts
|
||||
func (server *Server) buildRetryMiddleware(handler http.Handler, globalConfig configuration.GlobalConfiguration, countServers int, backendName string) http.Handler {
|
||||
retryListeners := middlewares.RetryListeners{}
|
||||
if server.metricsRegistry.IsEnabled() {
|
||||
retryListeners = append(retryListeners, middlewares.NewMetricsRetryListener(server.metricsRegistry, backendName))
|
||||
}
|
||||
if server.accessLoggerMiddleware != nil {
|
||||
retryListeners = append(retryListeners, &accesslog.SaveRetries{})
|
||||
}
|
||||
|
||||
httpHandler = middlewares.NewRetry(retries, httpHandler, listener)
|
||||
log.Debugf("Creating retries max attempts %d", retries)
|
||||
retryAttempts := countServers
|
||||
if globalConfig.Retry.Attempts > 0 {
|
||||
retryAttempts = globalConfig.Retry.Attempts
|
||||
}
|
||||
|
||||
return httpHandler
|
||||
log.Debugf("Creating retries max attempts %d", retryAttempts)
|
||||
|
||||
return middlewares.NewRetry(retryAttempts, handler, retryListeners)
|
||||
}
|
||||
|
|
|
@ -495,74 +495,6 @@ func TestConfigureBackends(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRegisterRetryMiddleware(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
globalConfig configuration.GlobalConfiguration
|
||||
countServers int
|
||||
expectedRetries int
|
||||
}{
|
||||
{
|
||||
name: "configured retry attempts",
|
||||
globalConfig: configuration.GlobalConfiguration{
|
||||
Retry: &configuration.Retry{
|
||||
Attempts: 3,
|
||||
},
|
||||
},
|
||||
expectedRetries: 3,
|
||||
},
|
||||
{
|
||||
name: "retry attempts defaults to server amount",
|
||||
globalConfig: configuration.GlobalConfiguration{
|
||||
Retry: &configuration.Retry{},
|
||||
},
|
||||
expectedRetries: 2,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var retryListener middlewares.RetryListener
|
||||
httpHandler := okHTTPHandler{}
|
||||
dynamicConfig := &types.Configuration{
|
||||
Backends: map[string]*types.Backend{
|
||||
"backend": {
|
||||
Servers: map[string]types.Server{
|
||||
"server": {
|
||||
URL: "http://localhost",
|
||||
},
|
||||
"server2": {
|
||||
URL: "http://localhost",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
httpHandlerWithRetry := registerRetryMiddleware(httpHandler, tc.globalConfig, dynamicConfig, "backend", retryListener)
|
||||
|
||||
retry, ok := httpHandlerWithRetry.(*middlewares.Retry)
|
||||
if !ok {
|
||||
t.Fatalf("httpHandler was not decorated with retry httpHandler, got %#v", httpHandlerWithRetry)
|
||||
}
|
||||
|
||||
expectedRetry := middlewares.NewRetry(tc.expectedRetries, httpHandler, retryListener)
|
||||
if !reflect.DeepEqual(retry, expectedRetry) {
|
||||
t.Errorf("retry httpHandler was not instantiated correctly, got %#v want %#v", retry, expectedRetry)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type okHTTPHandler struct{}
|
||||
|
||||
func (okHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func TestServerEntrypointWhitelistConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue