-
Notifications
You must be signed in to change notification settings - Fork 441
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
contrib/log/slog: fix WithAttrs and WithGroup implementation #2857
Conversation
BenchmarksBenchmark execution time: 2024-10-18 13:55:16 Comparing candidate commit 221dcff in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 58 metrics, 1 unstable metrics. |
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.
Thank you for addressing the issue. Let me leave one comment.
contrib/log/slog/slog.go
Outdated
curGroup := h.groups[len(h.groups)-1] | ||
|
||
groupAttrs := groupAttrsCopy(h.groupAttrs) | ||
groupAttrs[curGroup] = append(groupAttrs[curGroup], attrs...) |
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.
It is possible that the same group name appears multiple times.
I think it might be better to keep the attributes by using the depth of groups.
like groupAttrs [][]slog.Attr
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.
@shota3506 you are right, nice catch! 👍
if len(g.attrs) > 0 { | ||
reqHandler = reqHandler.WithAttrs(g.attrs) | ||
} | ||
} |
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.
So everytime Handle
is invoked, we have to copy over the existing attributes + the new trace attributes, as well as the existing groups?
Is this a performance concern? Is Handle
called often?
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.
I think this is definitely gonna have a performance impact. Not sure about slog
internals, but I would assume this is called every time a log entry is created (so depends on the application). Sadly, I don't see another way to solve the .Group
issue without the performance impact.
However this is a nice call, I will write a benchmark to measure how worse this is 👍
groups := append([]group{}, h.groups...) | ||
curGroup := groups[len(groups)-1] | ||
curGroup.attrs = append(curGroup.attrs, attrs...) | ||
groups[len(groups)-1] = curGroup |
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.
So a call to WithAttrs
applies attributes to the most-recently added group. I'm assuming WithAttrs
is only ever called when initializing the Handler, like this?
Handler.WithAttrs(<attr1>).WithGroup(<group1>).WithAttrs(<attr2>)
(Where attr1 is applied globally to the handler, and attr2 is applied to group1?)
If you were to use it like this:
handler := Handler.WithGroup(<group1>)
<some other stuff>
handler = handler.WithGroup(<group2>)
<some other stuff>
handler = handler.WithAttrs(<attr1>)
Then it's less obvious that attr1 would be applied to only group2.
So just confirming this is all in-line with how slog is used. 👍
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.
The handler methods are not called directly, these are used through the slog
api like:
logger := slog.Default().With("id", systemID).WithGroup("group").With("nested", "attr")
Feel free to check the slog documentation to confirm, but basically what you said is correct 👍
@mtoffl01 Wrote some more benchmarks locally, comparing the previous version
Since these changes have a notable performance degradation compared to the previous version, I will mark the PR as draft for now to discuss if there's an alternative approach. |
I've taken a close look at this and also experimented with some code ideas. My main takeaways are:
So I think we should go ahead with this. |
@felixge great, thanks so much for taking a look! |
Co-authored-by: Dario Castañé <[email protected]>
What does this PR do?
Fixes #2838
Motivation
Since we were embedding the wrapped
slog.Handler
, we were using the original implementation ofWithAttrs
andWithGroup
from the original handler, and therefore losing our custom implementation that adds the trace and span ids.Also, as @shota3506 mentioned in #2839, the tricky part is the
Group
implementation, since DataDog expected thedd.trace_id
anddd.span_id
keys to be root level. Hopefully this implementation solves this issue.In addition, it changes the type used for these fields from
slog.Uint64
toslog.String
as recommended in the docs.Reviewer's Checklist
Unsure? Have a question? Request a review!