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

feat: add log level guards to prevent unnecessary string generation #3154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

belloibrahv
Copy link

Description:
This PR optimizes logging performance by implementing proper log level guards using Pino's isLevelEnabled API.
Changes made:

  • Add isLevelEnabled checks before debug and trace logging calls
  • Wrap existing debug/trace logs with level guards
  • Implement consistent logging patterns across the codebase

Example of optimized logging:

if (cachedResponse) {
  if (this.logger.isLevelEnabled('debug')) {
    this.logger.debug(
      `${requestIdPrefix} getTransactionReceipt returned cached response: ${JSON.stringify(cachedResponse)}`,
    );
  }
  return cachedResponse;
}

if (!syntheticLogs.length) {
  if (this.logger.isLevelEnabled('trace')) {
    this.logger.trace(`${requestIdPrefix} no receipt for ${hash}`);
  }
  return null;
}

Related issue(s):
Fixes #1118

Notes for reviewer:

  • The implemented guards prevent unnecessary string interpolation and JSON.stringify operations when debug/trace logging is disabled
  • Pattern is consistently applied across debug and trace level logs
  • No functional changes to the logging output - only performance optimization

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)
    • Verified log output with different log levels
    • Confirmed debug/trace messages are properly guarded
    • Tested with logging enabled and disabled to ensure proper functionality

@Nana-EC
Copy link
Collaborator

Nana-EC commented Oct 24, 2024

@belloibrahv you might want to rebase, there's a lot of unrelated changes that were pulled in

@belloibrahv belloibrahv changed the title add log level guards to prevent unnecessary string generation feat: add log level guards to prevent unnecessary string generation Oct 24, 2024
@belloibrahv
Copy link
Author

@Nana-EC, I've rebased the branch and cleaned up the PR to only include the log level guard changes.

@quiet-node quiet-node added enhancement New feature or request P3 perf performance gains good first issue candidate Issues that can become a good first issue but need more description/context. hacktoberfest Issues shown by lists for the Hacktoberfest and made for newcomers to do the first contribution. labels Oct 24, 2024
@quiet-node quiet-node added this to the 0.59.0 milestone Oct 24, 2024
@Nana-EC
Copy link
Collaborator

Nana-EC commented Oct 25, 2024

@Nana-EC, I've rebased the branch and cleaned up the PR to only include the log level guard changes.

@belloibrahv please double check.
Looking at https://github.com/hashgraph/hedera-json-rpc-relay/pull/3154/files I'm still seeing 176 files getting changed, with many not relating to logs at all

@ebadiere ebadiere modified the milestones: 0.59.0, 0.60.0 Oct 30, 2024
@belloibrahv
Copy link
Author

belloibrahv commented Oct 31, 2024

@ebadiere @quiet-node @shemnon @Nana-EC please help review my PR. Thank you

@quiet-node
Copy link
Member

@belloibrahv hey thanks for the good work!

Looks like the DCO CI didn't pass due to this commit refactor: reduce code duplication in relay package didn't get signed off. Looking at the Details of the DCO job, it suggeests to run the commands below to fix it:

git rebase HEAD~7 --signoff
git push --force-with-lease origin issue-1118-log-level-guard

@belloibrahv
Copy link
Author

@ebadiere @quiet-node @shemnon @Nana-EC please help review my PR. Thank you

@quiet-node
Copy link
Member

heyy @belloibrahv thanks for updating the PR! However, the DCO still seems to fail. Also it seems like the PR now has some conflicts. Please Pull and resolve the conflicts.

@belloibrahv belloibrahv force-pushed the issue-1118-log-level-guard branch 2 times, most recently from bed5f27 to 1e9da7e Compare November 1, 2024 12:42
@belloibrahv
Copy link
Author

@Nana-EC @quiet-node @ebadiere @victor-yanev, I've been working to resolve the DCO (Developer Certificate of Origin) issues on this PR, but I'm encountering some challenges. The DCO check is failing due to incorrect sign-offs on several commits that were merged from the main branch.
I've tried the suggested approaches, including rebasing the branch and adding the sign-off manually, but I'm still running into problems. The commits from other authors seem to be the root cause, and I'm having difficulty properly resolving the sign-off discrepancies.
At this point, I would greatly appreciate if you could provide some guidance on how to best address this. I'm happy to work with you directly to find a solution, as I'm anxious to get this PR merged. Please let me know if you have any suggestions or if there's a better way I should be approaching this. Thank you for your understanding and assistance.

@Nana-EC
Copy link
Collaborator

Nana-EC commented Nov 1, 2024

@Nana-EC @quiet-node @ebadiere @victor-yanev, I've been working to resolve the DCO (Developer Certificate of Origin) issues on this PR, but I'm encountering some challenges. The DCO check is failing due to incorrect sign-offs on several commits that were merged from the main branch. I've tried the suggested approaches, including rebasing the branch and adding the sign-off manually, but I'm still running into problems. The commits from other authors seem to be the root cause, and I'm having difficulty properly resolving the sign-off discrepancies. At this point, I would greatly appreciate if you could provide some guidance on how to best address this. I'm happy to work with you directly to find a solution, as I'm anxious to get this PR merged. Please let me know if you have any suggestions or if there's a better way I should be approaching this. Thank you for your understanding and assistance.

HI @belloibrahv , you need to do a clean rebase.
This would mean this PR would have only your commits on top of the latest main branch, no others authors commit would be included and you wouldn't encounter any DCO given you sign and sign off.
If your branch actually includes others commits at this point it might be easier to do a new clean branch and add only your changes on top of it.
Your changes look good otherwise and we can merge this once all the steps are successful in CI.

@belloibrahv
Copy link
Author

@Nana-EC @quiet-node @ebadiere @victor-yanev, I've been able to resolve the DCO, kindly help review the PR. Thank you.

@quiet-node
Copy link
Member

@belloibrahv It appears your changes have broken the CI with the following error: src/lib/config/hbarSpendingPlanConfigService.ts(135,16): error TS2339: Property 'logger' does not exist on type 'typeof HbarSpendingPlanConfigService'. Please double-check.

Signed-off-by: belloibrahv <[email protected]>
Copy link

sonarcloud bot commented Nov 5, 2024

@belloibrahv
Copy link
Author

@Nana-EC @quiet-node @ebadiere @shemnon kindly help review the PR, Thank you.

@@ -131,7 +131,9 @@ export class HbarSpendingPlanConfigService {
const spendingPlanConfig = ConfigService.get('HBAR_SPENDING_PLANS_CONFIG') as string;

if (!spendingPlanConfig) {
logger.trace('HBAR_SPENDING_PLANS_CONFIG is undefined');
if (this.logger.isLevelEnabled('trace')) {
Copy link
Member

@quiet-node quiet-node Nov 5, 2024

Choose a reason for hiding this comment

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

@belloibrahv This line is causing the CI failures because this class doesn’t have a logger property. This can be easily fixed by using the logger instance from the arguments, which should resolve the CI issues.

Suggested change
if (this.logger.isLevelEnabled('trace')) {
if (logger.isLevelEnabled('trace')) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice catch @quiet-node.
@belloibrahv please run an npm run build-and-test before pushing to confirm everything builds and unit tests run well.
Looking forward to getting this PR in.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue candidate Issues that can become a good first issue but need more description/context. hacktoberfest Issues shown by lists for the Hacktoberfest and made for newcomers to do the first contribution. P3 perf performance gains
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add logging level guarding
4 participants