@@ -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
10969local increment = redis.call('hIncrByFloat', KEYS[1], KEYS[2], ARGV[1])
11070redis.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
0 commit comments