Skip to content

Commit

Permalink
adding automaxproc to auto optimize GOPROC management (#225)
Browse files Browse the repository at this point in the history
* - imported automaxprocs in main.go

* - adding logging for automaxprocs

* - updated maxprocs logger to use pre-existing logger functions
- made `Klogr` a public variable in `pkg/logger/logger.go` in-order to use it in `main.go`

* - updated the else if

* Grouping imports in main.go

Co-authored-by: Ashley Dumaine <5779804+AshleyDumaine@users.noreply.github.com>

* Re-ordering imports in alphabetical order

Co-authored-by: Ashley Dumaine <5779804+AshleyDumaine@users.noreply.github.com>

* - removed `os.exit` on failure to set `GOMAXPROCS`

---------

Co-authored-by: Ashley Dumaine <5779804+AshleyDumaine@users.noreply.github.com>
  • Loading branch information
prajwalvathreya and AshleyDumaine authored Sep 10, 2024
1 parent 46cc901 commit c366f37
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
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
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ianschenck/envflag"
"github.com/linode/linodego"
"go.uber.org/automaxprocs/maxprocs"
"k8s.io/klog/v2"

"github.com/linode/linode-blockstorage-csi-driver/internal/driver"
Expand Down Expand Up @@ -75,11 +76,20 @@ func main() {
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()

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

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

0 comments on commit c366f37

Please sign in to comment.