Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit 4b675e3

Browse files
committed
Remove handling of connection from predis adapter
1 parent 8d21a57 commit 4b675e3

File tree

11 files changed

+33
-81
lines changed

11 files changed

+33
-81
lines changed

examples/flush_adapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
$redisAdapter = new Prometheus\Storage\Redis(array('host' => REDIS_HOST));
1010
$redisAdapter->flushRedis();
1111
} elseif ($adapter === 'predis') {
12-
$adapter = new Prometheus\Storage\Predis([
13-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
14-
]);
12+
$adapter = new Prometheus\Storage\Predis(
13+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
14+
);
1515
$adapter->flushRedis();
1616
} elseif ($adapter === 'apc') {
1717
$apcAdapter = new Prometheus\Storage\APC();

examples/metrics.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
1313
$adapter = new Prometheus\Storage\Redis();
1414
} elseif ($adapter === 'predis') {
15-
$adapter = new Prometheus\Storage\Predis([
16-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
17-
]);
15+
$adapter = new Prometheus\Storage\Predis(
16+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
17+
);
1818
} elseif ($adapter === 'apc') {
1919
$adapter = new Prometheus\Storage\APC();
2020
} elseif ($adapter === 'in-memory') {

examples/pushgateway.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
1111
$adapter = new Prometheus\Storage\Redis();
1212
} elseif ($adapter === 'predis') {
13-
$adapter = new Prometheus\Storage\Predis([
14-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
15-
]);
13+
$adapter = new Prometheus\Storage\Predis(
14+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
15+
);
1616
} elseif ($adapter === 'apc') {
1717
$adapter = new Prometheus\Storage\APC();
1818
} elseif ($adapter === 'in-memory') {

examples/some_counter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
1212
$adapter = new Prometheus\Storage\Redis();
1313
} elseif ($adapter === 'predis') {
14-
$adapter = new Prometheus\Storage\Predis([
15-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
16-
]);
14+
$adapter = new Prometheus\Storage\Predis(
15+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
16+
);
1717
} elseif ($adapter === 'apc') {
1818
$adapter = new Prometheus\Storage\APC();
1919
} elseif ($adapter === 'in-memory') {

examples/some_gauge.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
1515
$adapter = new Prometheus\Storage\Redis();
1616
} elseif ($adapter === 'predis') {
17-
$adapter = new Prometheus\Storage\Predis([
18-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
19-
]);
17+
$adapter = new Prometheus\Storage\Predis(
18+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
19+
);
2020
} elseif ($adapter === 'apc') {
2121
$adapter = new Prometheus\Storage\APC();
2222
} elseif ($adapter === 'in-memory') {

examples/some_histogram.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
1414
$adapter = new Prometheus\Storage\Redis();
1515
} elseif ($adapter === 'predis') {
16-
$adapter = new Prometheus\Storage\Predis([
17-
'host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'
18-
]);
16+
$adapter = new Prometheus\Storage\Predis(
17+
new \Predis\Client(['host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'])
18+
);
1919
} elseif ($adapter === 'apc') {
2020
$adapter = new Prometheus\Storage\APC();
2121
} elseif ($adapter === 'in-memory') {

src/Prometheus/Storage/Predis.php

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,16 @@ class Predis implements Adapter
1313
{
1414
const PROMETHEUS_METRIC_KEYS_SUFFIX = '_METRIC_KEYS';
1515

16-
private static $defaultOptions = [
17-
'host' => '127.0.0.1',
18-
'port' => 6379,
19-
'timeout' => 0.1,
20-
'read_timeout' => 10,
21-
'persistent_connections' => false,
22-
];
23-
2416
private static $prefix = 'PROMETHEUS_';
2517

26-
private $options;
2718
/**
2819
* @var Client
2920
*/
3021
private $predis;
3122

32-
public function __construct(array $options = array())
23+
public function __construct(Client $predis)
3324
{
34-
$this->options = array_merge(self::$defaultOptions, $options);
35-
}
36-
37-
/**
38-
* @param array $options
39-
*/
40-
public static function setDefaultOptions(array $options)
41-
{
42-
self::$defaultOptions = array_merge(self::$defaultOptions, $options);
25+
$this->predis = $predis;
4326
}
4427

4528
public static function setPrefix($prefix)
@@ -49,7 +32,6 @@ public static function setPrefix($prefix)
4932

5033
public function flushRedis()
5134
{
52-
$this->openConnection();
5335
$this->predis->flushall();
5436
}
5537

@@ -59,7 +41,6 @@ public function flushRedis()
5941
*/
6042
public function collect()
6143
{
62-
$this->openConnection();
6344
$metrics = $this->collectHistograms();
6445
$metrics = array_merge($metrics, $this->collectGauges());
6546
$metrics = array_merge($metrics, $this->collectCounters());
@@ -71,30 +52,8 @@ function (array $metric) {
7152
);
7253
}
7354

74-
/**
75-
* @throws StorageException
76-
*/
77-
private function openConnection()
78-
{
79-
try {
80-
81-
$this->predis = new Client([
82-
'scheme' => 'tcp',
83-
'host' => $this->options['host'],
84-
'port' => $this->options['port'],
85-
'timeout' => $this->options['timeout'],
86-
'read_write_timeout' => $this->options['read_timeout'],
87-
'persistent' => $this->options['persistent_connections'],
88-
]);
89-
90-
} catch (\RedisException $e) {
91-
throw new StorageException("Can't connect to Redis server", 0, $e);
92-
}
93-
}
94-
9555
public function updateHistogram(array $data)
9656
{
97-
$this->openConnection();
9857
$bucketToIncrease = '+Inf';
9958
foreach ($data['buckets'] as $bucket) {
10059
if ($data['value'] <= $bucket) {
@@ -105,6 +64,7 @@ public function updateHistogram(array $data)
10564
$metaData = $data;
10665
unset($metaData['value']);
10766
unset($metaData['labelValues']);
67+
10868
$this->predis->eval(<<<LUA
10969
local increment = redis.call('hIncrByFloat', KEYS[1], KEYS[2], ARGV[1])
11070
redis.call('hIncrBy', KEYS[1], KEYS[3], 1)
@@ -126,7 +86,6 @@ public function updateHistogram(array $data)
12686

12787
public function updateGauge(array $data)
12888
{
129-
$this->openConnection();
13089
$metaData = $data;
13190
unset($metaData['value']);
13291
unset($metaData['labelValues']);
@@ -159,7 +118,6 @@ public function updateGauge(array $data)
159118

160119
public function updateCounter(array $data)
161120
{
162-
$this->openConnection();
163121
$metaData = $data;
164122
unset($metaData['value']);
165123
unset($metaData['labelValues']);
@@ -219,22 +177,16 @@ private function collectHistograms()
219177
$acc = 0;
220178
foreach ($histogram['buckets'] as $bucket) {
221179
$bucketKey = json_encode(array('b' => $bucket, 'labelValues' => $labelValues));
222-
if (!isset($raw[$bucketKey])) {
223-
$histogram['samples'][] = array(
224-
'name' => $histogram['name'] . '_bucket',
225-
'labelNames' => array('le'),
226-
'labelValues' => array_merge($labelValues, array($bucket)),
227-
'value' => $acc
228-
);
229-
} else {
180+
if (isset($raw[$bucketKey])) {
230181
$acc += $raw[$bucketKey];
231-
$histogram['samples'][] = array(
232-
'name' => $histogram['name'] . '_bucket',
233-
'labelNames' => array('le'),
234-
'labelValues' => array_merge($labelValues, array($bucket)),
235-
'value' => $acc
236-
);
237182
}
183+
184+
$histogram['samples'][] = array(
185+
'name' => $histogram['name'] . '_bucket',
186+
'labelNames' => array('le'),
187+
'labelValues' => array_merge($labelValues, array($bucket)),
188+
'value' => $acc
189+
);
238190
}
239191

240192
// Add the count

tests/Test/Prometheus/Predis/CollectorRegistryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CollectorRegistryTest extends AbstractCollectorRegistryTest
1010
{
1111
public function configureAdapter()
1212
{
13-
$this->adapter = new Predis(array('host' => REDIS_HOST));
13+
$this->adapter = new Predis(new \Predis\Client(['host' => REDIS_HOST]));
1414
$this->adapter->flushRedis();
1515
}
1616
}

tests/Test/Prometheus/Predis/CounterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CounterTest extends AbstractCounterTest
1414

1515
public function configureAdapter()
1616
{
17-
$this->adapter = new Predis(array('host' => REDIS_HOST));
17+
$this->adapter = new Predis(new \Predis\Client(['host' => REDIS_HOST]));
1818
$this->adapter->flushRedis();
1919
}
2020
}

tests/Test/Prometheus/Predis/GaugeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GaugeTest extends AbstractGaugeTest
1414

1515
public function configureAdapter()
1616
{
17-
$this->adapter = new Predis(array('host' => REDIS_HOST));
17+
$this->adapter = new Predis(new \Predis\Client(['host' => REDIS_HOST]));
1818
$this->adapter->flushRedis();
1919
}
2020
}

0 commit comments

Comments
 (0)