Skip to content

Commit 28d4ad4

Browse files
authored
On flush remove only prometheus specific keys from redis (#31)
Signed-off-by: Matouš Michalík <i@matousmichalik.com>
1 parent 70d67bc commit 28d4ad4

22 files changed

+136
-29
lines changed

examples/flush_adapter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
$adapter = $_GET['adapter'];
5+
$adapterName = $_GET['adapter'];
66

7-
if ($adapter === 'redis') {
7+
$adapter = null;
8+
9+
if ($adapterName === 'redis') {
810
define('REDIS_HOST', $_SERVER['REDIS_HOST'] ?? '127.0.0.1');
911

10-
$redisAdapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
11-
$redisAdapter->flushRedis();
12-
} elseif ($adapter === 'apc') {
13-
$apcAdapter = new Prometheus\Storage\APC();
14-
$apcAdapter->flushAPC();
15-
} elseif ($adapter === 'in-memory') {
16-
$inMemoryAdapter = new Prometheus\Storage\InMemory();
17-
$inMemoryAdapter->flushMemory();
12+
$adapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
13+
} elseif ($adapterName === 'apc') {
14+
$adapter = new Prometheus\Storage\APC();
15+
} elseif ($adapterName === 'in-memory') {
16+
$adapter = new Prometheus\Storage\InMemory();
1817
}
18+
19+
$adapter->wipeStorage();

src/Prometheus/Storage/APC.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,21 @@ public function updateCounter(array $data): void
125125
}
126126

127127
/**
128-
* Removes all previously stored data from apcu
128+
* @deprecated use replacement method wipeStorage from Adapter interface
129129
*
130130
* @return void
131131
*/
132132
public function flushAPC(): void
133+
{
134+
$this->wipeStorage();
135+
}
136+
137+
/**
138+
* Removes all previously stored data from apcu
139+
*
140+
* @return void
141+
*/
142+
public function wipeStorage(): void
133143
{
134144
// / / | PCRE expresion boundary
135145
// ^ | match from first character only

src/Prometheus/Storage/Adapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Prometheus\Storage;
66

7+
use Prometheus\Exception\StorageException;
78
use Prometheus\MetricFamilySamples;
89

910
interface Adapter
@@ -34,4 +35,12 @@ public function updateGauge(array $data): void;
3435
* @return void
3536
*/
3637
public function updateCounter(array $data): void;
38+
39+
/**
40+
* Removes all previously stored metrics from underlying storage
41+
*
42+
* @throws StorageException
43+
* @return void
44+
*/
45+
public function wipeStorage(): void;
3746
}

src/Prometheus/Storage/InMemory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ public function collect(): array
3636
return $metrics;
3737
}
3838

39+
/**
40+
* @deprecated use replacement method wipeStorage from Adapter interface
41+
*/
3942
public function flushMemory(): void
43+
{
44+
$this->wipeStorage();
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function wipeStorage(): void
4051
{
4152
$this->counters = [];
4253
$this->gauges = [];

src/Prometheus/Storage/Redis.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,47 @@ public static function setPrefix(string $prefix): void
9292
}
9393

9494
/**
95+
* @deprecated use replacement method wipeStorage from Adapter interface
9596
* @throws StorageException
9697
*/
9798
public function flushRedis(): void
99+
{
100+
$this->wipeStorage();
101+
}
102+
103+
/**
104+
* @inheritDoc
105+
*/
106+
public function wipeStorage(): void
98107
{
99108
$this->ensureOpenConnection();
100-
$this->redis->flushAll();
109+
110+
$searchPattern = "";
111+
112+
$globalPrefix = $this->redis->getOption(\Redis::OPT_PREFIX);
113+
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
114+
if (is_string($globalPrefix)) {
115+
$searchPattern .= $globalPrefix;
116+
}
117+
118+
$searchPattern .= self::$prefix;
119+
$searchPattern .= '*';
120+
121+
$this->redis->eval(
122+
<<<LUA
123+
local cursor = "0"
124+
repeat
125+
local results = redis.call('SCAN', cursor, 'MATCH', ARGV[1])
126+
cursor = results[1]
127+
for _, key in ipairs(results[2]) do
128+
redis.call('DEL', key)
129+
end
130+
until cursor == "0"
131+
LUA
132+
,
133+
[$searchPattern],
134+
0
135+
);
101136
}
102137

103138
/**

tests/Test/Prometheus/APC/CollectorRegistryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class CollectorRegistryTest extends AbstractCollectorRegistryTest
1616
public function configureAdapter(): void
1717
{
1818
$this->adapter = new APC();
19-
$this->adapter->flushAPC();
19+
$this->adapter->wipeStorage();
2020
}
2121
}

tests/Test/Prometheus/APC/CounterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class CounterTest extends AbstractCounterTest
1616
public function configureAdapter(): void
1717
{
1818
$this->adapter = new APC();
19-
$this->adapter->flushAPC();
19+
$this->adapter->wipeStorage();
2020
}
2121
}

tests/Test/Prometheus/APC/GaugeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class GaugeTest extends AbstractGaugeTest
1616
public function configureAdapter(): void
1717
{
1818
$this->adapter = new APC();
19-
$this->adapter->flushAPC();
19+
$this->adapter->wipeStorage();
2020
}
2121
}

tests/Test/Prometheus/APC/HistogramTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class HistogramTest extends AbstractHistogramTest
1616
public function configureAdapter(): void
1717
{
1818
$this->adapter = new APC();
19-
$this->adapter->flushAPC();
19+
$this->adapter->wipeStorage();
2020
}
2121
}

tests/Test/Prometheus/InMemory/CollectorRegistryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class CollectorRegistryTest extends AbstractCollectorRegistryTest
1212
public function configureAdapter(): void
1313
{
1414
$this->adapter = new InMemory();
15-
$this->adapter->flushMemory();
15+
$this->adapter->wipeStorage();
1616
}
1717
}

0 commit comments

Comments
 (0)