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

adding automaxproc to auto optimize GOPROC management #225

Merged
merged 9 commits into from
Sep 10, 2024
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
require (
github.com/go-resty/resty/v2 v2.13.1 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"errors"
"flag"
"fmt"
"go.uber.org/automaxprocs/maxprocs"
"os"

"github.com/ianschenck/envflag"
prajwalvathreya marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -75,11 +76,21 @@
ctx := context.Background()
log := logger.NewLogger(ctx)
ctx = context.WithValue(ctx, logger.LoggerKey{}, log)
undoMaxprocs, maxprocsError := maxprocs.Set(maxprocs.Logger(func(msg string, keysAndValues ...interface{}) {
log.Klogr.WithValues("component", "maxprocs", "version", maxprocs.Version).V(2).Info(fmt.Sprintf(msg, keysAndValues...))
}))
defer undoMaxprocs()

Check warning on line 82 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L79-L82

Added lines #L79 - L82 were not covered by tests

if maxprocsError != nil {
log.Error(maxprocsError, "Failed to set GOMAXPROCS")
os.Exit(1)

Check warning on line 86 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L84-L86

Added lines #L84 - L86 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should necessarily exit if we can't set GOMAXPROCS since that could result in a CrashLoopBackoff where there was none prior without this, but we should keep the error log.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. This would introduce a new problem. I'll fix that and push the changes.

}

if err := handle(ctx); err != nil {
log.Error(err, "Fatal error")
os.Exit(1)
}

os.Exit(0)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
type LoggerKey struct{}

type Logger struct {
klogr logr.Logger
Klogr logr.Logger
}

// NewLogger creates a new Logger instance with a klogr logger.
func NewLogger(ctx context.Context) *Logger {
return &Logger{
klogr: klog.NewKlogr(),
Klogr: klog.NewKlogr(),
}
}

Expand All @@ -26,7 +26,7 @@ func NewLogger(ctx context.Context) *Logger {
func (l *Logger) WithMethod(method string) (*Logger, context.Context, func()) {
traceID := uuid.New().String()
newLogger := &Logger{
klogr: klog.NewKlogr().WithValues("method", method, "traceID", traceID),
Klogr: klog.NewKlogr().WithValues("method", method, "traceID", traceID),
}
ctx := context.WithValue(context.Background(), LoggerKey{}, newLogger)

Expand All @@ -39,12 +39,12 @@ func (l *Logger) WithMethod(method string) (*Logger, context.Context, func()) {

// V returns a logr.Logger with the specified verbosity level.
func (l *Logger) V(level int) logr.Logger {
return l.klogr.V(level)
return l.Klogr.V(level)
}

// Error logs an error message with the specified keys and values.
func (l *Logger) Error(err error, msg string, keysAndValues ...interface{}) {
l.klogr.Error(err, msg, keysAndValues...)
l.Klogr.Error(err, msg, keysAndValues...)
}

// GetLogger retrieves the Logger from the context, or creates a new one if not present.
Expand Down