Skip to content

Commit 22555ba

Browse files
committed
fix for APCng prometheus storage - very high ttl for metrics entries #101
Apply patch from 018a336 and fix conflicts Signed-off-by: Lukas Kämmerling <[email protected]>
1 parent 3e4811f commit 22555ba

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/Prometheus/Storage/APCng.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class APCng implements Adapter
1818

1919
private const MAX_LOOPS = 10;
2020

21+
/**
22+
* @var int ttl for all apcu entries reserved for prometheus
23+
*/
24+
private $ttl;
25+
2126
/**
2227
* @var int
2328
*/
@@ -44,10 +49,11 @@ class APCng implements Adapter
4449
* APCng constructor.
4550
*
4651
* @param string $prometheusPrefix Prefix for APCu keys (defaults to {@see PROMETHEUS_PREFIX}).
52+
* @param int $ttl ttl for all apcu entries reserved for prometheus, default is 120 days
4753
*
4854
* @throws StorageException
4955
*/
50-
public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, int $decimalPrecision = 3)
56+
public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, int $decimalPrecision = 3, int $ttl = 10368000)
5157
{
5258
if (!extension_loaded('apcu')) {
5359
throw new StorageException('APCu extension is not loaded');
@@ -61,6 +67,8 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX,
6167
$this->metaInfoCounterKey = implode(':', [ $this->prometheusPrefix, 'metainfocounter' ]);
6268
$this->metaInfoCountedMetricKeyPattern = implode(':', [ $this->prometheusPrefix, 'metainfocountedmetric_#COUNTER#' ]);
6369

70+
$this->ttl = $ttl;
71+
6472
if ($decimalPrecision < 0 || $decimalPrecision > 6) {
6573
throw new UnexpectedValueException(
6674
sprintf('Decimal precision %d is not from interval <0;6>.', $decimalPrecision)
@@ -96,7 +104,7 @@ public function updateHistogram(array $data): void
96104

97105
if ($old === false) {
98106
// If sum does not exist, initialize it, store the metadata for the new histogram
99-
apcu_add($sumKey, 0, 0);
107+
apcu_add($sumKey, 0, $this->ttl);
100108
$this->storeMetadata($data);
101109
$this->storeLabelKeys($data);
102110
}
@@ -115,7 +123,7 @@ public function updateHistogram(array $data): void
115123
// Initialize and increment the bucket
116124
$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
117125
if (!apcu_exists($bucketKey)) {
118-
apcu_add($bucketKey, 0);
126+
apcu_add($bucketKey, $this->ttl);
119127
}
120128
apcu_inc($bucketKey);
121129
}
@@ -137,7 +145,7 @@ public function updateSummary(array $data): void
137145
{
138146
// store value key; store metadata & labels if new
139147
$valueKey = $this->valueKey($data);
140-
$new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), 0);
148+
$new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), $this->ttl);
141149
if ($new) {
142150
$this->storeMetadata($data, false);
143151
$this->storeLabelKeys($data);
@@ -173,7 +181,7 @@ public function updateGauge(array $data): void
173181
if ($data['command'] === Adapter::COMMAND_SET) {
174182
$new = $this->convertToIncrementalInteger($data['value']);
175183
if ($old === false) {
176-
apcu_store($valueKey, $new, 0);
184+
apcu_store($valueKey, $new, $this->ttl);
177185
$this->storeMetadata($data);
178186
$this->storeLabelKeys($data);
179187

@@ -198,7 +206,7 @@ public function updateGauge(array $data): void
198206
}
199207

200208
if ($old === false) {
201-
apcu_add($valueKey, 0, 0);
209+
apcu_add($valueKey, 0, $this->ttl);
202210
$this->storeMetadata($data);
203211
$this->storeLabelKeys($data);
204212
}
@@ -220,7 +228,7 @@ public function updateCounter(array $data): void
220228
$old = apcu_fetch($valueKey);
221229

222230
if ($old === false) {
223-
apcu_add($valueKey, 0, 0);
231+
apcu_add($valueKey, 0, $this->ttl);
224232
$this->storeMetadata($data);
225233
$this->storeLabelKeys($data);
226234
}
@@ -274,7 +282,7 @@ private function addItemToKey(string $key, string $item): void
274282
$_item = $this->encodeLabelKey($item);
275283
if (!array_key_exists($_item, $arr)) {
276284
$arr[$_item] = 1;
277-
apcu_store($key, $arr, 0);
285+
apcu_store($key, $arr, $this->ttl);
278286
}
279287
}
280288

@@ -356,7 +364,7 @@ private function scanAndBuildMetainfoCache(): array
356364
$arr[$type][] = ['key' => $metaKey, 'value' => $metaInfo];
357365
}
358366

359-
apcu_store($this->metainfoCacheKey, $arr, 0);
367+
apcu_store($this->metainfoCacheKey, $arr, $this->ttl);
360368

361369
return $arr;
362370
}
@@ -915,17 +923,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
915923
$toStore = json_encode($metaData);
916924
}
917925

918-
$stored = apcu_add($metaKey, $toStore, 0);
926+
$stored = apcu_add($metaKey, $toStore, $this->ttl);
919927

920928
if (!$stored) {
921929
return;
922930
}
923931

924-
apcu_add($this->metaInfoCounterKey, 0, 0);
932+
apcu_add($this->metaInfoCounterKey, 0, $this->ttl);
925933
$counter = apcu_inc($this->metaInfoCounterKey);
926934

927935
$newCountedMetricKey = $this->metaCounterKey($counter);
928-
apcu_store($newCountedMetricKey, $metaKey, 0);
936+
apcu_store($newCountedMetricKey, $metaKey, $this->ttl);
929937
}
930938

931939
private function metaCounterKey(int $counter): string

0 commit comments

Comments
 (0)