5
5
namespace Prometheus \Storage ;
6
6
7
7
use InvalidArgumentException ;
8
+ use Predis \Client ;
8
9
use Prometheus \Counter ;
9
10
use Prometheus \Exception \StorageException ;
10
11
use Prometheus \Gauge ;
@@ -38,12 +39,12 @@ class Redis implements Adapter
38
39
/**
39
40
* @var mixed[]
40
41
*/
41
- private $ options = [];
42
+ protected $ options = [];
42
43
43
44
/**
44
- * @var \Redis
45
+ * @var \Redis|Client
45
46
*/
46
- private $ redis ;
47
+ protected $ redis ;
47
48
48
49
/**
49
50
* @var boolean
@@ -112,8 +113,7 @@ public function wipeStorage(): void
112
113
113
114
$ searchPattern = "" ;
114
115
115
- $ globalPrefix = $ this ->redis ->getOption (\Redis::OPT_PREFIX );
116
- // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
116
+ $ globalPrefix = $ this ->getGlobalPrefix ();
117
117
if (is_string ($ globalPrefix )) {
118
118
$ searchPattern .= $ globalPrefix ;
119
119
}
@@ -133,8 +133,10 @@ public function wipeStorage(): void
133
133
until cursor == "0"
134
134
LUA
135
135
,
136
- [$ searchPattern ],
137
- 0
136
+ ...$ this ->evalParams (
137
+ [$ searchPattern ],
138
+ 0
139
+ )
138
140
);
139
141
}
140
142
@@ -187,7 +189,7 @@ function (array $metric): MetricFamilySamples {
187
189
/**
188
190
* @throws StorageException
189
191
*/
190
- private function ensureOpenConnection (): void
192
+ protected function ensureOpenConnection (): void
191
193
{
192
194
if ($ this ->connectionInitialized === true ) {
193
195
return ;
@@ -260,15 +262,17 @@ public function updateHistogram(array $data): void
260
262
return result
261
263
LUA
262
264
,
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
+ )
272
276
);
273
277
}
274
278
@@ -301,7 +305,7 @@ public function updateSummary(array $data): void
301
305
$ done = false ;
302
306
while (!$ done ) {
303
307
$ 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 ' ]]) );
305
309
}
306
310
}
307
311
@@ -331,15 +335,17 @@ public function updateGauge(array $data): void
331
335
end
332
336
LUA
333
337
,
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
+ )
343
349
);
344
350
}
345
351
@@ -362,15 +368,17 @@ public function updateCounter(array $data): void
362
368
return result
363
369
LUA
364
370
,
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
+ )
374
382
);
375
383
}
376
384
@@ -395,7 +403,7 @@ private function collectHistograms(): array
395
403
sort ($ keys );
396
404
$ histograms = [];
397
405
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 ));
399
407
if (!isset ($ raw ['__meta ' ])) {
400
408
continue ;
401
409
}
@@ -473,12 +481,11 @@ private function collectHistograms(): array
473
481
*/
474
482
private function removePrefixFromKey (string $ key ): string
475
483
{
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 ) {
478
485
return $ key ;
479
486
}
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 ( )));
482
489
}
483
490
484
491
/**
@@ -578,7 +585,7 @@ private function collectGauges(bool $sortMetrics = true): array
578
585
sort ($ keys );
579
586
$ gauges = [];
580
587
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 ));
582
589
if (!isset ($ raw ['__meta ' ])) {
583
590
continue ;
584
591
}
@@ -614,7 +621,7 @@ private function collectCounters(bool $sortMetrics = true): array
614
621
sort ($ keys );
615
622
$ counters = [];
616
623
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 ));
618
625
if (!isset ($ raw ['__meta ' ])) {
619
626
continue ;
620
627
}
@@ -699,4 +706,30 @@ private function decodeLabelValues(string $values): array
699
706
}
700
707
return $ decodedValues ;
701
708
}
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
+ }
702
735
}
0 commit comments