-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger][Profiler] Trace middleware execution #27321
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
Merged
Merged
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
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Messenger/Middleware/Enhancers/TraceableMiddleware.php
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,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Middleware\Enhancers; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\EnvelopeAwareInterface; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* Collects some data about a middleware. | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class TraceableMiddleware implements MiddlewareInterface, EnvelopeAwareInterface | ||
{ | ||
private $inner; | ||
private $stopwatch; | ||
private $busName; | ||
private $eventCategory; | ||
|
||
public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, string $busName = null, string $eventCategory = 'messenger.middleware') | ||
{ | ||
$this->inner = $inner; | ||
$this->stopwatch = $stopwatch; | ||
$this->busName = $busName; | ||
$this->eventCategory = $eventCategory; | ||
} | ||
|
||
/** | ||
* @param Envelope $envelope | ||
*/ | ||
public function handle($envelope, callable $next) | ||
{ | ||
$class = \get_class($this->inner); | ||
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class; | ||
|
||
if ($this->busName) { | ||
$eventName .= " (bus: {$this->busName})"; | ||
} | ||
|
||
$this->stopwatch->start($eventName, $this->eventCategory); | ||
|
||
try { | ||
$result = $this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) { | ||
$this->stopwatch->stop($eventName); | ||
$result = $next($message); | ||
$this->stopwatch->start($eventName, $this->eventCategory); | ||
|
||
return $result; | ||
}); | ||
} finally { | ||
if ($this->stopwatch->isStarted($eventName)) { | ||
$this->stopwatch->stop($eventName); | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
src/Symfony/Component/Messenger/Tests/Middleware/Enhancers/TraceableMiddlewareTest.php
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,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Middleware\Enhancers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\Enhancers\TraceableMiddleware; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class TraceableMiddlewareTest extends TestCase | ||
{ | ||
public function testHandle() | ||
{ | ||
$busId = 'command_bus'; | ||
$envelope = Envelope::wrap($message = new DummyMessage('Hello')); | ||
|
||
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); | ||
$middleware->expects($this->once()) | ||
->method('handle') | ||
->with($message, $this->anything()) | ||
->will($this->returnCallback(function ($message, callable $next) { | ||
return $next($message); | ||
})) | ||
; | ||
|
||
$next = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
$next | ||
->expects($this->once()) | ||
->method('__invoke') | ||
->with($message) | ||
->willReturn($expectedReturnedValue = 'Hello') | ||
; | ||
|
||
$stopwatch = $this->createMock(Stopwatch::class); | ||
$stopwatch->expects($this->once())->method('isStarted')->willReturn(true); | ||
$stopwatch->expects($this->exactly(2)) | ||
->method('start') | ||
->with($this->matches('%sMiddlewareInterface%s (bus: command_bus)'), 'messenger.middleware') | ||
; | ||
$stopwatch->expects($this->exactly(2)) | ||
->method('stop') | ||
->with($this->matches('%sMiddlewareInterface%s (bus: command_bus)')) | ||
; | ||
|
||
$traced = new TraceableMiddleware($middleware, $stopwatch, $busId); | ||
|
||
$this->assertSame($expectedReturnedValue, $traced->handle($envelope, $next)); | ||
} | ||
|
||
/** | ||
* @expectedException \RuntimeException | ||
* @expectedExceptionMessage Foo exception from next callable | ||
*/ | ||
public function testHandleWithException() | ||
{ | ||
$busId = 'command_bus'; | ||
$envelope = Envelope::wrap($message = new DummyMessage('Hello')); | ||
|
||
$middleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); | ||
$middleware->expects($this->once()) | ||
->method('handle') | ||
->with($message, $this->anything()) | ||
->will($this->returnCallback(function ($message, callable $next) { | ||
return $next($message); | ||
})) | ||
; | ||
|
||
$next = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
$next | ||
->expects($this->once()) | ||
->method('__invoke') | ||
->willThrowException(new \RuntimeException('Foo exception from next callable')) | ||
; | ||
|
||
$stopwatch = $this->createMock(Stopwatch::class); | ||
$stopwatch->expects($this->once())->method('isStarted')->willReturn(true); | ||
// Start is only expected to be called once, as an exception is thrown by the next callable: | ||
$stopwatch->expects($this->exactly(1)) | ||
->method('start') | ||
->with($this->matches('%sMiddlewareInterface%s (bus: command_bus)'), 'messenger.middleware') | ||
; | ||
$stopwatch->expects($this->exactly(2)) | ||
->method('stop') | ||
->with($this->matches('%sMiddlewareInterface%s (bus: command_bus)')) | ||
; | ||
|
||
$traced = new TraceableMiddleware($middleware, $stopwatch, $busId); | ||
$traced->handle($envelope, $next); | ||
} | ||
} |
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
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.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.