-
Notifications
You must be signed in to change notification settings - Fork 53
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
Refactor loggers with Slog #1642
Conversation
6991582
to
6ad9c63
Compare
Note: branches |
8709948
to
2cb04ba
Compare
4abc9af
to
7b0fd9a
Compare
fa1f47c
to
6b5d854
Compare
Note: I still need to update go.mod with the updated commit hash once dbason/opentelemetry-collector-contrib#1 is merged |
Some of the log messages that look like this: a.logger.With(
zap.Error(err),
zap.String("group", ruleGroup.Name),
).Error("failed to marshal rule group") seem to have been rewritten as a.logger.Error("failed to marshal rule group", logger.Err(err),
"group", ruleGroup.Name) Is there a way we can have the format match? i.e. a.logger.With(
logger.Err(err),
"group", ruleGroup.Name,
).Error("failed to marshal rule group") |
Yes, your last example is valid slog syntax. The reason why I changed the format is because the original format causes an extra allocation per line, and you mentioned you wanted to reduce allocations by moving to slog. I can revert the format of the |
Ah, yeah I think for most cases the extra memory allocation isn't a big deal, but in performance oriented code that is good to keep in mind. we should keep the old format in places where it's originally written like that. |
e1564b8
to
ece00ef
Compare
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.
LGTM
Rebased onto main, no other changes |
Yeah that sounds fine to me, we can merge this PR first. |
run go mod tidy add handler test with fixes omit prefix on keys for readability
Related #1170
Depends on dbason/opentelemetry-collector-contrib#1
Depends on kralicky/totem#5
Replaces zap with slog. According to docs, this should eliminate any allocations for logging strings and numbers. Benchmark test results for slog handlers: https://gist.github.com/jan-law/bfb4db82ba05dbcb551e0de1ed36f8c1
How to rebase branches ahead of main
In general, logging calls are identical with a few exceptions:
Infof
,Errorf
-> replace withInfo(fmt.Sprintf(...))
,Error(fmt.Sprintf(...))
logger.New().Named("some-name")
-> replace withlogger.New().WithGroup("some-name")
logr.Logr
, you can uselogger.NewLogr(options...)
pkg/logger/logger.go
for available optionsThis script will help you refactor existing zap logs with the slog equivalents.
Disclaimer: gofmt can only refactor logging calls that have a fixed number of arguments and is also case sensitive. If you created a log message with a different number of key/val pairs than what already exists in the codebase, the script may not format that log message.
Notes
logger.With(args...).Info("blah")
are still valid, but may cost extra memory allocations. You can replace them withlogger.Info("blah", args...)
!BADKEY=<value>
. Docs recommend using https://pkg.go.dev/cmd/vet to check your code