Skip to content
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

zapslog: fix all with slogtest, support inline group, ignore empty group. #1408

Merged
merged 9 commits into from
Feb 5, 2024

Conversation

arukiidou
Copy link
Contributor

@arukiidou arukiidou commented Feb 2, 2024

This change adds a test based on testing/slogtest
that verifies compliance with the slog handler contract
(a draft of this was available in #1335),
and fixes all resulting issues.

The two remaining issues were:

  • Group("", attrs) should inline the new fields
    instead of creating a group with an empty name.
    This was fixed with the use of zap.Inline.
  • Groups without any attributes should not be created.
    That is, logger.WithGroup("foo").Info("bar") should not
    create an empty "foo" namespace ("foo": {}).
    This was fixed by keeping track of unapplied groups
    and applying them the first time a field is serialized.

Following this change, slogtest passes as expected.

Refs #1333
Resolves #1334, #1401, #1402
Supersedes #1263, #1335

TESTS

@arukiidou
Copy link
Contributor Author

@abhinav @tchung1118
Please take a look at this one? Thanks!

Copy link

codecov bot commented Feb 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (27b96e3) 98.42% compared to head (8466cda) 98.44%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1408      +/-   ##
==========================================
+ Coverage   98.42%   98.44%   +0.01%     
==========================================
  Files          53       53              
  Lines        3495     3527      +32     
==========================================
+ Hits         3440     3472      +32     
  Misses         46       46              
  Partials        9        9              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, @arukiidou! Really appreciate the work you put into matching the slog.Handler requirements. This is looking pretty good at a high-level, but there's one issue with the state management: we can't change the handler state like that.
Once a Handler is constructed, consider all its fields immutable. Only new handlers (like the ones returned by WithAttrs) can have different values for those fields—that's the only time we can mutate them. After construction, these handlers will be re-used for concurrent log calls, so they can't modify their own state. I've left suggestions on how I think we could fix the issue.

exp/zapslog/handler.go Outdated Show resolved Hide resolved
exp/zapslog/handler.go Outdated Show resolved Hide resolved
@arukiidou

This comment was marked as resolved.

@arukiidou

This comment was marked as resolved.

Logger name is already tested in WithName.
It doesn't need to be duplicated in every test.
This requires holding onto a slice instead of a single string
to create the nested namespaces.

The slice is cloned on each WithGroup call.
We could probably do this more efficiently with an immutable tree of
references, but deferring that for now.

In the process, also make the addedNamespace business
append during the record.Attrs loop instead of after.
This is more efficient because we don't have to shift the slice over
afterwards.
Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix, @arukiidou!

I made a few minor fixups and changed the holdGroup to a slice because the string can't handle cases like WithGroup("foo").WithGroup("bar").Info("msg").

The change looks good to me now. I'll ping a couple of the other maintainers to try to get their approval as well.

Again, thanks for the contribution!

@abhinav
Copy link
Collaborator

abhinav commented Feb 5, 2024

Got an offline LGTM from @mway.
Merging!

@abhinav abhinav merged commit 35ded09 into uber-go:master Feb 5, 2024
8 checks passed
@abhinav
Copy link
Collaborator

abhinav commented Feb 5, 2024

Thanks, @arukiidou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

zapslog.Handler: Test with slogtest, fix all failures
2 participants