-
Notifications
You must be signed in to change notification settings - Fork 268
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 logging behavior around setting goproc limits #2018
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
18fd8e7
Add limits RunE function
tstirrat15 9ac2ba5
Move limit setup out of main
tstirrat15 23cf62e
Get rid of debug
tstirrat15 2a5da97
Add limit setting to default prerun
tstirrat15 6284b02
Move to break cycle:
tstirrat15 14a0ee6
Move import to break cycle
tstirrat15 643da22
Use cobrautil for proclimits
tstirrat15 6fbd3fb
Push up usage
tstirrat15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package limits | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/jzelinskie/cobrautil/v2" | ||
"github.com/rs/zerolog" | ||
slogzerolog "github.com/samber/slog-zerolog/v2" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/automaxprocs/maxprocs" | ||
|
||
log "github.com/authzed/spicedb/internal/logging" | ||
) | ||
|
||
// SetLimitsRunE wraps the RunFunc with setup logic for memory limits and maxprocs | ||
// limits for the go process. Implementing it here means that we can use already- | ||
// established loggers and wrap only those commands that require it. | ||
func SetLimitsRunE() cobrautil.CobraRunFunc { | ||
return func(cmd *cobra.Command, args []string) error { | ||
// Need to invert the slog => zerolog map so that we can get the correct | ||
// slog loglevel for memlimit logging | ||
logLevelMap := make(map[zerolog.Level]slog.Level, len(slogzerolog.LogLevels)) | ||
for sLevel, zLevel := range slogzerolog.LogLevels { | ||
logLevelMap[zLevel] = sLevel | ||
} | ||
|
||
logLevel := logLevelMap[zerolog.DefaultContextLogger.GetLevel()] | ||
|
||
slogger := slog.New(slogzerolog.Option{Level: logLevel, Logger: zerolog.DefaultContextLogger}.NewZerologHandler()) | ||
|
||
undo, err := maxprocs.Set(maxprocs.Logger(zerolog.DefaultContextLogger.Printf)) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("failed to set maxprocs") | ||
} | ||
defer undo() | ||
|
||
_, _ = memlimit.SetGoMemLimitWithOpts( | ||
memlimit.WithRatio(0.9), | ||
memlimit.WithProvider( | ||
memlimit.ApplyFallback( | ||
memlimit.FromCgroup, | ||
memlimit.FromSystem, | ||
), | ||
), | ||
memlimit.WithLogger(slogger), | ||
) | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems useful enough to put into
cobrautil
so it can be used across other projects.