Skip to content

Commit f64e598

Browse files
committed
Storage/Redis & RedisNG: Remove usage of str_replace to get metrics as it might lead to unexpected results
More details in #182 Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent e7e1720 commit f64e598

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/Prometheus/Storage/Redis.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public static function setPrefix(string $prefix): void
9797
}
9898

9999
/**
100-
* @deprecated use replacement method wipeStorage from Adapter interface
101100
* @throws StorageException
101+
* @deprecated use replacement method wipeStorage from Adapter interface
102102
*/
103103
public function flushRedis(): void
104104
{
@@ -230,11 +230,11 @@ private function connectToServer(): void
230230
if ($this->options['persistent_connections'] !== false) {
231231
$connection_successful = $this->redis->pconnect(
232232
$this->options['host'],
233-
(int) $this->options['port'],
234-
(float) $this->options['timeout']
233+
(int)$this->options['port'],
234+
(float)$this->options['timeout']
235235
);
236236
} else {
237-
$connection_successful = $this->redis->connect($this->options['host'], (int) $this->options['port'], (float) $this->options['timeout']);
237+
$connection_successful = $this->redis->connect($this->options['host'], (int)$this->options['port'], (float)$this->options['timeout']);
238238
}
239239
if (!$connection_successful) {
240240
throw new StorageException(
@@ -306,15 +306,17 @@ public function updateSummary(array $data): void
306306
if (false === $json) {
307307
throw new RuntimeException(json_last_error_msg());
308308
}
309-
$this->redis->setNx($metaKey, $json); /** @phpstan-ignore-line */
309+
$this->redis->setNx($metaKey, $json);
310+
/** @phpstan-ignore-line */
310311

311312
// store value key
312313
$valueKey = $summaryKey . ':' . $this->valueKey($data);
313314
$json = json_encode($this->encodeLabelValues($data['labelValues']));
314315
if (false === $json) {
315316
throw new RuntimeException(json_last_error_msg());
316317
}
317-
$this->redis->setNx($valueKey, $json); /** @phpstan-ignore-line */
318+
$this->redis->setNx($valueKey, $json);
319+
/** @phpstan-ignore-line */
318320

319321
// trick to handle uniqid collision
320322
$done = false;
@@ -414,7 +416,7 @@ private function collectHistograms(): array
414416
sort($keys);
415417
$histograms = [];
416418
foreach ($keys as $key) {
417-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
419+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
418420
if (!isset($raw['__meta'])) {
419421
continue;
420422
}
@@ -546,7 +548,7 @@ private function collectSummaries(): array
546548
$sampleValues = $this->redis->keys($summaryKey . ':' . $metaData['name'] . ':' . $encodedLabelValues . ':value:*');
547549
foreach ($sampleValues as $sampleValueWithPrefix) {
548550
$sampleValue = $this->removePrefixFromKey($sampleValueWithPrefix);
549-
$samples[] = (float) $this->redis->get($sampleValue);
551+
$samples[] = (float)$this->redis->get($sampleValue);
550552
}
551553

552554
if (count($samples) === 0) {
@@ -608,7 +610,7 @@ private function collectGauges(bool $sortMetrics = true): array
608610
sort($keys);
609611
$gauges = [];
610612
foreach ($keys as $key) {
611-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
613+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
612614
if (!isset($raw['__meta'])) {
613615
continue;
614616
}
@@ -648,7 +650,7 @@ private function collectCounters(bool $sortMetrics = true): array
648650
sort($keys);
649651
$counters = [];
650652
foreach ($keys as $key) {
651-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
653+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
652654
if (!isset($raw['__meta'])) {
653655
continue;
654656
}

src/Prometheus/Storage/RedisNg.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ private function collectHistograms(): array
414414
sort($keys);
415415
$histograms = [];
416416
foreach ($keys as $key) {
417-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
417+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
418418
if (!isset($raw['__meta'])) {
419419
continue;
420420
}
@@ -597,7 +597,7 @@ private function collectGauges(bool $sortMetrics = true): array
597597
sort($keys);
598598
$gauges = [];
599599
foreach ($keys as $key) {
600-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
600+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
601601
if (!isset($raw['__meta'])) {
602602
continue;
603603
}
@@ -637,7 +637,7 @@ private function collectCounters(bool $sortMetrics = true): array
637637
sort($keys);
638638
$counters = [];
639639
foreach ($keys as $key) {
640-
$raw = $this->redis->hGetAll(str_replace($this->redis->_prefix(''), '', $key));
640+
$raw = $this->redis->hGetAll(ltrim($key, $this->redis->_prefix('')));
641641
if (!isset($raw['__meta'])) {
642642
continue;
643643
}

0 commit comments

Comments
 (0)