-
Notifications
You must be signed in to change notification settings - Fork 535
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: go-kit/log-slog adapter fixes #9943
Open
tjhop
wants to merge
3
commits into
grafana:arve/upgrade-mimir-prometheus
Choose a base branch
from
tjhop:fix/slog-adapter-tweaks
base: arve/upgrade-mimir-prometheus
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: go-kit/log-slog adapter fixes #9943
tjhop
wants to merge
3
commits into
grafana:arve/upgrade-mimir-prometheus
from
tjhop:fix/slog-adapter-tweaks
+70
−27
Conversation
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
Changes included: - log/slog always includes timestamps by default; they are now included in the go-kit/log output. We make no attempts to adjust/remove any other timestamp keys (such as the often used `ts` kv pair in go-kit/log implementations). - a new function to handle properly appending/nesting our kv pairs for the logger - proper handling of nested grouping - introduction of a `preformatted` field on the goKitHandler struct to cache previously added kv pairs containing log values. helpful to maintain the ordering of kv pairs added via multiple/nested/chained calls to a logger's `.With()` or `.WithGroup()` methods. - WithGroup() now returns a new handler struct, as opposed to updating itself (as recommended by slog implementation docs). Not addressed: - tests still need to be updated. they currently use a mock logger, and the log lines have timestamps included. Since timestamps aren't deterministic, they're not easy to mock. I refrained from creating a wrapper interface to mock the time just for sake of testing; I used pattern matching with regexs when implementing slog tests for prometheus/common's `promslog` pkg. - I haven't found a good way to (programmatically) address call depth when getting source caller info. Using a call depth of 6 (see code snippet) seems to properly unwind back to the originating `.Info()` etc method call, but I haven't checked other options. Example output, after shimming the following logging calls into the existing handler test suite: ```go gkadapterLogger := SlogFromGoKit(log.With(log.NewLogfmtLogger(os.Stderr), "caller", log.Caller(6))) gkadapterLogger.Info("go-kit/log adapter output") gkadapterLogger.WithGroup("g1").WithGroup("g2").Info("test1", "hello", "world") gkadapterLogger.WithGroup("g1").With("hello", "world").Info("test2", "hello", "world") gkadapterLogger.WithGroup("g1").With("hello", "world").WithGroup("g2").Info("test3", "hello", "world") gkadapterLogger.With("hello", "world").With("hello", "world").Info("test4", "hello", "world") // Output: // level=info caller=slogadapter_test.go:26 time=2024-11-18T13:09:59.674350655-05:00 msg=test1 g1.g2.hello=world // level=info caller=slogadapter_test.go:27 time=2024-11-18T13:09:59.674376383-05:00 msg=test2 g1.hello=world g1.hello=world // level=info caller=slogadapter_test.go:28 time=2024-11-18T13:09:59.674392598-05:00 msg=test3 g1.hello=world g1.g2.hello=world // level=info caller=slogadapter_test.go:29 time=2024-11-18T13:09:59.674419421-05:00 msg=test4 hello=world hello=world hello=world ``` Signed-off-by: TJ Hoplock <[email protected]>
aknuds1
previously approved these changes
Nov 18, 2024
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, thanks
aknuds1
requested changes
Nov 18, 2024
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.
Can you fix failing tests?
Needed to make tests for the slog adapter play nicely with Mock. Signed-off-by: TJ Hoplock <[email protected]>
- The adapter now uses a `preformatted` keys cache on the handler struct to more consistently order keys; msg is always first real key other than timestamp/level - as part of fixing group nesting, nested group key value pairs now take the form of `group1.group2.key=value`, update mock calls accordingly Signed-off-by: TJ Hoplock <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes included:
ts
kv pair in go-kit/log implementations).preformatted
field on the goKitHandler struct to cache previously added kv pairs containing log values. helpful to maintain the ordering of kv pairs added via multiple/nested/chained calls to a logger's.With()
or.WithGroup()
methods.Not addressed:
promslog
pkg..Info()
etc method call, but I haven't checked other options.Example output, after shimming the following logging calls into the existing handler test suite:
What this PR does
Which issue(s) this PR fixes or relates to
Fixes #
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]
.about-versioning.md
updated with experimental features.