-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathEventHandler.php
343 lines (306 loc) · 9.6 KB
/
EventHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
namespace Sentry\Laravel;
use Exception;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\QueueManager;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Routing\Route;
use RuntimeException;
use Sentry\State\Scope;
use Sentry\Breadcrumb;
class EventHandler
{
/**
* Map event handlers to events.
*
* @var array
*/
protected static $eventHandlerMap = [
'router.matched' => 'routerMatched', // Until Laravel 5.1
'Illuminate\Routing\Events\RouteMatched' => 'routeMatched', // Since Laravel 5.2
'illuminate.query' => 'query', // Until Laravel 5.1
'Illuminate\Database\Events\QueryExecuted' => 'queryExecuted', // Since Laravel 5.2
'illuminate.log' => 'log', // Until Laravel 5.3
'Illuminate\Log\Events\MessageLogged' => 'messageLogged', // Since Laravel 5.4
'Illuminate\Console\Events\CommandStarting' => 'commandStarting', // Since Laravel 5.5
'Illuminate\Console\Events\CommandFinished' => 'commandFinished', // Since Laravel 5.5
];
/**
* Map authentication event handlers to events.
*
* @var array
*/
protected static $authEventHandlerMap = [
'Illuminate\Auth\Events\Authenticated' => 'authenticated', // Since Laravel 5.3
];
/**
* Map queue event handlers to events.
*
* @var array
*/
protected static $queueEventHandlerMap = [
'Illuminate\Queue\Events\JobProcessing' => 'queueJobProcessing', // Since Laravel 5.2
];
/**
* The Laravel event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
private $events;
/**
* Indicates if we should we add query bindings to the breadcrumbs.
*
* @var bool
*/
private $recordSqlBindings;
/**
* EventHandler constructor.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param array $config
*/
public function __construct(Dispatcher $events, array $config)
{
$this->events = $events;
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
}
/**
* Attach all event handlers.
*/
public function subscribe()
{
foreach (static::$eventHandlerMap as $eventName => $handler) {
$this->events->listen($eventName, [$this, $handler]);
}
}
/**
* Attach all authentication event handlers.
*/
public function subscribeAuthEvents()
{
foreach (static::$authEventHandlerMap as $eventName => $handler) {
$this->events->listen($eventName, [$this, $handler]);
}
}
/**
* Attach all queue event handlers.
*
* @param \Illuminate\Queue\QueueManager $queue
*/
public function subscribeQueueEvents(QueueManager $queue)
{
$queue->looping(function () {
// Flush any and all events that were possibly generated by queue jobs
Integration::flushEvents();
// We have added a scope when a job starts processing
Integration::getCurrentHub()->popScope();
});
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
$this->events->listen($eventName, [$this, $handler]);
}
}
/**
* Pass through the event and capture any errors.
*
* @param string $method
* @param array $arguments
*/
public function __call($method, $arguments)
{
if (!method_exists($this, $method . 'handler')) {
throw new RuntimeException('Missing event handler:' . $method . 'handler');
}
try {
call_user_func_array([$this, $method . 'handler'], $arguments);
} catch (Exception $exception) {
// Ignore
}
}
/**
* Until Laravel 5.1
*
* @param Route $route
*/
protected function routerMatchedHandler(Route $route)
{
if ($route->getName()) {
// someaction (route name/alias)
$routeName = $route->getName();
} elseif ($route->getActionName()) {
// SomeController@someAction (controller action)
$routeName = $route->getActionName();
}
if (empty($routeName) || $routeName === 'Closure') {
// /someaction // Fallback to the url
$routeName = $route->uri();
}
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_NAVIGATION,
'route',
$routeName
));
Integration::setTransaction($routeName);
}
/**
* Since Laravel 5.2
*
* @param \Illuminate\Routing\Events\RouteMatched $match
*/
protected function routeMatchedHandler(RouteMatched $match)
{
$this->routerMatchedHandler($match->route);
}
/**
* Until Laravel 5.1
*
* @param string $query
* @param array $bindings
* @param int $time
* @param string $connectionName
*/
protected function queryHandler($query, $bindings, $time, $connectionName)
{
$data = ['connectionName' => $connectionName];
if ($this->recordSqlBindings) {
$data['bindings'] = $bindings;
}
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_USER,
'sql.query',
$query,
$data
));
}
/**
* Since Laravel 5.2
*
* @param \Illuminate\Database\Events\QueryExecuted $query
*/
protected function queryExecutedHandler(QueryExecuted $query)
{
$data = ['connectionName' => $query->connectionName];
if ($this->recordSqlBindings) {
$data['bindings'] = $query->bindings;
}
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_USER,
'sql.query',
$query->sql,
$data
));
}
/**
* Until Laravel 5.3
*
* @param string $level
* @param string $message
* @param array|null $context
*/
protected function logHandler($level, $message, $context)
{
Integration::addBreadcrumb(new Breadcrumb(
$level,
Breadcrumb::TYPE_USER,
'log.' . $level,
$message,
empty($context) ? [] : ['params' => $context]
));
}
/**
* Since Laravel 5.4
*
* @param \Illuminate\Log\Events\MessageLogged $logEntry
*/
protected function messageLoggedHandler(MessageLogged $logEntry)
{
Integration::addBreadcrumb(new Breadcrumb(
$logEntry->level,
Breadcrumb::TYPE_USER,
'log.' . $logEntry->level,
$logEntry->message,
empty($logEntry->context) ? [] : ['params' => $logEntry->context]
));
}
/**
* Since Laravel 5.3
*
* @param \Illuminate\Auth\Events\Authenticated $event
*/
protected function authenticatedHandler(Authenticated $event)
{
Integration::configureScope(function (Scope $scope) use ($event): void {
$scope->setUser([
'id' => $event->user->getAuthIdentifier(),
]);
});
}
/**
* Since Laravel 5.2
*
* @param \Illuminate\Queue\Events\JobProcessing $event
*/
protected function queueJobProcessingHandler(JobProcessing $event)
{
// When a job starts, we want to push a new scope
Integration::getCurrentHub()->pushScope();
$job = [
'job' => $event->job->getName(),
'queue' => $event->job->getQueue(),
'attempts' => $event->job->attempts(),
'connection' => $event->connectionName,
];
// Resolve name exists only from Laravel 5.3+
if (method_exists($event->job, 'resolveName')) {
$job['resolved'] = $event->job->resolveName();
}
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_USER,
'queue.job',
'Processing queue job',
$job
));
}
/**
* Since Laravel 5.5
*
* @param \Illuminate\Console\Events\CommandStarting $event
*/
protected function commandStartingHandler(CommandStarting $event)
{
if ($event->command) {
Integration::configureScope(function (Scope $scope) use ($event): void {
$scope->setTag('command', $event->command);
});
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_USER,
'artisan.command',
'Invoked Artisan command: ' . $event->command,
method_exists($event->input, '__toString') ? [
'input' => (string)$event->input,
] : []
));
}
}
/**
* Since Laravel 5.5
*
* @param \Illuminate\Console\Events\CommandFinished $event
*/
protected function commandFinishedHandler(CommandFinished $event)
{
Integration::configureScope(function (Scope $scope) use ($event): void {
$scope->setTag('command', '');
});
}
}