7
7
use Inspector \Inspector ;
8
8
use Inspector \Models \Transaction ;
9
9
use Psr \Container \ContainerInterface ;
10
+ use Symfony \Component \Console \Event \ConsoleCommandEvent ;
10
11
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
11
12
use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
12
13
use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
14
+ use Symfony \Component \HttpKernel \Event \RequestEvent ;
15
+ use Symfony \Component \HttpKernel \Event \ResponseEvent ;
13
16
use Symfony \Component \HttpKernel \KernelEvents ;
14
17
use Symfony \Component \Console \ConsoleEvents ;
15
18
use Symfony \Component \Console \Event \ConsoleErrorEvent ;
@@ -37,8 +40,10 @@ public function __construct(ContainerInterface $container)
37
40
public static function getSubscribedEvents ()
38
41
{
39
42
$ listeners = [
40
- //KernelEvents::REQUEST => ['onKernelRequest', 256],
43
+ KernelEvents::REQUEST => ['onKernelRequest ' , 256 ],
44
+ KernelEvents::RESPONSE => ['onKernelResponse ' ],
41
45
KernelEvents::EXCEPTION => ['onKernelException ' , 128 ],
46
+ ConsoleEvents::COMMAND => ['onConsoleStart ' ],
42
47
];
43
48
44
49
// Added ConsoleEvents in Symfony 2.3
@@ -54,6 +59,44 @@ public static function getSubscribedEvents()
54
59
return $ listeners ;
55
60
}
56
61
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
+
57
100
/**
58
101
* Handle an http kernel exception.
59
102
*
@@ -69,11 +112,15 @@ public function onKernelException($event)
69
112
// where the ExceptionEvent exists and is used but doesn't implement
70
113
// the `getThrowable` method, which was introduced in Symfony 4.4
71
114
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 ' );
73
117
$ this ->notifyUnexpectedError ($ event ->getThrowable ());
118
+
74
119
} elseif ($ event instanceof GetResponseForExceptionEvent) {
75
- $ this ->startTransaction (get_class ($ event ->getException ()));
120
+
121
+ $ this ->startTransaction (get_class ($ event ->getException ()))->setResult ('error ' );
76
122
$ this ->notifyUnexpectedError ($ event ->getException ());
123
+
77
124
} else {
78
125
throw new \InvalidArgumentException ('Invalid exception event. ' );
79
126
}
@@ -89,7 +136,7 @@ public function onKernelException($event)
89
136
*/
90
137
public function onConsoleException (ConsoleExceptionEvent $ event )
91
138
{
92
- $ this ->startTransaction ($ event ->getCommand ()->getName ());
139
+ $ this ->startTransaction ($ event ->getCommand ()->getName ())-> setResult ( ' error ' ) ;
93
140
94
141
$ this ->notifyUnexpectedError ($ event ->getException ());
95
142
}
@@ -103,7 +150,7 @@ public function onConsoleException(ConsoleExceptionEvent $event)
103
150
*/
104
151
public function onConsoleError (ConsoleErrorEvent $ event )
105
152
{
106
- $ this ->startTransaction ($ event ->getCommand ()->getName ());
153
+ $ this ->startTransaction ($ event ->getCommand ()->getName ())-> setResult ( ' error ' ) ;
107
154
108
155
$ this ->notifyUnexpectedError ($ event ->getError ());
109
156
}
0 commit comments