Skip to content

Commit

Permalink
fix: ensure consistent JSON log format for automaxprocs
Browse files Browse the repository at this point in the history
Signed-off-by: Omer Aplatony <[email protected]>
  • Loading branch information
omerap12 committed Nov 15, 2024
1 parent 8332446 commit 0f95de7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 11 additions & 1 deletion cmd/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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"
"go.uber.org/automaxprocs/maxprocs"
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 +257,16 @@ func main() {
return
}

// Configure automaxprocs logger with a wrapper function because klogr expects structured logging
// with string keys, but automaxprocs tries to pass numeric keys which causes a panic.
_, err = maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
logger.Info(fmt.Sprintf(format, args...))
}))
if err != nil {
logger.Error(err, "unable to set max procs")
return
}

kedaProvider, err := cmd.makeProvider(ctx)
if err != nil {
logger.Error(err, "making provider")
Expand Down
15 changes: 14 additions & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package main

import (
"flag"
"fmt"
"os"
"time"

"github.com/spf13/pflag"
_ "go.uber.org/automaxprocs"
"go.uber.org/automaxprocs/maxprocs"
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 +116,18 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
ctx := ctrl.SetupSignalHandler()

// Configure automaxprocs logger to work with controller-runtime's structured logging.
// We need to wrap the logger to ensure proper key-value pair formatting since
// automaxprocs doesn't provide structured logging pairs by default.
_, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
setupLog.Info(fmt.Sprintf(format, args...), "system", "automaxprocs")
}))
if err != nil {
setupLog.Error(err, "failed to set maxprocs")
os.Exit(1)
}

namespaces, err := kedautil.GetWatchNamespaces()
if err != nil {
setupLog.Error(err, "failed to get watch namespace")
Expand Down
15 changes: 14 additions & 1 deletion cmd/webhooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package main
import (
"crypto/tls"
"flag"
"fmt"
"os"

"go.uber.org/automaxprocs/maxprocs"

"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 +82,17 @@ func main() {

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

// Configure automaxprocs logger to work with controller-runtime's structured logging.
// We need to wrap the logger to ensure proper key-value pair formatting since
// automaxprocs doesn't provide structured logging pairs by default.
_, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
setupLog.Info(fmt.Sprintf(format, args...), "system", "automaxprocs")
}))
if err != nil {
setupLog.Error(err, "unable to set max procs")
os.Exit(1)
}

ctx := ctrl.SetupSignalHandler()

cfg := ctrl.GetConfigOrDie()
Expand Down

0 comments on commit 0f95de7

Please sign in to comment.