Skip to content

Commit d055837

Browse files
authored
Merge branch 'master' into apcu-present-check
2 parents 60ee85d + e16ce9a commit d055837

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

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
129+
*
128130
* @return void
129131
*/
130132
public function flushAPC(): void
131133
{
132-
apcu_clear_cache();
134+
// / / | PCRE expresion boundary
135+
// ^ | match from first character only
136+
// %s: | common prefix substitute with colon suffix
137+
// .+ | at least one additional character
138+
$matchAll = sprintf('/^%s:.+/', self::PROMETHEUS_PREFIX);
139+
140+
foreach (new APCUIterator($matchAll) as $key => $value) {
141+
apcu_delete($key);
142+
}
133143
}
134144

135145
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Prometheus\Storage;
6+
7+
use APCuIterator;
8+
use PHPUnit\Framework\TestCase;
9+
use Prometheus\CollectorRegistry;
10+
11+
/**
12+
* @requires extension apcu
13+
*/
14+
class APCTest extends TestCase
15+
{
16+
17+
/**
18+
* @test
19+
*/
20+
public function itShouldNotClearWholeAPCacheOnFlush(): void
21+
{
22+
apcu_clear_cache();
23+
apcu_add("not a prometheus metric key", "data");
24+
25+
$apc = new APC();
26+
$registry = new CollectorRegistry($apc);
27+
$registry->getOrRegisterCounter("namespace", "counter", "counter help")->inc();
28+
$registry->getOrRegisterGauge("namespace", "gauge", "gauge help")->inc();
29+
$registry->getOrRegisterHistogram("namespace", "histogram", "histogram help")->observe(1);
30+
$apc->flushAPC();
31+
32+
$cacheEntries = iterator_to_array(new APCuIterator(null), true);
33+
$cacheMap = array_map(function ($item) {
34+
return $item['value'];
35+
}, $cacheEntries);
36+
37+
self::assertThat(
38+
$cacheMap,
39+
self::equalTo([
40+
'not a prometheus metric key' => 'data',
41+
])
42+
);
43+
}
44+
}

0 commit comments

Comments
 (0)