-
-
Notifications
You must be signed in to change notification settings - Fork 28
fix(sentry): isolate sentry runtime context per request #1089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huangdijia
wants to merge
1
commit into
main
Choose a base branch
from
sentry-pr/r4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Tests\Sentry; | ||
|
|
||
| use FastRoute\Dispatcher; | ||
| use FriendsOfHyperf\Sentry\Feature; | ||
| use FriendsOfHyperf\Sentry\Listener\EventHandleListener as BaseEventHandleListener; | ||
| use FriendsOfHyperf\Sentry\Tracing\Listener\EventHandleListener as TracingEventHandleListener; | ||
| use FriendsOfHyperf\Sentry\Tracing\Tracer; | ||
| use Hyperf\Config\Config; | ||
| use Hyperf\Context\ApplicationContext; | ||
| use Hyperf\Contract\StdoutLoggerInterface; | ||
| use Hyperf\HttpServer\Event\RequestReceived as HttpRequestReceived; | ||
| use Hyperf\HttpServer\Router\Dispatched; | ||
| use Hyperf\HttpServer\Router\Handler; | ||
| use Hyperf\Rpc\Context as RpcContext; | ||
| use Mockery as m; | ||
| use Psr\Container\ContainerInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Message\UriInterface; | ||
| use Sentry\SentrySdk; | ||
| use Swoole\Coroutine; | ||
|
|
||
| beforeEach(function () { | ||
| // Tests run inside a Swoole coroutine (TestCase::runBare wraps each test in | ||
| // Swoole\Coroutine\run), so Hyperf\Coroutine\defer() registers a coroutine-exit callback. | ||
| // Make sure no active runtime context leaks from a previous test. | ||
| SentrySdk::endContext(); | ||
|
|
||
| $this->container = m::mock(ContainerInterface::class); | ||
| $this->container->shouldReceive('has')->with(RpcContext::class)->andReturn(false); | ||
|
|
||
| // The startTransaction() and Carrier helpers resolve from the ApplicationContext | ||
| // container, so make Tracer resolvable there deterministically. | ||
| $this->container->shouldReceive('get')->with(Tracer::class)->andReturn(new Tracer()); | ||
| ApplicationContext::setContainer($this->container); | ||
|
|
||
| $this->makeRequestReceived = function (): HttpRequestReceived { | ||
| $handler = new Handler('App\Controller\IndexController::index', '/test'); | ||
| $dispatched = new Dispatched([Dispatcher::FOUND, $handler, []], 'http'); | ||
|
|
||
| $uri = m::mock(UriInterface::class); | ||
| $uri->shouldReceive('getPath')->andReturn('/test'); | ||
| $uri->shouldReceive('getScheme')->andReturn('http'); | ||
|
|
||
| $request = m::mock(ServerRequestInterface::class); | ||
| $request->shouldReceive('getAttribute')->with(Dispatched::class)->andReturn($dispatched); | ||
| $request->shouldReceive('getUri')->andReturn($uri); | ||
| $request->shouldReceive('getMethod')->andReturn('GET'); | ||
| $request->shouldReceive('getHeaders')->andReturn([]); | ||
| $request->shouldReceive('hasHeader')->andReturn(false); | ||
| $request->shouldReceive('getHeaderLine')->andReturn(''); | ||
|
|
||
| $response = m::mock(ResponseInterface::class); | ||
|
|
||
| return new HttpRequestReceived($request, $response); | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| SentrySdk::endContext(); | ||
| m::close(); | ||
| }); | ||
|
|
||
| test('tracing listener starts an isolated runtime context per request and restores on endContext', function () { | ||
| $config = new Config([ | ||
| 'sentry' => [ | ||
| 'enable' => ['request' => true], | ||
| 'tracing' => ['request' => true, 'missing_routes' => true], | ||
| ], | ||
| ]); | ||
| $feature = new Feature($config); | ||
| $listener = new TracingEventHandleListener($this->container, $config, $feature); | ||
|
|
||
| $before = SentrySdk::getCurrentHub(); | ||
| $listener->process(($this->makeRequestReceived)()); | ||
| $after = SentrySdk::getCurrentHub(); | ||
|
|
||
| // The request got its own hub instead of the shared global one. | ||
| expect($after)->not->toBe($before); | ||
|
|
||
| // Ending the context manually restores the global hub instance. | ||
| SentrySdk::endContext(); | ||
| expect(SentrySdk::getCurrentHub())->toBe($before); | ||
| }); | ||
|
|
||
| test('the deferred endContext restores the global hub when the request coroutine exits', function () { | ||
| $config = new Config([ | ||
| 'sentry' => [ | ||
| 'enable' => ['request' => true], | ||
| 'tracing' => ['request' => true, 'missing_routes' => true], | ||
| ], | ||
| ]); | ||
| $feature = new Feature($config); | ||
| $listener = new TracingEventHandleListener($this->container, $config, $feature); | ||
| $event = ($this->makeRequestReceived)(); | ||
|
|
||
| $before = SentrySdk::getCurrentHub(); | ||
| $innerHub = null; | ||
|
|
||
| $cid = Coroutine::create(function () use ($listener, $event, &$innerHub): void { | ||
| $listener->process($event); | ||
| $innerHub = SentrySdk::getCurrentHub(); | ||
| }); | ||
| Coroutine::join([$cid]); | ||
|
|
||
| // While the request coroutine is alive it has an isolated hub... | ||
| expect($innerHub)->not->toBe($before); | ||
| // ...and once it exits, the defer ends the context automatically. | ||
| expect(SentrySdk::getCurrentHub())->toBe($before); | ||
| }); | ||
|
|
||
| test('base listener starts an isolated runtime context when tracing is disabled', function () { | ||
| $config = new Config([ | ||
| 'sentry' => [ | ||
| 'enable' => ['request' => true], | ||
| 'tracing' => ['request' => false], | ||
| ], | ||
| ]); | ||
| $feature = new Feature($config); | ||
| $listener = new BaseEventHandleListener($this->container, $feature, $config, m::mock(StdoutLoggerInterface::class)); | ||
|
|
||
| $before = SentrySdk::getCurrentHub(); | ||
| $listener->process(($this->makeRequestReceived)()); | ||
|
|
||
| expect(SentrySdk::getCurrentHub())->not->toBe($before); | ||
|
|
||
| SentrySdk::endContext(); | ||
| expect(SentrySdk::getCurrentHub())->toBe($before); | ||
| }); | ||
|
|
||
| test('no runtime context is started when request features are disabled', function () { | ||
| $config = new Config([ | ||
| 'sentry' => [ | ||
| 'enable' => ['request' => false], | ||
| 'tracing' => ['request' => false], | ||
| ], | ||
| ]); | ||
| $feature = new Feature($config); | ||
| $tracingListener = new TracingEventHandleListener($this->container, $config, $feature); | ||
| $baseListener = new BaseEventHandleListener($this->container, $feature, $config, m::mock(StdoutLoggerInterface::class)); | ||
|
|
||
| $before = SentrySdk::getCurrentHub(); | ||
| $tracingListener->process(($this->makeRequestReceived)()); | ||
| $baseListener->process(($this->makeRequestReceived)()); | ||
|
|
||
| expect(SentrySdk::getCurrentHub())->toBe($before); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When both request handling and request tracing are enabled (the defaults),
ConfigProviderinvokes the tracing listener first and this base listener second. Although this listener'sstartContext()is a no-op for the already-active context, it still registers another LIFOendContext()defer; that defer runs before the tracing listener's transaction-finishing defer, removing and flushing the request context before the transaction is finished. Consequently, transaction telemetry is produced after the request's final flush and may be lost by transports that require flushing. Only the listener that creates the context should schedule its teardown, or context ownership should be centralized.Useful? React with 👍 / 👎.