Skip to content

fix(client): Capture exceptions raised by before_send callbacks#6851

Closed
ericapisani wants to merge 1 commit into
py-2617-handle-exceptions-in-callbacksfrom
py-2617-handle-exceptions-in-before-send-span
Closed

fix(client): Capture exceptions raised by before_send callbacks#6851
ericapisani wants to merge 1 commit into
py-2617-handle-exceptions-in-callbacksfrom
py-2617-handle-exceptions-in-before-send-span

Conversation

@ericapisani

Copy link
Copy Markdown
Member

Description

Issues

Reminders


Stack created with GitHub Stacks CLIGive Feedback 💬

Wrap the before_send callback invocation in capture_internal_exceptions()
so that exceptions raised by user-provided before_send_log,
before_send_metric, and before_send_span callbacks do not crash the
application. When a callback raises, the original, unmodified telemetry
item is sent.

Refs PY-2617
Refs #6849
@ericapisani
ericapisani requested a review from a team as a code owner July 20, 2026 18:40
@linear-code

linear-code Bot commented Jul 20, 2026

Copy link
Copy Markdown

PY-2617

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d2f4ee7. Configure here.

Comment thread sentry_sdk/client.py
if before_send is not None:
serialized = before_send(serialized, {}) # type: ignore[arg-type]
with capture_internal_exceptions():
serialized = before_send(serialized, {}) # type: ignore[arg-type]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Exception keeps telemetry instead of dropping

Medium Severity

When before_send_log or before_send_metric raises, serialized keeps its prior value and the item is still queued. That diverges from before_send and before_send_transaction in the same module, which treat a raised callback like a drop. A failing scrubbing callback can therefore still emit the original, possibly sensitive, payload.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d2f4ee7. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

94273 passed | ❌ 37 failed | ⏭️ 6339 skipped | Total: 100649 | Pass Rate: 93.67% | Execution Time: 326m 19s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +2433
Passed Tests 📈 +2359
Failed Tests 📈 +37
Skipped Tests 📈 +37

✅ Patch coverage is 100.00%. Project has 2481 uncovered lines.
✅ Project coverage is 89.75%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.68%    89.75%    +0.07%
==========================================
  Files          193       193         —
  Lines        24007     24200      +193
  Branches      8342      8494      +152
==========================================
+ Hits         21530     21719      +189
- Misses        2477      2481        +4
- Partials      1386      1381        -5

Generated by Codecov Action

@ericapisani
ericapisani deleted the py-2617-handle-exceptions-in-before-send-span branch July 20, 2026 18:55
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