Skip to content

refactor(sentry): replace manual flush with context lifecycle management#1070

Draft
huangdijia wants to merge 15 commits into
3.1from
refactor/sentry-context-management
Draft

refactor(sentry): replace manual flush with context lifecycle management#1070
huangdijia wants to merge 15 commits into
3.1from
refactor/sentry-context-management

Conversation

@huangdijia

Copy link
Copy Markdown
Contributor

Summary

Refactor Sentry integration to use SentrySdk::startContext() / endContext() for coroutine context management, replacing the previous pattern of manual SentrySdk::flush() calls scattered across the codebase.

Background

The previous implementation manually called SentrySdk::flush() in multiple listeners and aspects after each operation. Additionally, SentrySdkAspect intercepted SentrySdk methods via AOP to manage coroutine-safe hub/context state. This approach was fragile and scattered flush logic across many files.

Changes

  • Delete SentrySdkAspect — Remove AOP interception of SentrySdk::init, setCurrentHub, getRuntimeContextManager
  • CoroutineAspect refactor — Use SentrySdk::startContext() before coroutine starts and SentrySdk::endContext() on defer, replacing the old SentrySdk::flush() pattern
  • Remove all SentrySdk::flush() calls from:
    • Metrics/Listener/OnBeforeHandle
    • Metrics/Listener/OnCoroutineServerStart
    • Metrics/Listener/OnMetricFactoryReady
    • Metrics/Listener/OnWorkerStart
    • Metrics/Listener/PoolWatcher
    • Metrics/Listener/QueueWatcher
    • Tracing/Aspect/CoroutineAspect
    • Tracing/Listener/EventHandleListener
  • Unregister SentrySdkAspect from ConfigProvider

Test Plan

  • Verify Sentry events are still captured and sent correctly
  • Verify coroutine context isolation works as expected
  • Run existing sentry test suite

Risks

  • The startContext() / endContext() APIs must be available in the target Sentry SDK version. Verify compatibility before merging.

- Remove SentrySdkAspect (AOP interception for SentrySdk init/hub/context)
- Use SentrySdk::startContext()/endContext() in CoroutineAspect
- Remove all manual SentrySdk::flush() calls from Metrics listeners
  (OnBeforeHandle, OnCoroutineServerStart, OnMetricFactoryReady,
   OnWorkerStart, PoolWatcher, QueueWatcher)
- Remove SentrySdk::flush() from Tracing CoroutineAspect and EventHandleListener
- Unregister SentrySdkAspect from ConfigProvider
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • main

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4d4e35d3-568b-4943-87fb-c111cfce977b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/sentry-context-management

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.

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