Skip to content

Commit 918b996

Browse files
authored
Replace redis keys to scan (#91)
* replace redis keys to scan Signed-off-by: ikhabitoff <ikhabitoff@gmail.com> * fix phpcs and phpstan errors Signed-off-by: ikhabitoff <ikhabitoff@gmail.com>
1 parent 1088114 commit 918b996

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Prometheus/Storage/Redis.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ private function collectSummaries(): array
485485
{
486486
$math = new Math();
487487
$summaryKey = self::$prefix . Summary::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX;
488-
$keys = $this->redis->keys($summaryKey . ':*:meta');
488+
$keys = $this->getRedisKeys($summaryKey . ':*:meta');
489489

490490
$summaries = [];
491491
foreach ($keys as $metaKeyWithPrefix) {
@@ -506,7 +506,7 @@ private function collectSummaries(): array
506506
'samples' => [],
507507
];
508508

509-
$values = $this->redis->keys($summaryKey . ':' . $metaData['name'] . ':*:value');
509+
$values = $this->getRedisKeys($summaryKey . ':' . $metaData['name'] . ':*:value');
510510
foreach ($values as $valueKeyWithPrefix) {
511511
$valueKey = $this->removePrefixFromKey($valueKeyWithPrefix);
512512
$rawValue = $this->redis->get($valueKey);
@@ -518,7 +518,7 @@ private function collectSummaries(): array
518518
$decodedLabelValues = $this->decodeLabelValues($encodedLabelValues);
519519

520520
$samples = [];
521-
$sampleValues = $this->redis->keys($summaryKey . ':' . $metaData['name'] . ':' . $encodedLabelValues . ':value:*');
521+
$sampleValues = $this->getRedisKeys($summaryKey . ':' . $metaData['name'] . ':' . $encodedLabelValues . ':value:*');
522522
foreach ($sampleValues as $sampleValueWithPrefix) {
523523
$sampleValue = $this->removePrefixFromKey($sampleValueWithPrefix);
524524
$samples[] = (float) $this->redis->get($sampleValue);
@@ -642,6 +642,31 @@ private function getRedisCommand(int $cmd): string
642642
}
643643
}
644644

645+
/**
646+
* @param string $pattern
647+
* @return mixed[]
648+
*/
649+
private function getRedisKeys(string $pattern): array
650+
{
651+
$prefix = $this->redis->getOption(\Redis::OPT_PREFIX);
652+
$optScan = $this->redis->getOption(\Redis::OPT_SCAN);
653+
654+
$cursor = null;
655+
$result = [];
656+
if ($optScan === \Redis::SCAN_RETRY) {
657+
while ($tmpKeys = $this->redis->scan($cursor, $prefix . $pattern)) {
658+
$result = array_merge($result, $tmpKeys);
659+
}
660+
} else {
661+
do {
662+
$tmpKeys = $this->redis->scan($cursor, $prefix . $pattern);
663+
$result = is_array($tmpKeys) ? array_merge($result, $tmpKeys) : $result;
664+
} while ($cursor > 0);
665+
}
666+
667+
return $result;
668+
}
669+
645670
/**
646671
* @param mixed[] $data
647672
* @return string

0 commit comments

Comments
 (0)