|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SpeedPuzzling\Web\Services; |
| 6 | + |
| 7 | +use Sentry\State\HubInterface; |
| 8 | +use Symfony\Component\EventDispatcher\Attribute\AsEventListener; |
| 9 | +use Symfony\Component\HttpKernel\Event\ResponseEvent; |
| 10 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 11 | + |
| 12 | +/** |
| 13 | + * Enhances Sentry transaction names for Live Component requests. |
| 14 | + * |
| 15 | + * Uses RESPONSE event to ensure the component name and action are fully resolved |
| 16 | + * after the controller has executed. |
| 17 | + */ |
| 18 | +#[AsEventListener(event: KernelEvents::RESPONSE, priority: 10)] |
| 19 | +readonly final class SentryLiveComponentTransactionListener |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private HubInterface $hub, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function __invoke(ResponseEvent $event): void |
| 27 | + { |
| 28 | + if ($event->isMainRequest() === false) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + $request = $event->getRequest(); |
| 33 | + |
| 34 | + if ($request->attributes->has('_live_component') === false) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $transaction = $this->hub->getTransaction(); |
| 39 | + |
| 40 | + if ($transaction === null) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $componentName = $request->attributes->get('_component_name') |
| 45 | + ?? $request->attributes->get('_live_component'); |
| 46 | + |
| 47 | + if (!is_string($componentName) || $componentName === '') { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $liveAction = $request->attributes->get('_live_action'); |
| 52 | + $action = is_string($liveAction) && $liveAction !== '' ? $liveAction : 'render'; |
| 53 | + |
| 54 | + $transaction->setName( |
| 55 | + sprintf('LiveComponent::%s::%s', $componentName, $action) |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
0 commit comments