-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4879 from coopcycle/logging-routes
Log routes in messenger logs
- Loading branch information
Showing
10 changed files
with
197 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,26 @@ | ||
<?php | ||
|
||
namespace AppBundle\Log; | ||
|
||
use AppBundle\Messenger\Stamp\RouteStamp; | ||
use Monolog\Attribute\AsMonologProcessor; | ||
|
||
#[AsMonologProcessor] | ||
class MessengerRouteProcessor extends MessengerStampProcessor | ||
{ | ||
public function __invoke(array $record): array | ||
{ | ||
$stamp = $this->getStamp(); | ||
|
||
if ($stamp instanceof RouteStamp) { | ||
$record['extra']['requests'] = [ | ||
[ | ||
'controller' => $stamp->getController(), | ||
'route' => $stamp->getRoute() | ||
] | ||
]; | ||
} | ||
|
||
return $record; | ||
} | ||
} |
This file contains 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,20 @@ | ||
<?php | ||
|
||
namespace AppBundle\Log; | ||
|
||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
abstract class MessengerStampProcessor | ||
{ | ||
private ?StampInterface $stamp = null; | ||
|
||
public function getStamp(): ?StampInterface | ||
{ | ||
return $this->stamp; | ||
} | ||
|
||
public function setStamp(?StampInterface $stamp): void | ||
{ | ||
$this->stamp = $stamp; | ||
} | ||
} |
This file contains 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 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,43 @@ | ||
<?php | ||
|
||
namespace AppBundle\Messenger; | ||
|
||
use AppBundle\Log\MessengerRouteProcessor; | ||
use AppBundle\Messenger\Stamp\RouteStamp; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
class RouteMiddleware extends StampMiddleware | ||
{ | ||
public function __construct( | ||
private readonly RequestStack $requestStack, | ||
MessengerRouteProcessor $stampProcessor, | ||
) | ||
{ | ||
parent::__construct($stampProcessor); | ||
} | ||
|
||
protected function getStampFqcn(): string | ||
{ | ||
return RouteStamp::class; | ||
} | ||
|
||
protected function createStamp(): ?StampInterface | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
if ($request) { | ||
$route = $request->attributes->get('_route'); | ||
$controller = $request->attributes->get('_controller'); | ||
|
||
if (null !== $route && null !== $controller) { | ||
return new RouteStamp($route, $controller); | ||
} else { | ||
return null; | ||
} | ||
|
||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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 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,24 @@ | ||
<?php | ||
|
||
namespace AppBundle\Messenger\Stamp; | ||
|
||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
class RouteStamp implements StampInterface | ||
{ | ||
public function __construct( | ||
private readonly string $route, | ||
private readonly string $controller, | ||
) { | ||
} | ||
|
||
public function getRoute(): string | ||
{ | ||
return $this->route; | ||
} | ||
|
||
public function getController(): string | ||
{ | ||
return $this->controller; | ||
} | ||
} |
This file contains 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,48 @@ | ||
<?php | ||
|
||
namespace AppBundle\Messenger; | ||
|
||
use AppBundle\Log\MessengerStampProcessor; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
abstract class StampMiddleware implements MiddlewareInterface | ||
{ | ||
private ?StampInterface $currentStamp = null; | ||
|
||
public function __construct( | ||
private readonly MessengerStampProcessor $messengerStampProcessor | ||
) | ||
{ | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
if ($stamp = $envelope->last($this->getStampFqcn())) { | ||
$this->messengerStampProcessor->setStamp($stamp); | ||
$this->currentStamp = $stamp; | ||
|
||
try { | ||
return $stack->next()->handle($envelope, $stack); | ||
} finally { | ||
$this->messengerStampProcessor->setStamp(null); | ||
$this->currentStamp = null; | ||
} | ||
} | ||
|
||
if (!$envelope->last(ConsumedByWorkerStamp::class) && $stamp = $this->createStamp()) { | ||
$envelope = $envelope->with($stamp); | ||
} elseif (!$envelope->last(ConsumedByWorkerStamp::class) && $this->currentStamp !== null) { | ||
$envelope = $envelope->with($this->currentStamp); | ||
} | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
abstract protected function getStampFqcn(): string; | ||
|
||
abstract protected function createStamp(): ?StampInterface; | ||
} |