From d12dba9773f7412fe0947ffd0e98633267871601 Mon Sep 17 00:00:00 2001 From: Omer Aplatony Date: Thu, 21 Nov 2024 10:41:57 +0200 Subject: [PATCH] fix: ensure consistent JSON log format for automaxprocs (#6335) * fix: ensure consistent JSON log format for automaxprocs Signed-off-by: Omer Aplatony * moved to Unreleased Signed-off-by: Omer Aplatony --------- Signed-off-by: Omer Aplatony --- CHANGELOG.md | 1 + cmd/adapter/main.go | 7 ++++++- cmd/operator/main.go | 8 +++++++- cmd/webhooks/main.go | 7 ++++++- pkg/util/maxprocs.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 pkg/util/maxprocs.go diff --git a/CHANGELOG.md b/CHANGELOG.md index dbba7b57a05..df2a1987770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ Here is an overview of all new **experimental** features: ### Fixes +- **General**: Centralize and improve automaxprocs configuration with proper structured logging ([#5970](https://github.com/kedacore/keda/issues/5970)) - **General**: Paused ScaledObject count is reported correctly after operator restart ([#6321](https://github.com/kedacore/keda/issues/6321)) ### Deprecations diff --git a/cmd/adapter/main.go b/cmd/adapter/main.go index b4ecf6781bb..8444e51dfac 100644 --- a/cmd/adapter/main.go +++ b/cmd/adapter/main.go @@ -26,7 +26,6 @@ import ( grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" - _ "go.uber.org/automaxprocs" appsv1 "k8s.io/api/apps/v1" apimetrics "k8s.io/apiserver/pkg/endpoints/metrics" "k8s.io/client-go/kubernetes/scheme" @@ -257,6 +256,12 @@ func main() { return } + err = kedautil.ConfigureMaxProcs(logger) + if err != nil { + logger.Error(err, "failed to set max procs") + return + } + kedaProvider, err := cmd.makeProvider(ctx) if err != nil { logger.Error(err, "making provider") diff --git a/cmd/operator/main.go b/cmd/operator/main.go index 16f6899d230..dd1dc656f28 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -22,7 +22,6 @@ import ( "time" "github.com/spf13/pflag" - _ "go.uber.org/automaxprocs" apimachineryruntime "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" kubeinformers "k8s.io/client-go/informers" @@ -115,6 +114,13 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) ctx := ctrl.SetupSignalHandler() + + err := kedautil.ConfigureMaxProcs(setupLog) + if err != nil { + setupLog.Error(err, "failed to set max procs") + os.Exit(1) + } + namespaces, err := kedautil.GetWatchNamespaces() if err != nil { setupLog.Error(err, "failed to get watch namespace") diff --git a/cmd/webhooks/main.go b/cmd/webhooks/main.go index 56c03eb1b00..72d452bbc1f 100644 --- a/cmd/webhooks/main.go +++ b/cmd/webhooks/main.go @@ -22,7 +22,6 @@ import ( "os" "github.com/spf13/pflag" - _ "go.uber.org/automaxprocs" apimachineryruntime "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" @@ -80,6 +79,12 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + err := kedautil.ConfigureMaxProcs(setupLog) + if err != nil { + setupLog.Error(err, "failed to set max procs") + os.Exit(1) + } + ctx := ctrl.SetupSignalHandler() cfg := ctrl.GetConfigOrDie() diff --git a/pkg/util/maxprocs.go b/pkg/util/maxprocs.go new file mode 100644 index 00000000000..2cedc59185a --- /dev/null +++ b/pkg/util/maxprocs.go @@ -0,0 +1,34 @@ +/* +Copyright 2024 The KEDA Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "fmt" + + "go.uber.org/automaxprocs/maxprocs" + "k8s.io/klog/v2" +) + +// ConfigureMaxProcs sets up automaxprocs with proper logging configuration. +// It wraps the automaxprocs logger to handle structured logging with string keys +// to prevent panics when automaxprocs tries to pass numeric keys. +func ConfigureMaxProcs(logger klog.Logger) error { + _, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) { + logger.Info(fmt.Sprintf(format, args...)) + })) + return err +}