Skip to content

feat: add stacktrace to panics - #4438

Merged
chrisgacsal merged 1 commit into
mainfrom
feat/stacktrace
May 26, 2026
Merged

chrisgacsal merged 1 commit into
mainfrom
feat/stacktrace

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Include stacktrace info if wrapped function panicked.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced panic recovery to capture and record stacktrace information with error status, providing improved visibility into unexpected failures during debugging and production monitoring.

Review Change Stack

@chrisgacsal chrisgacsal self-assigned this May 26, 2026
@chrisgacsal
chrisgacsal requested a review from a team as a code owner May 26, 2026 09:23
@chrisgacsal chrisgacsal added the release-note/misc Miscellaneous changes label May 26, 2026
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR enhances panic recovery in span wrapping by capturing the panic value, recording it with a runtime stacktrace attribute, setting the span status to Error, properly ending the span, and then re-panicking with the original panic value.

Changes

Panic Recovery Enhancement

Layer / File(s) Summary
Panic recovery with stacktrace attribute
pkg/framework/tracex/tracex.go
Imports runtime/debug and go.opentelemetry.io/otel/attribute to enable stacktrace capture. The defer panic recovery in Span[T].Wrap is reworked to record the panic error with a panic.stacktrace attribute sourced from debug.Stack(), set span status to otelcodes.Error with reason "panic", and ensure the span ends before re-panicking with the original recovered value.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding stacktrace information to panic recovery in the Span.Wrap method.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/stacktrace

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
pkg/framework/tracex/tracex.go (2)

50-50: ⚡ Quick win

Simplify the stacktrace string conversion.

The fmt.Sprintf("%v", ...) wrapper is redundant here since debug.Stack() already returns a []byte that you're converting to string. You can pass string(debug.Stack()) directly to attribute.String().

♻️ Simpler approach
-					attribute.String("panic.stacktrace", fmt.Sprintf("%v", string(debug.Stack()))),
+					attribute.String("panic.stacktrace", string(debug.Stack())),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/framework/tracex/tracex.go` at line 50, Replace the redundant fmt.Sprintf
wrapper around the stacktrace conversion in the attribute.String call: directly
pass string(debug.Stack()) to attribute.String("panic.stacktrace", ...) instead
of using fmt.Sprintf("%v", string(debug.Stack())); locate the attribute.String
invocation in tracex.go (the panic stacktrace assignment) and update it
accordingly.

48-50: Stack trace capture: WithStackTrace(true) plus panic.stacktrace is likely redundant

In pkg/framework/tracex/tracex.go (panic recovery, around lines 48–50), trace.WithStackTrace(true) records the error with the standard OTel exception stacktrace attributes (e.g., exception.stacktrace), and you also add a custom panic.stacktrace attribute from debug.Stack(). If no consumer relies specifically on the custom panic.stacktrace key, you can drop either trace.WithStackTrace(true) or the custom attribute to avoid duplicating work/output (or rename the custom attribute to align with the standard semantic convention).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/framework/tracex/tracex.go` around lines 48 - 50, The code currently
records both trace.WithStackTrace(true) and a custom attribute via
attribute.String("panic.stacktrace", fmt.Sprintf("%v", string(debug.Stack()))),
causing duplicate stacktrace output; fix by removing the custom attribute line
(attribute.String("panic.stacktrace", ...)) and rely on
trace.WithStackTrace(true), or alternatively rename the custom key to the
semantic convention "exception.stacktrace" and remove trace.WithStackTrace(true)
so only one stacktrace source remains—update the logic around
trace.WithStackTrace(true) and attribute.String(...) in the panic recovery code
(tracex.go) accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/framework/tracex/tracex.go`:
- Line 50: Replace the redundant fmt.Sprintf wrapper around the stacktrace
conversion in the attribute.String call: directly pass string(debug.Stack()) to
attribute.String("panic.stacktrace", ...) instead of using fmt.Sprintf("%v",
string(debug.Stack())); locate the attribute.String invocation in tracex.go (the
panic stacktrace assignment) and update it accordingly.
- Around line 48-50: The code currently records both trace.WithStackTrace(true)
and a custom attribute via attribute.String("panic.stacktrace",
fmt.Sprintf("%v", string(debug.Stack()))), causing duplicate stacktrace output;
fix by removing the custom attribute line (attribute.String("panic.stacktrace",
...)) and rely on trace.WithStackTrace(true), or alternatively rename the custom
key to the semantic convention "exception.stacktrace" and remove
trace.WithStackTrace(true) so only one stacktrace source remains—update the
logic around trace.WithStackTrace(true) and attribute.String(...) in the panic
recovery code (tracex.go) accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 59bc7555-8a33-408d-9e8a-8e1ace1f261e

📥 Commits

Reviewing files that changed from the base of the PR and between 7cf7e19 and 180d0a1.

📒 Files selected for processing (1)
  • pkg/framework/tracex/tracex.go

@chrisgacsal
chrisgacsal merged commit 2057439 into main May 26, 2026
31 of 33 checks passed
@chrisgacsal
chrisgacsal deleted the feat/stacktrace branch May 26, 2026 11:33

This branch was previously deployed

1 inactive deployment
dev — 180d0a1f Deployed May 26, 2026 by chrisgacsal via Migration Checks #14645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants