Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -91,7 +96,7 @@ public static function setPrefix($prefix): void
*/
public function flushRedis(): void
{
$this->openConnection();
$this->ensureOpenConnection();
$this->redis->flushAll();
}

Expand All @@ -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());
Expand All @@ -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']);
Expand All @@ -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);
}
}

Expand All @@ -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) {
Expand All @@ -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(
<<<LUA
Expand Down Expand Up @@ -204,11 +209,9 @@ public function updateHistogram(array $data): void
*/
public function updateGauge(array $data): void
{
$this->openConnection();
$this->ensureOpenConnection();
$metaData = $data;
unset($metaData['value']);
unset($metaData['labelValues']);
unset($metaData['command']);
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])
Expand Down Expand Up @@ -244,11 +247,9 @@ public function updateGauge(array $data): void
*/
public function updateCounter(array $data): void
{
$this->openConnection();
$this->ensureOpenConnection();
$metaData = $data;
unset($metaData['value']);
unset($metaData['labelValues']);
unset($metaData['command']);
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[3], ARGV[2])
Expand Down