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.


Summary by cubic

Fixes Sentry transaction profiling so overlapping requests in a Swoole worker no longer contaminate each other's profiles, adds continuous worker profiling, and changes event delivery to skip immediately when the transport pool is saturated.

  • Transaction profiles collect only the owning request's work, including attached child coroutines, and mark unrelated gaps as idle.
  • Repeated frames share storage and long profiles stop accumulating samples past the SDK's 30-second boundary, keeping what the SDK needs to report the rejection.
  • Continuous profiling uses the SDK lifecycle (trace or manual) to record the whole worker; transaction profiling takes precedence when configured.
  • The HTTP transport borrows without waiting, so events are skipped when every transport is busy or the pool is closed; the Sentry wait_timeout option is removed and tryBorrow() was added to the pool contract.
  • Profiler start/stop failures log and leave the transaction sampled, and discarded transactions release their profilers.
  • Requires sentry/sentry ^4.38.0; Excimer is optional but enabled in CI for real stack samples.

[!NOTE]

Add coroutine-isolated Sentry transaction profiling and continuous worker profiling

  • Adds CoroutineProfiler backed by Excimer: samples are recorded only when the flush callback runs in the captured runtime context or coroutine, so a profile includes only the owning transaction's work. ProfileSamples records idle markers between samples and trims profiles over 30 seconds to adjacent samples.
  • Adds continuous profiling with trace or manual lifecycle via SENTRY_PROFILE_SESSION_SAMPLE_RATE and SENTRY_PROFILE_LIFECYCLE in sentry.php. Hub starts the continuous profiler for root spans and attaches profiler metadata; Middleware.php stops continuous and transaction profilers when a transaction is discarded.
  • Adds ObjectPool::tryBorrow() and switches HttpPoolTransport.send to it, so events are skipped immediately when all transports are busy instead of waiting on a pool timeout. Removes the wait_timeout pool option; supported options are now only max_objects and max_lifetime.
  • Profiler factory and startup failures in Hub no longer propagate: the profiler is detached, cancellations rethrow, other failures are logged.
  • Behavioral Change: pool config with wait_timeout is rejected as unsupported (SentryServiceProvider.sentryPoolOptions), busy-pool sends return a skipped result instead of waiting, and sentry/sentry minimum is raised to ^4.38.0 with ext-excimer suggested.

Macroscope summarized 16c4ddf.

Summary by CodeRabbit

  • New Features
    • Added transaction-based and continuous profiling for supported PHP workers, with configurable sampling and lifecycle options.
    • The application information command now displays continuous profiling settings.
  • Bug Fixes
    • Profiling startup failures no longer interrupt sampled transactions. Profilers are also stopped when transactions are discarded.
  • Documentation
    • Added guidance on enabling profiling, sampling behavior, coroutine coverage, and lifecycle controls. Excimer is required for profiling.