Isolate Sentry transaction profiles and support continuous worker profiling

Merged
BI
1d ago
+821-92

A Swoole worker can switch requests while a Sentry transaction is being profiled. If request A waits for I/O and request B runs, the SDK’s default profiler can put B’s stack in A’s profile.

This change gives Sentry a profiler that collects samples belonging to the request that started the transaction. Child coroutines that share its Sentry context are included. A gap between samples is marked as idle while unrelated work runs. Repeated frames share storage, and long jobs stop accumulating samples once the SDK would discard their profile. An application can still supply its own profiler.

Continuous profiling

For a view of the whole worker, applications can use the SDK’s continuous profiler. With SENTRY_PROFILE_LIFECYCLE=trace, sampled transactions share a worker recording until the last one ends. The default manual lifecycle lets the application start and stop the recording. The session sample rate and lifecycle are available in the published configuration and through artisan about.

Both modes are off by default. Transaction profiling takes precedence when its rate or sampler is configured. The integration requires Sentry PHP SDK 4.38 or later in the 4.x series. Excimer is optional.

Event delivery

If every HTTP transport is busy, Sentry now skips the event immediately. Previously the request waited for a transport, and cancellation during that wait could be caught by the SDK as a failed send. The pool still reuses transports and bounds concurrent sends with sentry.pool.max_objects. Remove wait_timeout from published Sentry pool configuration. Other object pools can keep using borrow() with their configured timeout, or use tryBorrow() to check for capacity immediately.

Verification

With the SDK profiler, ProfilingCoroutineIsolationTest::testTransactionProfileExcludesWorkFromAnotherRequest captured request B’s stack while request A was waiting. With this change it captures A’s work and marks the gap as idle. The profiling changes were exercised with composer test:parallel -- --processes=4 with Excimer loaded and with the Sentry suite both with and without the extension. Local formatting and static analysis passed with composer lint:fix and composer analyse. The transport regression covers SDK capture while the only transport is checked out. The cutoff regression passes retained samples through the SDK formatter and checks why it drops the profile.

The released SDK has a separate issue when a new recording starts while the previous recording’s final chunks are being sent. The fix belongs in the SDK and works with this integration. Upstream fix

[!NOTE]

Add transaction and continuous profiling to Sentry with non-blocking transport pool

  • Adds Excimer-based transaction profiling via CoroutineProfiler and ProfileSamples, isolating each request's samples by coroutine/request context and inserting idle markers for foreign work.
  • Supports continuous (worker) profiling with profile_session_sample_rate and profile_lifecycle config, driven from SENTRY_PROFILE_SESSION_SAMPLE_RATE and SENTRY_PROFILE_LIFECYCLE in sentry.php. Hub.startTransaction and Hub.bindClient now hook the continuous profiler.
  • Adds tryBorrow() to ObjectPool (ObjectPool.php); HttpPoolTransport.send uses it and skips events immediately when every transport is busy, instead of waiting.
  • Discarding a transaction via Middleware.php now stops both profilers; cancellation exceptions propagate, other stop errors are logged.
  • Raises sentry/sentry to ^4.38.0 and suggests ext-excimer in composer.json.
  • Risk: pool wait_timeout config is removed and now rejected as unsupported; events are dropped when the transport pool is full — check poolOptions in HttpTransportPoolTest.php and the delivery notes in sentry.md.
📊 Macroscope summarized 16c4ddf. 9 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted

🗂️ Filtered Issues

Summary by CodeRabbit

  • New Features
    • Added transaction profiling and continuous worker profiling, with configurable sampling rates and lifecycle controls. Profiling is disabled by default and requires the Excimer PHP extension.
    • Profiles now better isolate work by request and coroutine, and limit retained samples for long-running profiles.
  • Bug Fixes
    • Improved profiler cleanup when transactions are discarded or profiling fails.
  • Documentation
    • Added guidance on enabling profiling, sampling behavior, and its limits for concurrent requests and coroutine work.