From 19ff5d15bacc44a48542f5564a35610aad2c6c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Fri, 2 Oct 2020 16:03:21 +0200 Subject: [PATCH] Add missing doc strings to Redis Storage Improve type checking at Redis Storage Simplify Redis Storage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Kämmerling --- src/Prometheus/Storage/Redis.php | 53 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index 55706854..6e59574a 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -57,6 +57,11 @@ public function __construct(array $options = []) $this->redis = new \Redis(); } + /** + * @param \Redis $redis + * @return static + * @throws StorageException + */ public static function fromExistingConnection(\Redis $redis): self { if ($redis->isConnected() === false) { @@ -79,9 +84,9 @@ public static function setDefaultOptions(array $options): void } /** - * @param $prefix + * @param string $prefix */ - public static function setPrefix($prefix): void + public static function setPrefix(string $prefix): void { self::$prefix = $prefix; } @@ -91,7 +96,7 @@ public static function setPrefix($prefix): void */ public function flushRedis(): void { - $this->openConnection(); + $this->ensureOpenConnection(); $this->redis->flushAll(); } @@ -101,7 +106,7 @@ public function flushRedis(): void */ public function collect(): array { - $this->openConnection(); + $this->ensureOpenConnection(); $metrics = $this->collectHistograms(); $metrics = array_merge($metrics, $this->collectGauges()); $metrics = array_merge($metrics, $this->collectCounters()); @@ -116,16 +121,13 @@ function (array $metric) { /** * @throws StorageException */ - private function openConnection(): void + private function ensureOpenConnection(): void { if ($this->connectionInitialized === true) { return; } - $connectionStatus = $this->connectToServer(); - if ($connectionStatus === false) { - throw new StorageException("Can't connect to Redis server", 0); - } + $this->connectToServer(); if ($this->options['password']) { $this->redis->auth($this->options['password']); @@ -139,22 +141,26 @@ private function openConnection(): void } /** - * @return bool + * @throws StorageException */ - private function connectToServer(): bool + private function connectToServer(): void { try { + $connection_successful = false; if ($this->options['persistent_connections']) { - return $this->redis->pconnect( + $connection_successful = $this->redis->pconnect( $this->options['host'], $this->options['port'], $this->options['timeout'] ); + } else { + $connection_successful = $this->redis->connect($this->options['host'], $this->options['port'], $this->options['timeout']); + } + if (!$connection_successful) { + throw new StorageException("Can't connect to Redis server", 0); } - - return $this->redis->connect($this->options['host'], $this->options['port'], $this->options['timeout']); } catch (\RedisException $e) { - return false; + throw new StorageException("Can't connect to Redis server", 0, $e); } } @@ -164,7 +170,7 @@ private function connectToServer(): bool */ public function updateHistogram(array $data): void { - $this->openConnection(); + $this->ensureOpenConnection(); $bucketToIncrease = '+Inf'; foreach ($data['buckets'] as $bucket) { if ($data['value'] <= $bucket) { @@ -173,8 +179,7 @@ public function updateHistogram(array $data): void } } $metaData = $data; - unset($metaData['value']); - unset($metaData['labelValues']); + unset($metaData['value'], $metaData['labelValues']); $this->redis->eval( <<openConnection(); + $this->ensureOpenConnection(); $metaData = $data; - unset($metaData['value']); - unset($metaData['labelValues']); - unset($metaData['command']); + unset($metaData['value'], $metaData['labelValues'], $metaData['command']); $this->redis->eval( <<openConnection(); + $this->ensureOpenConnection(); $metaData = $data; - unset($metaData['value']); - unset($metaData['labelValues']); - unset($metaData['command']); + unset($metaData['value'], $metaData['labelValues'], $metaData['command']); $this->redis->eval( <<