-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make prefix/suffix interceptors for phpredis logging customizable
By inversing dependency for prefix/suffix closures and unifying it into something that looks more like an middleware, we are making interceptor customizable. One use case I can think of is building interceptor for APM agents - this way, we don't have to integrate such support inside this bundle, but 3rd party package can be created to facilitate such use cases.
- Loading branch information
1 parent
54f1204
commit 7328961
Showing
7 changed files
with
137 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Snc\RedisBundle\Logger; | ||
|
||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
use function array_values; | ||
use function implode; | ||
use function is_numeric; | ||
use function is_scalar; | ||
use function microtime; | ||
use function strtoupper; | ||
use function strval; | ||
use function trim; | ||
|
||
class RedisCallInterceptor | ||
{ | ||
private RedisLogger $logger; | ||
private ?Stopwatch $stopwatch; | ||
|
||
public function __construct(RedisLogger $logger, ?Stopwatch $stopwatch = null) | ||
{ | ||
$this->logger = $logger; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
/** | ||
* @param mixed[] $args | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke( | ||
object $instance, | ||
string $method, | ||
array $args, | ||
?string $connection | ||
) { | ||
$args = array_values($args); | ||
$command = $this->getCommandString($method, $args); | ||
$time = microtime(true); | ||
|
||
if ($this->stopwatch) { | ||
$event = $this->stopwatch->start($command, 'redis'); | ||
} | ||
|
||
$return = $instance->$method(...$args); | ||
|
||
$this->logger->logCommand($command, microtime(true) - $time, $connection); | ||
|
||
if (isset($event)) { | ||
$event->stop(); | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the given command including arguments. | ||
* | ||
* @param mixed[] $arguments List of command arguments | ||
*/ | ||
private function getCommandString(string $command, array $arguments): string | ||
{ | ||
$list = []; | ||
$this->flatten($arguments, $list); | ||
|
||
return trim(strtoupper($command) . ' ' . implode(' ', $list)); | ||
} | ||
|
||
/** | ||
* Flatten arguments to single dimension array. | ||
* | ||
* @param mixed[] $arguments An array of command arguments | ||
* @param mixed[] $list Holder of results | ||
*/ | ||
private function flatten(array $arguments, array &$list): void | ||
{ | ||
foreach ($arguments as $key => $item) { | ||
if (!is_numeric($key)) { | ||
$list[] = $key; | ||
} | ||
|
||
if (is_scalar($item)) { | ||
$list[] = strval($item); | ||
} elseif ($item === null) { | ||
$list[] = '<null>'; | ||
} else { | ||
$this->flatten($item, $list); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.