Skip to content

Commit 1923725

Browse files
omerap12ctccxxd
authored andcommitted
fix: ensure consistent JSON log format for automaxprocs (kedacore#6335)
* fix: ensure consistent JSON log format for automaxprocs Signed-off-by: Omer Aplatony <[email protected]> * moved to Unreleased Signed-off-by: Omer Aplatony <[email protected]> --------- Signed-off-by: Omer Aplatony <[email protected]>
1 parent c017a92 commit 1923725

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Here is an overview of all new **experimental** features:
7373

7474
### Fixes
7575

76+
- **General**: Centralize and improve automaxprocs configuration with proper structured logging ([#5970](https://github.com/kedacore/keda/issues/5970))
7677
- **General**: Paused ScaledObject count is reported correctly after operator restart ([#6321](https://github.com/kedacore/keda/issues/6321))
7778

7879
### Deprecations

cmd/adapter/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
2727
"github.com/prometheus/client_golang/prometheus"
2828
"github.com/prometheus/client_golang/prometheus/collectors"
29-
_ "go.uber.org/automaxprocs"
3029
appsv1 "k8s.io/api/apps/v1"
3130
apimetrics "k8s.io/apiserver/pkg/endpoints/metrics"
3231
"k8s.io/client-go/kubernetes/scheme"
@@ -257,6 +256,12 @@ func main() {
257256
return
258257
}
259258

259+
err = kedautil.ConfigureMaxProcs(logger)
260+
if err != nil {
261+
logger.Error(err, "failed to set max procs")
262+
return
263+
}
264+
260265
kedaProvider, err := cmd.makeProvider(ctx)
261266
if err != nil {
262267
logger.Error(err, "making provider")

cmd/operator/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"time"
2323

2424
"github.com/spf13/pflag"
25-
_ "go.uber.org/automaxprocs"
2625
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
2726
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2827
kubeinformers "k8s.io/client-go/informers"
@@ -115,6 +114,13 @@ func main() {
115114

116115
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
117116
ctx := ctrl.SetupSignalHandler()
117+
118+
err := kedautil.ConfigureMaxProcs(setupLog)
119+
if err != nil {
120+
setupLog.Error(err, "failed to set max procs")
121+
os.Exit(1)
122+
}
123+
118124
namespaces, err := kedautil.GetWatchNamespaces()
119125
if err != nil {
120126
setupLog.Error(err, "failed to get watch namespace")

cmd/webhooks/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323

2424
"github.com/spf13/pflag"
25-
_ "go.uber.org/automaxprocs"
2625
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
2726
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2827
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -80,6 +79,12 @@ func main() {
8079

8180
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8281

82+
err := kedautil.ConfigureMaxProcs(setupLog)
83+
if err != nil {
84+
setupLog.Error(err, "failed to set max procs")
85+
os.Exit(1)
86+
}
87+
8388
ctx := ctrl.SetupSignalHandler()
8489

8590
cfg := ctrl.GetConfigOrDie()

pkg/util/maxprocs.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2024 The KEDA Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"fmt"
21+
22+
"go.uber.org/automaxprocs/maxprocs"
23+
"k8s.io/klog/v2"
24+
)
25+
26+
// ConfigureMaxProcs sets up automaxprocs with proper logging configuration.
27+
// It wraps the automaxprocs logger to handle structured logging with string keys
28+
// to prevent panics when automaxprocs tries to pass numeric keys.
29+
func ConfigureMaxProcs(logger klog.Logger) error {
30+
_, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
31+
logger.Info(fmt.Sprintf(format, args...))
32+
}))
33+
return err
34+
}

0 commit comments

Comments
 (0)