Skip to content

Commit 9a51177

Browse files
authored
Make APCu key prefix configurable (#46)
1 parent 5d27b6d commit 9a51177

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/Prometheus/Storage/APC.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111

1212
class APC implements Adapter
1313
{
14+
/** @var string Default prefix to use for APC keys. */
1415
const PROMETHEUS_PREFIX = 'prom';
1516

17+
/** @var string Prefix to use for APC keys. */
18+
private $prometheusPrefix;
19+
1620
/**
1721
* APC constructor.
1822
*
23+
* @param string $prometheusPrefix Prefix for APCu keys (defaults to {@see PROMETHEUS_PREFIX}).
24+
*
1925
* @throws StorageException
2026
*/
21-
public function __construct()
27+
public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX)
2228
{
2329
if (!extension_loaded('apcu')) {
2430
throw new StorageException('APCu extension is not loaded');
2531
}
2632
if (!apcu_enabled()) {
2733
throw new StorageException('APCu is not enabled');
2834
}
35+
36+
$this->prometheusPrefix = $prometheusPrefix;
2937
}
3038

3139
/**
@@ -145,7 +153,7 @@ public function wipeStorage(): void
145153
// ^ | match from first character only
146154
// %s: | common prefix substitute with colon suffix
147155
// .+ | at least one additional character
148-
$matchAll = sprintf('/^%s:.+/', self::PROMETHEUS_PREFIX);
156+
$matchAll = sprintf('/^%s:.+/', $this->prometheusPrefix);
149157

150158
foreach (new APCUIterator($matchAll) as $key => $value) {
151159
apcu_delete($key);
@@ -158,7 +166,7 @@ public function wipeStorage(): void
158166
*/
159167
private function metaKey(array $data): string
160168
{
161-
return implode(':', [self::PROMETHEUS_PREFIX, $data['type'], $data['name'], 'meta']);
169+
return implode(':', [$this->prometheusPrefix, $data['type'], $data['name'], 'meta']);
162170
}
163171

164172
/**
@@ -168,7 +176,7 @@ private function metaKey(array $data): string
168176
private function valueKey(array $data): string
169177
{
170178
return implode(':', [
171-
self::PROMETHEUS_PREFIX,
179+
$this->prometheusPrefix,
172180
$data['type'],
173181
$data['name'],
174182
$this->encodeLabelValues($data['labelValues']),
@@ -184,7 +192,7 @@ private function valueKey(array $data): string
184192
private function histogramBucketValueKey(array $data, $bucket): string
185193
{
186194
return implode(':', [
187-
self::PROMETHEUS_PREFIX,
195+
$this->prometheusPrefix,
188196
$data['type'],
189197
$data['name'],
190198
$this->encodeLabelValues($data['labelValues']),
@@ -210,7 +218,7 @@ private function metaData(array $data): array
210218
private function collectCounters(): array
211219
{
212220
$counters = [];
213-
foreach (new APCUIterator('/^prom:counter:.*:meta/') as $counter) {
221+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':counter:.*:meta/') as $counter) {
214222
$metaData = json_decode($counter['value'], true);
215223
$data = [
216224
'name' => $metaData['name'],
@@ -219,7 +227,7 @@ private function collectCounters(): array
219227
'labelNames' => $metaData['labelNames'],
220228
'samples' => [],
221229
];
222-
foreach (new APCUIterator('/^prom:counter:' . $metaData['name'] . ':.*:value/') as $value) {
230+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':counter:' . $metaData['name'] . ':.*:value/') as $value) {
223231
$parts = explode(':', $value['key']);
224232
$labelValues = $parts[3];
225233
$data['samples'][] = [
@@ -241,7 +249,7 @@ private function collectCounters(): array
241249
private function collectGauges(): array
242250
{
243251
$gauges = [];
244-
foreach (new APCUIterator('/^prom:gauge:.*:meta/') as $gauge) {
252+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':gauge:.*:meta/') as $gauge) {
245253
$metaData = json_decode($gauge['value'], true);
246254
$data = [
247255
'name' => $metaData['name'],
@@ -250,7 +258,7 @@ private function collectGauges(): array
250258
'labelNames' => $metaData['labelNames'],
251259
'samples' => [],
252260
];
253-
foreach (new APCUIterator('/^prom:gauge:' . $metaData['name'] . ':.*:value/') as $value) {
261+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':gauge:' . $metaData['name'] . ':.*:value/') as $value) {
254262
$parts = explode(':', $value['key']);
255263
$labelValues = $parts[3];
256264
$data['samples'][] = [
@@ -273,7 +281,7 @@ private function collectGauges(): array
273281
private function collectHistograms(): array
274282
{
275283
$histograms = [];
276-
foreach (new APCUIterator('/^prom:histogram:.*:meta/') as $histogram) {
284+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':histogram:.*:meta/') as $histogram) {
277285
$metaData = json_decode($histogram['value'], true);
278286
$data = [
279287
'name' => $metaData['name'],
@@ -287,7 +295,7 @@ private function collectHistograms(): array
287295
$data['buckets'][] = '+Inf';
288296

289297
$histogramBuckets = [];
290-
foreach (new APCUIterator('/^prom:histogram:' . $metaData['name'] . ':.*:value/') as $value) {
298+
foreach (new APCUIterator('/^' . $this->prometheusPrefix . ':histogram:' . $metaData['name'] . ':.*:value/') as $value) {
291299
$parts = explode(':', $value['key']);
292300
$labelValues = $parts[3];
293301
$bucket = $parts[4];

tests/Test/Prometheus/Storage/APCTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,29 @@ public function itShouldNotClearWholeAPCacheOnFlush(): void
4141
])
4242
);
4343
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function itShouldUseConfiguredPrefix(): void
49+
{
50+
$apc = new APC('custom_prefix');
51+
$apc->wipeStorage();
52+
53+
$registry = new CollectorRegistry($apc);
54+
55+
$registry->getOrRegisterCounter('namespace', 'counter', 'counter help')->inc();
56+
$registry->getOrRegisterGauge('namespace', 'gauge', 'gauge help')->inc();
57+
$registry->getOrRegisterHistogram('namespace', 'histogram', 'histogram help')->observe(1);
58+
59+
$entries = iterator_to_array(new APCUIterator('/^custom_prefix:.*:meta$/'), true);
60+
61+
$cacheKeys = array_map(function ($item) {
62+
return $item['key'];
63+
}, $entries);
64+
65+
self::assertArrayHasKey('custom_prefix:counter:namespace_counter:meta', $cacheKeys);
66+
self::assertArrayHasKey('custom_prefix:gauge:namespace_gauge:meta', $cacheKeys);
67+
self::assertArrayHasKey('custom_prefix:histogram:namespace_histogram:meta', $cacheKeys);
68+
}
4469
}

0 commit comments

Comments
 (0)