Skip to content

Commit a3da7af

Browse files
committed
Live components - properly set sentry transaction name
1 parent a6d0ba0 commit a3da7af

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

config/packages/sentry.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
NotFoundHttpException::class,
2727
],
2828
'traces_sampler' => 'sentry.traces_sampler',
29-
'before_send_transaction' => 'sentry.before_send_transaction',
3029
'profiles_sample_rate' => 1.0, // Profile all traced requests (sampling controlled by traces_sampler)
3130
'ignore_transactions' => [
3231
// Symfony profiler/debug toolbar routes

config/services.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,4 @@
148148

149149
$services->set('sentry.traces_sampler', \Closure::class)
150150
->factory([service(SentryTracesSampler::class), '__invoke']);
151-
152-
$services->set('sentry.before_send_transaction', \Closure::class)
153-
->factory([service(SentryTransactionNameEnhancer::class), '__invoke']);
154151
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

src/Services/SentryTransactionNameEnhancer.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)