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

Enhance panic error logging in SDK by adding stack trace #272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

onur-yildirim-infinitusai
Copy link

@onur-yildirim-infinitusai onur-yildirim-infinitusai commented Jan 7, 2025

Proposed changes

Currently, when a panic occurs in the SDK, the error messages are generic and don't provide enough context for debugging:

"Panic triggered"
"ProcessError(fatal panic - attempt to recover) failed"
"listen: Fatal socket error"
Screenshot 2025-01-06 at 23 51 51

This PR adds detailed stack traces to panic recovery scenarios to help developers better understand and troubleshoot the root cause of failures. The enhanced logging will show the full call stack where the panic originated, making it easier to identify issues.

Types of changes

What types of changes does your code introduce to the community Go SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced error handling and logging for WebSocket client operations.
    • Improved panic recovery mechanisms across multiple WebSocket client components.
    • Added detailed stack trace logging during unexpected errors.
  • Chores

    • Added runtime/debug package for advanced error tracking.
    • Updated error logging to provide more comprehensive diagnostic information.

Copy link

coderabbitai bot commented Jan 7, 2025

Walkthrough

The pull request introduces consistent panic recovery and logging mechanisms across multiple WebSocket client files. The changes focus on enhancing error handling by importing the runtime/debug package and adding detailed logging for panic scenarios. Specifically, in various WebSocket client files, new log statements capture panic values and stack traces during method execution, particularly in the ping and flush methods. Additionally, an intentional panic is introduced for testing purposes, ensuring that the application logs the panic details and stack trace before closing the WebSocket connection.

Changes

File Change Summary
pkg/client/common/v1/websocket.go Added runtime/debug import and panic logging in listen method
pkg/client/listen/v1/websocket/client_callback.go Added panic recovery logging in ping and flush methods
pkg/client/listen/v1/websocket/client_channel.go Added panic recovery logging in ping and flush methods
pkg/client/speak/v1/websocket/client_callback.go Added runtime/debug import and enhanced panic logging in flush method
pkg/client/speak/v1/websocket/client_channel.go Added runtime/debug import, updated fatal socket error log prefix, and added stack trace logging

Sequence Diagram

sequenceDiagram
    participant Client
    participant WebSocket
    participant ErrorHandler
    
    Client->>WebSocket: Initiate Connection
    WebSocket->>Client: Connection Established
    
    alt Panic Occurs
        WebSocket->>ErrorHandler: Capture Panic
        ErrorHandler-->>ErrorHandler: Log Panic Value
        ErrorHandler-->>ErrorHandler: Log Stack Trace
        ErrorHandler->>Client: Close Connection
    end
Loading

The sequence diagram illustrates the enhanced error handling workflow, showing how panics are now captured, logged with detailed information, and managed during WebSocket interactions.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e9fbb05 and 5a3960f.

📒 Files selected for processing (5)
  • pkg/client/common/v1/websocket.go (2 hunks)
  • pkg/client/listen/v1/websocket/client_callback.go (3 hunks)
  • pkg/client/listen/v1/websocket/client_channel.go (3 hunks)
  • pkg/client/speak/v1/websocket/client_callback.go (2 hunks)
  • pkg/client/speak/v1/websocket/client_channel.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • pkg/client/listen/v1/websocket/client_channel.go
  • pkg/client/speak/v1/websocket/client_channel.go
  • pkg/client/common/v1/websocket.go
  • pkg/client/speak/v1/websocket/client_callback.go
  • pkg/client/listen/v1/websocket/client_callback.go

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@@ -217,7 +218,9 @@ func (c *WSChannel) flush() {
err := common.ErrFatalPanicRecovered
sendErr := c.ProcessError(err)
if sendErr != nil {
klog.V(1).Infof("listen: Fatal socket error. Err: %v\n", sendErr)
klog.V(1).Infof("speak: Fatal socket error. Err: %v\n", sendErr)

Choose a reason for hiding this comment

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

changing log to speak bc this is use in the speak side.

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: 2

🧹 Nitpick comments (3)
pkg/client/speak/v1/websocket/client_callback.go (1)

223-224: LGTM: Enhanced panic logging with stack trace.

The additional logging statements provide valuable context for debugging by capturing both the panic value and the stack trace.

Consider adding structured logging in a future enhancement to make the logs more machine-parseable.

-klog.V(1).Infof("Panic: %v\n", r)
-klog.V(1).Infof("Stack trace: %s\n", string(debug.Stack()))
+klog.V(1).Infof("panic_details: {\"value\": %q, \"stack_trace\": %q}\n", r, string(debug.Stack()))
pkg/client/listen/v1/websocket/client_channel.go (1)

256-257: Consider extracting panic logging to a utility function.

The panic logging implementation is duplicated across multiple files and methods. Consider creating a common utility function to improve maintainability and ensure consistent logging format.

// Add to common/v1/logging/panic.go
+package logging
+
+import (
+    "runtime/debug"
+    klog "k8s.io/klog/v2"
+)
+
+// LogPanic logs panic details with stack trace
+func LogPanic(r interface{}, prefix string) {
+    klog.V(1).Infof("%s: Fatal socket error\n", prefix)
+    klog.V(1).Infof("Panic: %v\n", r)
+    klog.V(1).Infof("Stack trace: %s\n", string(debug.Stack()))
+}

Then use it in the panic recovery blocks:

-klog.V(1).Infof("listen: Fatal socket error. Err: %v\n", sendErr)
-klog.V(1).Infof("Panic: %v\n", r)
-klog.V(1).Infof("Stack trace: %s\n", string(debug.Stack()))
+logging.LogPanic(r, "listen")

Also applies to: 301-302

pkg/client/common/v1/websocket.go (1)

Line range hint 1-644: Consider extending panic recovery to other critical methods.

The enhanced panic logging is currently only implemented in the listen method. Consider adding similar panic recovery blocks to other critical methods that handle WebSocket communication (WriteBinary, WriteJSON) and connection cleanup (closeWs). This would provide consistent error handling and debugging capabilities across the entire WebSocket client implementation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 42c3486 and e27fce3.

📒 Files selected for processing (5)
  • pkg/client/common/v1/websocket.go (3 hunks)
  • pkg/client/listen/v1/websocket/client_callback.go (3 hunks)
  • pkg/client/listen/v1/websocket/client_channel.go (3 hunks)
  • pkg/client/speak/v1/websocket/client_callback.go (2 hunks)
  • pkg/client/speak/v1/websocket/client_channel.go (2 hunks)
🔇 Additional comments (7)
pkg/client/speak/v1/websocket/client_callback.go (1)

13-13: LGTM: Import added for stack trace functionality.

The addition of the runtime/debug package is necessary for implementing the enhanced panic logging feature.

pkg/client/speak/v1/websocket/client_channel.go (2)

13-13: LGTM: Import added for stack trace functionality.

The addition of the runtime/debug package is consistent with the implementation across other files.


221-223: LGTM: Enhanced panic logging with stack trace.

The implementation follows the consistent pattern of logging both panic value and stack trace.

pkg/client/listen/v1/websocket/client_callback.go (2)

14-14: LGTM: Import added for stack trace functionality.

The addition of the runtime/debug package is consistent with the implementation across other files.


256-257: LGTM: Enhanced panic logging in both ping and flush methods.

The implementation consistently adds panic value and stack trace logging to both methods that can potentially panic.

Also applies to: 301-302

pkg/client/listen/v1/websocket/client_channel.go (1)

14-14: LGTM: Import added for stack trace functionality.

The addition of the runtime/debug package is consistent with the implementation across other files.

pkg/client/common/v1/websocket.go (1)

16-16: LGTM: Import required for stack trace capture.

The addition of the runtime/debug package is necessary for implementing the enhanced panic logging feature.

pkg/client/common/v1/websocket.go Outdated Show resolved Hide resolved
pkg/client/common/v1/websocket.go Outdated Show resolved Hide resolved
@onur-yildirim-infinitusai onur-yildirim-infinitusai force-pushed the adding-stack-trace-for-panics branch 2 times, most recently from e9fbb05 to 2b83715 Compare January 7, 2025 08:12
@onur-yildirim-infinitusai onur-yildirim-infinitusai force-pushed the adding-stack-trace-for-panics branch from 2b83715 to 5a3960f Compare January 7, 2025 17:37
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.

1 participant