Skip to content

Commit fca5506

Browse files
author
Rastusik
committed
fix for APCng prometheus storage - 0 ttl for entries does not work well with apcu entry expiration algorithm when low global ttl is set, entry ttl needs to be very high
Signed-off-by: Rastusik <[email protected]>
1 parent d10c3be commit fca5506

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Prometheus/Storage/APCng.php

+18-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 $ttl = 10368000, int $decimalPrecision = 3)
5157
{
5258
if (!extension_loaded('apcu')) {
5359
throw new StorageException('APCu extension is not loaded');
@@ -96,7 +102,7 @@ public function updateHistogram(array $data): void
96102

97103
if ($old === false) {
98104
// If sum does not exist, initialize it, store the metadata for the new histogram
99-
apcu_add($sumKey, 0, 0);
105+
apcu_add($sumKey, 0, $this->ttl);
100106
$this->storeMetadata($data);
101107
$this->storeLabelKeys($data);
102108
}
@@ -113,7 +119,7 @@ public function updateHistogram(array $data): void
113119
}
114120

115121
// Initialize and increment the bucket
116-
apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0);
122+
apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0, $this->ttl);
117123
apcu_inc($this->histogramBucketValueKey($data, $bucketToIncrease));
118124
}
119125

@@ -134,7 +140,7 @@ public function updateSummary(array $data): void
134140
{
135141
// store value key; store metadata & labels if new
136142
$valueKey = $this->valueKey($data);
137-
$new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), 0);
143+
$new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), $this->ttl);
138144
if ($new) {
139145
$this->storeMetadata($data, false);
140146
$this->storeLabelKeys($data);
@@ -167,7 +173,7 @@ public function updateGauge(array $data): void
167173
{
168174
$valueKey = $this->valueKey($data);
169175
if ($data['command'] === Adapter::COMMAND_SET) {
170-
apcu_store($valueKey, $this->convertToIncrementalInteger($data['value']), 0);
176+
apcu_store($valueKey, $this->convertToIncrementalInteger($data['value']), $this->ttl);
171177
$this->storeMetadata($data);
172178
$this->storeLabelKeys($data);
173179

@@ -177,7 +183,7 @@ public function updateGauge(array $data): void
177183
$old = apcu_fetch($valueKey);
178184

179185
if ($old === false) {
180-
apcu_add($valueKey, 0, 0);
186+
apcu_add($valueKey, 0, $this->ttl);
181187
$this->storeMetadata($data);
182188
$this->storeLabelKeys($data);
183189
}
@@ -199,7 +205,7 @@ public function updateCounter(array $data): void
199205
$old = apcu_fetch($valueKey);
200206

201207
if ($old === false) {
202-
apcu_add($valueKey, 0, 0);
208+
apcu_add($valueKey, 0, $this->ttl);
203209
$this->storeMetadata($data);
204210
$this->storeLabelKeys($data);
205211
}
@@ -253,7 +259,7 @@ private function addItemToKey(string $key, string $item): void
253259
$_item = $this->encodeLabelKey($item);
254260
if (!array_key_exists($_item, $arr)) {
255261
$arr[$_item] = 1;
256-
apcu_store($key, $arr, 0);
262+
apcu_store($key, $arr, $this->ttl);
257263
}
258264
}
259265

@@ -335,7 +341,7 @@ private function scanAndBuildMetainfoCache(): array
335341
$arr[$type][] = ['key' => $metaKey, 'value' => $metaInfo];
336342
}
337343

338-
apcu_store($this->metainfoCacheKey, $arr, 0);
344+
apcu_store($this->metainfoCacheKey, $arr, $this->ttl);
339345

340346
return $arr;
341347
}
@@ -892,17 +898,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
892898
$toStore = json_encode($metaData);
893899
}
894900

895-
$stored = apcu_add($metaKey, $toStore, 0);
901+
$stored = apcu_add($metaKey, $toStore, $this->ttl);
896902

897903
if (!$stored) {
898904
return;
899905
}
900906

901-
apcu_add($this->metaInfoCounterKey, 0, 0);
907+
apcu_add($this->metaInfoCounterKey, 0, $this->ttl);
902908
$counter = apcu_inc($this->metaInfoCounterKey);
903909

904910
$newCountedMetricKey = $this->metaCounterKey($counter);
905-
apcu_store($newCountedMetricKey, $metaKey, 0);
911+
apcu_store($newCountedMetricKey, $metaKey, $this->ttl);
906912
}
907913

908914
private function metaCounterKey(int $counter): string

0 commit comments

Comments
 (0)