-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathRedisWatcher.php
89 lines (76 loc) · 2.27 KB
/
RedisWatcher.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
<?php
namespace Laravel\Telescope\Watchers;
use Illuminate\Redis\Events\CommandExecuted;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
class RedisWatcher extends Watcher
{
/**
* Register the watcher.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function register($app)
{
if (! $app->bound('redis')) {
return;
}
$app['events']->listen(CommandExecuted::class, [$this, 'recordCommand']);
foreach ((array) $app['redis']->connections() as $connection) {
$connection->setEventDispatcher($app['events']);
}
$app['redis']->enableEvents();
}
/**
* Record a Redis command was executed.
*
* @param \Illuminate\Redis\Events\CommandExecuted $event
* @return void
*/
public function recordCommand(CommandExecuted $event)
{
if (! Telescope::isRecording() || $this->shouldIgnore($event)) {
return;
}
Telescope::recordRedis(IncomingEntry::make([
'connection' => $event->connectionName,
'command' => $this->formatCommand($event->command, $event->parameters),
'time' => number_format($event->time, 2, '.', ''),
]));
}
/**
* Format the given Redis command.
*
* @param string $command
* @param array $parameters
* @return string
*/
private function formatCommand($command, $parameters)
{
$parameters = collect($parameters)->map(function ($parameter) {
if (is_array($parameter)) {
return collect($parameter)->map(function ($value, $key) {
if (is_array($value)) {
return json_encode($value);
}
return is_int($key) ? $value : "{$key} {$value}";
})->implode(' ');
}
return $parameter;
})->implode(' ');
return "{$command} {$parameters}";
}
/**
* Determine if the event should be ignored.
*
* @param mixed $event
* @return bool
*/
private function shouldIgnore($event)
{
return in_array($event->command, [
'pipeline', 'transaction',
]);
}
}