mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-19 02:11:17 +02:00
- Replace klog.SetLogger with proper error handler filtering - Use package-level init() to avoid global side effects in NewTracker - Filter context canceled and timeout errors at ErrorHandler level - Preserve original error handlers while adding custom filter - Add nolint directives for necessary runtime.ErrorHandlers reassignment This implementation properly filters expected errors during normal shutdown while preserving all genuine error logging. Addresses review comments: 1. Actually filter errors instead of just setting klog logger 2. Avoid global side effects in NewTracker constructor Signed-off-by: yxxhero <aiopsclub@163.com>
48 lines
885 B
Go
48 lines
885 B
Go
package kubedog
|
|
|
|
import (
|
|
goContext "context"
|
|
"strings"
|
|
"sync"
|
|
|
|
"k8s.io/apimachinery/pkg/util/runtime"
|
|
)
|
|
|
|
var (
|
|
errorHandlersInitOnce sync.Once
|
|
)
|
|
|
|
func init() {
|
|
initErrorHandlers()
|
|
}
|
|
|
|
func initErrorHandlers() {
|
|
errorHandlersInitOnce.Do(func() {
|
|
originalHandlers := runtime.ErrorHandlers
|
|
// nolint: reassign
|
|
runtime.ErrorHandlers = []runtime.ErrorHandler{filterErrorHandler}
|
|
// nolint: reassign
|
|
runtime.ErrorHandlers = append(runtime.ErrorHandlers, originalHandlers...)
|
|
})
|
|
}
|
|
|
|
func filterErrorHandler(ctx goContext.Context, err error, msg string, keysAndValues ...interface{}) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
errMsg := err.Error()
|
|
|
|
if strings.Contains(errMsg, "context canceled") {
|
|
return
|
|
}
|
|
|
|
if strings.Contains(errMsg, "Client.Timeout exceeded") {
|
|
return
|
|
}
|
|
|
|
for _, handler := range runtime.ErrorHandlers[1:] {
|
|
handler(ctx, err, msg, keysAndValues...)
|
|
}
|
|
}
|