55namespace Prometheus \Storage ;
66
77use InvalidArgumentException ;
8+ use Predis \Client ;
89use Prometheus \Counter ;
910use Prometheus \Exception \StorageException ;
1011use Prometheus \Gauge ;
@@ -38,12 +39,12 @@ class Redis implements Adapter
3839 /**
3940 * @var mixed[]
4041 */
41- private $ options = [];
42+ protected $ options = [];
4243
4344 /**
44- * @var \Redis
45+ * @var \Redis|Client
4546 */
46- private $ redis ;
47+ protected $ redis ;
4748
4849 /**
4950 * @var boolean
@@ -112,8 +113,7 @@ public function wipeStorage(): void
112113
113114 $ searchPattern = "" ;
114115
115- $ globalPrefix = $ this ->redis ->getOption (\Redis::OPT_PREFIX );
116- // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
116+ $ globalPrefix = $ this ->getGlobalPrefix ();
117117 if (is_string ($ globalPrefix )) {
118118 $ searchPattern .= $ globalPrefix ;
119119 }
@@ -133,8 +133,10 @@ public function wipeStorage(): void
133133until cursor == "0"
134134LUA
135135 ,
136- [$ searchPattern ],
137- 0
136+ ...$ this ->evalParams (
137+ [$ searchPattern ],
138+ 0
139+ )
138140 );
139141 }
140142
@@ -187,7 +189,7 @@ function (array $metric): MetricFamilySamples {
187189 /**
188190 * @throws StorageException
189191 */
190- private function ensureOpenConnection (): void
192+ protected function ensureOpenConnection (): void
191193 {
192194 if ($ this ->connectionInitialized === true ) {
193195 return ;
@@ -260,15 +262,17 @@ public function updateHistogram(array $data): void
260262return result
261263LUA
262264 ,
263- [
264- $ this ->toMetricKey ($ data ),
265- self ::$ prefix . Histogram::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
266- json_encode (['b ' => 'sum ' , 'labelValues ' => $ data ['labelValues ' ]]),
267- json_encode (['b ' => $ bucketToIncrease , 'labelValues ' => $ data ['labelValues ' ]]),
268- $ data ['value ' ],
269- json_encode ($ metaData ),
270- ],
271- 2
265+ ...$ this ->evalParams (
266+ [
267+ $ this ->toMetricKey ($ data ),
268+ self ::$ prefix . Histogram::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
269+ json_encode (['b ' => 'sum ' , 'labelValues ' => $ data ['labelValues ' ]]),
270+ json_encode (['b ' => $ bucketToIncrease , 'labelValues ' => $ data ['labelValues ' ]]),
271+ $ data ['value ' ],
272+ json_encode ($ metaData ),
273+ ],
274+ 2
275+ )
272276 );
273277 }
274278
@@ -301,7 +305,7 @@ public function updateSummary(array $data): void
301305 $ done = false ;
302306 while (!$ done ) {
303307 $ sampleKey = $ valueKey . ': ' . uniqid ('' , true );
304- $ done = $ this ->redis ->set ($ sampleKey , $ data ['value ' ], ['NX ' , 'EX ' => $ data ['maxAgeSeconds ' ]]);
308+ $ done = $ this ->redis ->set ($ sampleKey , $ data ['value ' ], ... $ this -> setParams ( ['NX ' , 'EX ' => $ data ['maxAgeSeconds ' ]]) );
305309 }
306310 }
307311
@@ -331,15 +335,17 @@ public function updateGauge(array $data): void
331335end
332336LUA
333337 ,
334- [
335- $ this ->toMetricKey ($ data ),
336- self ::$ prefix . Gauge::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
337- $ this ->getRedisCommand ($ data ['command ' ]),
338- json_encode ($ data ['labelValues ' ]),
339- $ data ['value ' ],
340- json_encode ($ metaData ),
341- ],
342- 2
338+ ...$ this ->evalParams (
339+ [
340+ $ this ->toMetricKey ($ data ),
341+ self ::$ prefix . Gauge::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
342+ $ this ->getRedisCommand ($ data ['command ' ]),
343+ json_encode ($ data ['labelValues ' ]),
344+ $ data ['value ' ],
345+ json_encode ($ metaData ),
346+ ],
347+ 2
348+ )
343349 );
344350 }
345351
@@ -362,15 +368,17 @@ public function updateCounter(array $data): void
362368return result
363369LUA
364370 ,
365- [
366- $ this ->toMetricKey ($ data ),
367- self ::$ prefix . Counter::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
368- $ this ->getRedisCommand ($ data ['command ' ]),
369- $ data ['value ' ],
370- json_encode ($ data ['labelValues ' ]),
371- json_encode ($ metaData ),
372- ],
373- 2
371+ ...$ this ->evalParams (
372+ [
373+ $ this ->toMetricKey ($ data ),
374+ self ::$ prefix . Counter::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX ,
375+ $ this ->getRedisCommand ($ data ['command ' ]),
376+ $ data ['value ' ],
377+ json_encode ($ data ['labelValues ' ]),
378+ json_encode ($ metaData ),
379+ ],
380+ 2
381+ )
374382 );
375383 }
376384
@@ -395,7 +403,7 @@ private function collectHistograms(): array
395403 sort ($ keys );
396404 $ histograms = [];
397405 foreach ($ keys as $ key ) {
398- $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis -> _prefix ('' ), '' , $ key ));
406+ $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->prefix ('' ), '' , $ key ));
399407 if (!isset ($ raw ['__meta ' ])) {
400408 continue ;
401409 }
@@ -473,12 +481,11 @@ private function collectHistograms(): array
473481 */
474482 private function removePrefixFromKey (string $ key ): string
475483 {
476- // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
477- if ($ this ->redis ->getOption (\Redis::OPT_PREFIX ) === null ) {
484+ if ($ this ->getGlobalPrefix () === null ) {
478485 return $ key ;
479486 }
480- // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
481- return substr ($ key , strlen ($ this ->redis -> getOption (\Redis:: OPT_PREFIX )));
487+
488+ return substr ($ key , strlen ($ this ->getGlobalPrefix ( )));
482489 }
483490
484491 /**
@@ -578,7 +585,7 @@ private function collectGauges(bool $sortMetrics = true): array
578585 sort ($ keys );
579586 $ gauges = [];
580587 foreach ($ keys as $ key ) {
581- $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis -> _prefix ('' ), '' , $ key ));
588+ $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->prefix ('' ), '' , $ key ));
582589 if (!isset ($ raw ['__meta ' ])) {
583590 continue ;
584591 }
@@ -614,7 +621,7 @@ private function collectCounters(bool $sortMetrics = true): array
614621 sort ($ keys );
615622 $ counters = [];
616623 foreach ($ keys as $ key ) {
617- $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis -> _prefix ('' ), '' , $ key ));
624+ $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->prefix ('' ), '' , $ key ));
618625 if (!isset ($ raw ['__meta ' ])) {
619626 continue ;
620627 }
@@ -699,4 +706,30 @@ private function decodeLabelValues(string $values): array
699706 }
700707 return $ decodedValues ;
701708 }
709+
710+ protected function getGlobalPrefix (): ?string
711+ {
712+ // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
713+ return $ this ->redis ->getOption (\Redis::OPT_PREFIX );
714+ }
715+
716+ /**
717+ * @param mixed[] $args
718+ * @param int $keysCount
719+ * @return mixed[]
720+ */
721+ protected function evalParams (array $ args , int $ keysCount ): array
722+ {
723+ return [$ args , $ keysCount ];
724+ }
725+
726+ protected function prefix (string $ key ): string
727+ {
728+ return $ this ->redis ->_prefix ($ key );
729+ }
730+
731+ protected function setParams (array $ params ): array
732+ {
733+ return [$ params ];
734+ }
702735}
0 commit comments