Skip to content

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

Merged
huangdijia merged 17 commits into
3.1from
refactor/sentry-context-management
Jun 16, 2026
Merged

refactor(sentry): replace manual flush with context lifecycle management#1070
huangdijia merged 17 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: fc2ed1bf-cc6c-4f3e-929d-3d0b5a32423b

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.

@huangdijia huangdijia marked this pull request as ready for review June 16, 2026 01:43
@huangdijia huangdijia merged commit 3f07e54 into 3.1 Jun 16, 2026
22 checks passed
@huangdijia huangdijia deleted the refactor/sentry-context-management branch June 16, 2026 01:43
@huangdijia huangdijia restored the refactor/sentry-context-management branch June 16, 2026 01:44
@huangdijia huangdijia deleted the refactor/sentry-context-management branch June 16, 2026 01:45

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e3ddd4bf6c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

{
if ($this->globalContext === null) {
// Lazy fallback keeps baseline behavior when users do not opt into explicit context lifecycle.
$this->globalContext = new RuntimeContext('global', $this->baseHub);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Start a runtime context for request entrypoints

When a request/RPC/command is handled without going through Hyperf\Coroutine\Coroutine::create, it now falls back to this single global RuntimeContext, so Tracing\Listener\EventHandleListener::handleRequestReceived() sets spans/scopes on a hub shared by concurrent requests. The removed SentrySdkAspect previously kept the runtime manager in Hyperf's coroutine Context, but the only new startContext() calls are in the coroutine aspects, leaving normal server lifecycle events unisolated and allowing breadcrumbs/spans/metrics to bleed across requests until some later flush.

Useful? React with 👍 / 👎.

huangdijia added a commit that referenced this pull request Jun 16, 2026
…ent (#1070)

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

- 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

* chore(sentry): bump sentry/sentry minimum version to ^4.28.0

* chore(sentry): sync sentry/sentry version in root composer.json

* ♻️ refactor(sentry): remove unused SingletonAspect and clean up comments

* 🐛 fix(sentry): move startContext into coroutine after context restore

* ✨ feat(sentry): add coroutine-safe RuntimeContextManager via class_map

* 📝 docs(sentry): add comment clarifying ContextArrayObject usage in constructor

* 🐛 fix(sentry): ensure baseHub has a valid client in RuntimeContextManager

* ♻️ refactor(sentry): move context lifecycle into tracing aspect

* 📝 docs(sentry): correct type hints and comment for ContextArrayObject

* 🐛 fix(sentry): run metrics collection within a coroutine context

* 🐛 fix(sentry): disable coordinator tracing span by default

* ♻️ refactor(sentry): remove coordinator tracing aspect

* Revert "🐛 fix(sentry): run metrics collection within a coroutine context"

This reverts commit fd289ca.

* 🐛 fix(sentry): isolate sentry context per coroutine

* 🐛 fix(sentry): treat sentry metrics/transport as coroutine backtrace break points

* 📦 build(sentry): require hyperf/coordinator

---------

Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
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