Skip to content

fix(active-record): avoid error spans for handled RecordInvalid#2087

Open
aviralgarg05 wants to merge 4 commits into
open-telemetry:mainfrom
aviralgarg05:fix/issue-1459-active-record-recordinvalid-spans
Open

fix(active-record): avoid error spans for handled RecordInvalid#2087
aviralgarg05 wants to merge 4 commits into
open-telemetry:mainfrom
aviralgarg05:fix/issue-1459-active-record-recordinvalid-spans

Conversation

@aviralgarg05

@aviralgarg05 aviralgarg05 commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR fixes issue #1459 by preventing low-signal error spans for app-handled ActiveRecord bang persistence exceptions.

What changed

  • save!, create!, and update! now close spans and re-raise configured handled exceptions outside the span.
  • Default handled exception list remains narrow: [ActiveRecord::RecordInvalid].
  • Added optional configuration: handled_exceptions, which can include entries like ActiveRecord::RecordNotFound when teams explicitly want that behavior.

Why this is in scope

  • Default behavior still targets the original issue for RecordInvalid.
  • Reviewer feedback about configurability is implemented without broadening defaults.

Tests

  • RecordInvalid is suppressed for save!, create!, and update!.
  • Non-handled exceptions still emit exception events.
  • Configured handled exception behavior is covered by tests.

@dmathieu dmathieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about other errors that usually don't raise either, such as RecordNotFound?
Should the list of exceptions be configurable?

@aviralgarg05 aviralgarg05 changed the title fix(active_record): avoid error spans for handled RecordInvalid fix(active-record): avoid error spans for handled RecordInvalid Mar 17, 2026
@aviralgarg05

Copy link
Copy Markdown
Contributor Author

What about other errors that usually don't raise either, such as RecordNotFound? Should the list of exceptions be configurable?

Applied these as well

@github-actions

Copy link
Copy Markdown
Contributor

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions Bot added the stale Marks an issue/PR stale label Apr 17, 2026

@arielvalentin arielvalentin 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.

Thank you for this contribution and patience. We are low on reviewing capacity recently so its taking me a while to get to changes like yours for a review.

I think that the way Rails allows flow and control through exception handling is a bit at odds with the guidance in the spec which recommends we should record an error if it is unhandled and set the span status to Error.

https://opentelemetry.io/docs/specs/otel/trace/exceptions/#recording-an-exception

Adding allow lists to a library in this way and then interrupting the flow control and re-raising seems like something we would want to centralized in the in_span helper or these methods should inline the in_span helper and only record unexpected exceptions.

I think we can also avoid the unnecessary comparisons in the handle_exception method if we explicitly handled them in rescue blocks.

@github-actions github-actions Bot removed the stale Marks an issue/PR stale label May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants