Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure consistent JSON log format for automaxprocs #6335

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion cmd/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -257,6 +256,12 @@ func main() {
return
}

err = kedautil.ConfigureMaxProcs(logger)
if err != nil {
logger.Error(err, "failed to set max procs")
return
wozniakjan marked this conversation as resolved.
Show resolved Hide resolved
}

kedaProvider, err := cmd.makeProvider(ctx)
if err != nil {
logger.Error(err, "making provider")
Expand Down
8 changes: 7 additions & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion cmd/webhooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/maxprocs.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading