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

[Merged by Bors] - Use zap logger in txs package #5973

Closed
wants to merge 3 commits into from

Conversation

fasmat
Copy link
Member

@fasmat fasmat commented May 23, 2024

Motivation

In the continued effort to replace our custom log.Log with zap.Logger I updated the txs package to not depend on our logger any more.

Description

Only logging was updated, no functional changes to the package

Test Plan

existing tests pass

TODO

  • Explain motivation or link existing issue(s)
  • Test changes and document test plan
  • Update documentation as needed
  • Update changelog as needed

@fasmat fasmat self-assigned this May 23, 2024
Copy link

codecov bot commented May 23, 2024

Codecov Report

Attention: Patch coverage is 89.76744% with 22 lines in your changes are missing coverage. Please review.

Project coverage is 80.8%. Comparing base (3ca48df) to head (e53fc38).
Report is 3 commits behind head on develop.

Files Patch % Lines
txs/cache.go 89.5% 17 Missing ⚠️
txs/conservative_state.go 57.1% 3 Missing ⚠️
txs/utils.go 50.0% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           develop   #5973     +/-   ##
=========================================
- Coverage     80.8%   80.8%   -0.1%     
=========================================
  Files          287     290      +3     
  Lines        29905   30572    +667     
=========================================
+ Hits         24183   24713    +530     
- Misses        4139    4228     +89     
- Partials      1583    1631     +48     

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

txs/cache.go Outdated Show resolved Hide resolved
txs/cache.go Outdated
db *sql.Database,
nextNonce, newBalance uint64,
applied types.LayerID,
) error {
logger = logger.WithFields(ac.addr)
logger.With().Debug("resetting to nonce", log.Uint64("nonce", nextNonce))
logger = logger.With(zap.Stringer("address", ac.addr))
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: this causes ac.addr.String() to be called every time, regardless of whether the logs are emitted. This might be inefficient if it happens on a hot path (I don't know - does it?). This logger emits logs down the stream only in error conditions and with the debug level, both shouldn't normally happen.

The addPendingFromNonce is a method of accountCache and it has access to the address field. How about instead of adding the address field to the logger, add it directly in the log calls to avoid the mentioned inefficiencies?

Copy link
Member Author

@fasmat fasmat May 23, 2024

Choose a reason for hiding this comment

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

Well it depends - the new code is equivalent to the previous one.

WithFields just unwrapped the provided fields and passed them to the internal zap.Logger by using With. So the old code also eagerly called String() on the ac.addr field.

The advantage of calling it eagerly is that the expensive String method on ac.addr is called only exactly once no matter how many log lines are printed with the new logger. Passing zap.Stringer("address", ac.addr)) to every log call later is much more expensive if they are actually printed and if they are not it doesn't matter.

@fasmat
Copy link
Member Author

fasmat commented May 23, 2024

bors merge

spacemesh-bors bot pushed a commit that referenced this pull request May 23, 2024
## Motivation

In the continued effort to replace our custom `log.Log` with `zap.Logger` I updated the `txs` package to not depend on our logger any more.
@spacemesh-bors
Copy link

Pull request successfully merged into develop.

Build succeeded:

@spacemesh-bors spacemesh-bors bot changed the title Use zap logger in txs package [Merged by Bors] - Use zap logger in txs package May 23, 2024
@spacemesh-bors spacemesh-bors bot closed this May 23, 2024
@spacemesh-bors spacemesh-bors bot deleted the zap-logger-in-txs branch May 23, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants