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

fix: enhance logging for request node #1463

Merged
merged 6 commits into from
Oct 3, 2024
Merged

Conversation

rodrigopavezi
Copy link
Contributor

@rodrigopavezi rodrigopavezi commented Oct 2, 2024

Description of the changes

  • Add logging for timeout
  • Add logging for requests and responses
  • Add more data to some existing logs

Fixes #1391

Summary by CodeRabbit

  • New Features

    • Integrated morgan logging middleware for enhanced request logging on the server.
  • Bug Fixes

    • Improved error handling and logging for IPFS add operations and transaction persistence, including timeout handling and detailed logging for both successful and failed operations.

Copy link

coderabbitai bot commented Oct 2, 2024

Walkthrough

The pull request introduces updates to the @requestnetwork/request-node package, focusing on enhancing logging and error handling. A new dependency, morgan, is added for logging incoming requests, and improvements are made to error handling in the IpfsAddHandler and PersistTransactionHandler classes. These modifications include timeout handling and detailed logging of transaction and request details, thereby improving the clarity and usability of logs.

Changes

File Path Change Summary
packages/request-node/package.json Added dependency "morgan": "1.10.0" and development dependency "@types/morgan": "1.9.9"
packages/request-node/src/request/ipfsAdd.ts Enhanced error handling and logging for IPFS add operation, including timeout handling and detailed logs.
packages/request-node/src/request/persistTransaction.ts Improved error handling and logging in PersistTransactionHandler, including timeout handling and detailed logs.
packages/request-node/src/requestNode.ts Integrated morgan middleware for logging incoming requests in the RequestNode class.

Assessment against linked issues

Objective Addressed Explanation
Every endpoint should log the incoming request and the outgoing response.
Logs should clearly label Channel ID, Request transaction ID, and on-chain transaction ID.
Log the request transaction ID, on-chain transaction ID, and IPFS CID when possible.
Log timeouts for the Request Node.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

🧹 Outside diff range comments (3)
packages/request-node/src/request/ipfsAdd.ts (1)

Line range hint 1-74: Summary of Review

Overall, the changes in this file effectively address the PR objectives by enhancing logging and error handling in the IpfsAddHandler class. The improvements include:

  1. Implementation of timeout handling and logging.
  2. Enhanced success logging with detailed IPFS operation results.
  3. Improved error logging with request data.

These changes will significantly improve the ability to monitor and debug the Request Node's IPFS operations.

Key points to consider:

  1. Add a comment explaining the conversion from seconds to milliseconds in the timeout setting.
  2. Ensure consistency in log messages between success and error cases.
  3. Address potential issues with logging large amounts of data or sensitive information in error logs.

Once these minor adjustments are made, the implementation will be robust and align perfectly with the PR objectives.

To further improve the logging system:

  1. Consider implementing a structured logging format (e.g., JSON) to make log parsing and analysis easier.
  2. Implement log correlation by including a unique request ID in all log messages related to a single operation.
  3. Consider adding log levels (e.g., INFO, WARN, ERROR) to facilitate log filtering and analysis.

These enhancements would make the logging system even more powerful and aligned with best practices in distributed systems.

packages/request-node/src/request/persistTransaction.ts (1)

Line range hint 35-109: Overall excellent improvements aligning with PR objectives!

The changes made to the PersistTransactionHandler class significantly enhance the logging capabilities of the Request Node, addressing the key objectives outlined in the PR summary and linked issue #1391. Here's a summary of the improvements:

  1. Implemented timeout logging, addressing the objective of logging timeout events.
  2. Enhanced logging for both successful and failed transactions, improving readability and analyzability.
  3. Clearly labeled logged data with relevant identifiers (transactionHash, channelId, topics).
  4. Consolidated relevant information in single log entries, making it easier to correlate data.

These enhancements will greatly facilitate better monitoring and debugging of the Request Node.

To further improve the logging architecture, consider:

  1. Implementing a structured logging format (e.g., JSON) throughout the application for easier parsing and analysis.
  2. Adding log correlation IDs to trace requests across different components of the system.
  3. Standardizing log levels and message formats across all logging statements for consistency.

These architectural improvements would further enhance the observability and maintainability of the Request Node.

packages/request-node/src/requestNode.ts (1)

Line range hint 1-193: Summary: Additional logging enhancements needed to fully meet PR objectives.

While the addition of morgan middleware improves request logging, several objectives from the PR are not yet addressed:

  1. Logging responses for all endpoints.
  2. Logging timeout events.
  3. Clearly labeling logged data with Channel ID, Request transaction ID, and on-chain transaction ID.
  4. Logging both request transaction ID and on-chain transaction ID along with IPFS CID when applicable.

To fully meet the PR objectives, consider the following suggestions:

  1. Implement response logging, possibly by extending the morgan middleware or adding a custom middleware.
  2. Add a timeout handling mechanism with associated logging.
  3. Extend the existing logging in individual request handlers (e.g., persistTransactionHandler, getConfirmedTransactionHandler) to include the required IDs.
  4. Implement a centralized logging function that ensures consistent formatting and inclusion of all required information.

Would you like assistance in designing and implementing these additional logging features?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 78a353d and 5ee006f.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (4)
  • packages/request-node/package.json (2 hunks)
  • packages/request-node/src/request/ipfsAdd.ts (2 hunks)
  • packages/request-node/src/request/persistTransaction.ts (2 hunks)
  • packages/request-node/src/requestNode.ts (2 hunks)
🔇 Additional comments (4)
packages/request-node/package.json (3)

61-61: Approved: Addition of Morgan for enhanced logging

The addition of the Morgan dependency (^1.10.0) is a good choice for improving the logging capabilities of the Request Node. This aligns well with the PR objectives to log every incoming request and outgoing response.


70-70: Approved: Addition of @types/morgan for TypeScript support

The addition of @types/morgan (^1.9.9) as a dev dependency is appropriate. It provides TypeScript type definitions for Morgan, which will enhance development experience and type safety when using Morgan in the project.


61-61: Verify Morgan implementation in the codebase

The addition of Morgan and its type definitions is a good step towards enhancing logging. To ensure full implementation:

  1. Verify that Morgan is properly configured and used in the main application file.
  2. Check if Morgan's logging format is customized to include Channel ID, Request transaction ID, and on-chain transaction ID as per the objectives.
  3. Ensure that timeout events are also being logged, which might require additional configuration beyond Morgan.

To help verify the Morgan implementation, you can run the following script:

This script will help identify where and how Morgan is being used in the codebase, as well as check for timeout logging implementation.

Also applies to: 70-70

packages/request-node/src/requestNode.ts (1)

14-14: LGTM: Morgan import added correctly.

The import statement for the morgan logging middleware is correctly placed and uses the appropriate syntax.

packages/request-node/src/request/ipfsAdd.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/requestNode.ts Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 5ee006f and 2436a4f.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (1)
  • packages/request-node/package.json (2 hunks)

packages/request-node/package.json Show resolved Hide resolved
packages/request-node/package.json Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/requestNode.ts Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 2436a4f and f6da6d6.

📒 Files selected for processing (2)
  • packages/request-node/src/request/ipfsAdd.ts (2 hunks)
  • packages/request-node/src/request/persistTransaction.ts (3 hunks)
🔇 Additional comments (3)
packages/request-node/src/request/persistTransaction.ts (3)

33-35: LGTM: Improved transaction hash handling

The introduction of the transactionHash variable using normalizeKeccak256Hash is a good addition. It aligns with the PR objective of clearly labeling logged data and will improve log correlation and readability.


79-85: LGTM: Improved 'confirmed' event logging

The updated 'confirmed' event handler now includes both transactionHash and channelId in the log message. This change addresses the PR objective of logging both request transaction ID and on-chain transaction ID, improving log correlation and readability.


98-104: LGTM: Concise and informative success logging

The updated success logging now includes transactionHash and channelId, while removing unnecessary details like topics and transactionData. This change aligns with the PR objectives of logging every request and clearly labeling logged data. The use of 'metric' and 'successRate' tags is also beneficial for log analysis.

packages/request-node/src/request/persistTransaction.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Outdated Show resolved Hide resolved
packages/request-node/src/request/ipfsAdd.ts Show resolved Hide resolved
Copy link
Member

@MantisClone MantisClone left a comment

Choose a reason for hiding this comment

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

I'm pre-approving on contingency that all comments are resolved either by making a code change or explaining why no change is necessary.

Nice work! 💪

@rodrigopavezi
Copy link
Contributor Author

I'm pre-approving on contingency that all comments are resolved either by making a code change or explaining why no change is necessary.

Nice work! 💪

Thanks @MantisClone, good review requests. I have addressed them.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range comments (1)
packages/request-node/src/request/persistTransaction.ts (1)

Line range hint 63-86: Improved logging for transaction persistence and confirmation

The addition of detailed logging before persisting the transaction and in the 'confirmed' event handler significantly enhances the observability of the Request Node. This change addresses the objectives of logging every incoming request and clearly labeling logged data.

For consistency and improved readability, consider using template literals for the log messages:

this.logger.debug(
  `Persisting Transaction: ${JSON.stringify({
    transactionHash,
    channelId: clientRequest.body.channelId,
    topics: clientRequest.body.topics,
    transactionData: clientRequest.body.transactionData,
  })}`,
);

// ... and in the 'confirmed' event handler:

this.logger.info(
  `Transaction confirmed: ${JSON.stringify({
    transactionHash,
    channelId: clientRequest.body.channelId,
  })}`,
  ['metric', 'successRate'],
);

This change would make the log messages more consistent and easier to read.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between f6da6d6 and 96ccf7e.

📒 Files selected for processing (2)
  • packages/request-node/src/request/ipfsAdd.ts (1 hunks)
  • packages/request-node/src/request/persistTransaction.ts (3 hunks)
🔇 Additional comments (3)
packages/request-node/src/request/persistTransaction.ts (2)

99-105: Well-implemented success logging

The addition of debug logging after successful transaction completion, including transactionHash and channelId, enhances the observability of the Request Node. This change addresses the objective of logging every outgoing response and follows the recommendations from previous reviews.

The log message format is clear and consistent, providing essential information without unnecessary details. Good job on implementing the suggested improvements.


Line range hint 1-124: Overall excellent improvements to logging and error handling

The changes made to the PersistTransactionHandler class significantly enhance the logging and error handling capabilities of the Request Node. These improvements address the main objectives of the PR, including:

  1. Logging every incoming request and outgoing response
  2. Clearly labeling logged data
  3. Implementing logging for timeout events

The code now provides more comprehensive and detailed logs, which will greatly improve the ability to monitor and debug the Request Node.

Key improvements:

  • Addition of timeout handling with detailed logging
  • Enhanced logging for transaction persistence, confirmation, and errors
  • Clearer labeling of logged data, including transactionHash and channelId

Remaining suggestions for consideration:

  1. Add a null check for clientRequest.body.transactionData before calculating the hash
  2. Extract the timeout duration to a constant for improved readability
  3. Use template literals consistently for log messages
  4. Consider combining error and debug logs for failed transactions

Overall, these changes represent a significant step forward in improving the Request Node's logging system. Great job on implementing most of the suggested improvements from previous reviews!

packages/request-node/src/request/ipfsAdd.ts (1)

56-62: Logging enhancements are well implemented

The success logging provides valuable information for monitoring and debugging, aligning with the PR objectives.

@rodrigopavezi rodrigopavezi merged commit 0e07b30 into master Oct 3, 2024
25 checks passed
@rodrigopavezi rodrigopavezi deleted the fix/request-node-logging branch October 3, 2024 16:28
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.

Improve Request Node Logging
3 participants