Skip to content

Commit dcb7e32

Browse files
committed
start transaction
1 parent b0cc29a commit dcb7e32

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

src/Listeners/InspectorListener.php

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use Inspector\Inspector;
88
use Inspector\Models\Transaction;
99
use Psr\Container\ContainerInterface;
10+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1011
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1112
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1213
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
14+
use Symfony\Component\HttpKernel\Event\RequestEvent;
15+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
1316
use Symfony\Component\HttpKernel\KernelEvents;
1417
use Symfony\Component\Console\ConsoleEvents;
1518
use Symfony\Component\Console\Event\ConsoleErrorEvent;
@@ -37,8 +40,10 @@ public function __construct(ContainerInterface $container)
3740
public static function getSubscribedEvents()
3841
{
3942
$listeners = [
40-
//KernelEvents::REQUEST => ['onKernelRequest', 256],
43+
KernelEvents::REQUEST => ['onKernelRequest', 256],
44+
KernelEvents::RESPONSE => ['onKernelResponse'],
4145
KernelEvents::EXCEPTION => ['onKernelException', 128],
46+
ConsoleEvents::COMMAND => ['onConsoleStart'],
4247
];
4348

4449
// Added ConsoleEvents in Symfony 2.3
@@ -54,6 +59,44 @@ public static function getSubscribedEvents()
5459
return $listeners;
5560
}
5661

62+
/**
63+
* Intercept an HTTP request.
64+
*
65+
* @param RequestEvent $event
66+
* @throws \Exception
67+
*/
68+
public function onKernelRequest($event)
69+
{
70+
$this->startTransaction(
71+
$event->getRequest()->getMethod() . ' ' . $event->getRequest()->getUri()
72+
);
73+
}
74+
75+
/**
76+
* Ending transaction.
77+
*
78+
* @param ResponseEvent $event
79+
*/
80+
public function onKernelResponse($event)
81+
{
82+
if (!$this->inspector->isRecording()) {
83+
return;
84+
}
85+
86+
$this->inspector->currentTransaction()->setResult($event->getResponse()->getStatusCode());
87+
}
88+
89+
/**
90+
* Intercept a command execution.
91+
*
92+
* @param ConsoleCommandEvent $event
93+
* @throws \Exception
94+
*/
95+
public function onConsoleStart($event)
96+
{
97+
$this->startTransaction($event->getCommand()->getName());
98+
}
99+
57100
/**
58101
* Handle an http kernel exception.
59102
*
@@ -69,11 +112,15 @@ public function onKernelException($event)
69112
// where the ExceptionEvent exists and is used but doesn't implement
70113
// the `getThrowable` method, which was introduced in Symfony 4.4
71114
if ($event instanceof ExceptionEvent && method_exists($event, 'getThrowable')) {
72-
$this->startTransaction(get_class($event->getThrowable()));
115+
116+
$this->startTransaction(get_class($event->getThrowable()))->setResult('error');
73117
$this->notifyUnexpectedError($event->getThrowable());
118+
74119
} elseif ($event instanceof GetResponseForExceptionEvent) {
75-
$this->startTransaction(get_class($event->getException()));
120+
121+
$this->startTransaction(get_class($event->getException()))->setResult('error');
76122
$this->notifyUnexpectedError($event->getException());
123+
77124
} else {
78125
throw new \InvalidArgumentException('Invalid exception event.');
79126
}
@@ -89,7 +136,7 @@ public function onKernelException($event)
89136
*/
90137
public function onConsoleException(ConsoleExceptionEvent $event)
91138
{
92-
$this->startTransaction($event->getCommand()->getName());
139+
$this->startTransaction($event->getCommand()->getName())->setResult('error');
93140

94141
$this->notifyUnexpectedError($event->getException());
95142
}
@@ -103,7 +150,7 @@ public function onConsoleException(ConsoleExceptionEvent $event)
103150
*/
104151
public function onConsoleError(ConsoleErrorEvent $event)
105152
{
106-
$this->startTransaction($event->getCommand()->getName());
153+
$this->startTransaction($event->getCommand()->getName())->setResult('error');
107154

108155
$this->notifyUnexpectedError($event->getError());
109156
}

0 commit comments

Comments
 (0)