From 018a336e7a36c6133feead2ea1d09b25a76f7adf Mon Sep 17 00:00:00 2001 From: Rastusik Date: Tue, 29 Nov 2022 14:36:02 +0100 Subject: [PATCH] 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 --- src/Prometheus/Storage/APCng.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Prometheus/Storage/APCng.php b/src/Prometheus/Storage/APCng.php index 080562b0..69dbaabb 100644 --- a/src/Prometheus/Storage/APCng.php +++ b/src/Prometheus/Storage/APCng.php @@ -18,6 +18,11 @@ class APCng implements Adapter private const MAX_LOOPS = 10; + /** + * @var int ttl for all apcu entries reserved for prometheus + */ + private $ttl; + /** * @var int */ @@ -44,10 +49,11 @@ class APCng implements Adapter * APCng constructor. * * @param string $prometheusPrefix Prefix for APCu keys (defaults to {@see PROMETHEUS_PREFIX}). + * @param int $ttl ttl for all apcu entries reserved for prometheus, default is 120 days * * @throws StorageException */ - public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, int $decimalPrecision = 3) + public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, int $ttl = 10368000, int $decimalPrecision = 3) { if (!extension_loaded('apcu')) { throw new StorageException('APCu extension is not loaded'); @@ -61,6 +67,8 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, $this->metaInfoCounterKey = implode(':', [ $this->prometheusPrefix, 'metainfocounter' ]); $this->metaInfoCountedMetricKeyPattern = implode(':', [ $this->prometheusPrefix, 'metainfocountedmetric_#COUNTER#' ]); + $this->ttl = $ttl; + if ($decimalPrecision < 0 || $decimalPrecision > 6) { throw new UnexpectedValueException( sprintf('Decimal precision %d is not from interval <0;6>.', $decimalPrecision) @@ -96,7 +104,7 @@ public function updateHistogram(array $data): void if ($old === false) { // If sum does not exist, initialize it, store the metadata for the new histogram - apcu_add($sumKey, 0, 0); + apcu_add($sumKey, 0, $this->ttl); $this->storeMetadata($data); $this->storeLabelKeys($data); } @@ -113,7 +121,7 @@ public function updateHistogram(array $data): void } // Initialize and increment the bucket - apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0); + apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0, $this->ttl); apcu_inc($this->histogramBucketValueKey($data, $bucketToIncrease)); } @@ -134,7 +142,7 @@ public function updateSummary(array $data): void { // store value key; store metadata & labels if new $valueKey = $this->valueKey($data); - $new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), 0); + $new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), $this->ttl); if ($new) { $this->storeMetadata($data, false); $this->storeLabelKeys($data); @@ -167,7 +175,7 @@ public function updateGauge(array $data): void { $valueKey = $this->valueKey($data); if ($data['command'] === Adapter::COMMAND_SET) { - apcu_store($valueKey, $this->convertToIncrementalInteger($data['value']), 0); + apcu_store($valueKey, $this->convertToIncrementalInteger($data['value']), $this->ttl); $this->storeMetadata($data); $this->storeLabelKeys($data); @@ -177,7 +185,7 @@ public function updateGauge(array $data): void $old = apcu_fetch($valueKey); if ($old === false) { - apcu_add($valueKey, 0, 0); + apcu_add($valueKey, 0, $this->ttl); $this->storeMetadata($data); $this->storeLabelKeys($data); } @@ -199,7 +207,7 @@ public function updateCounter(array $data): void $old = apcu_fetch($valueKey); if ($old === false) { - apcu_add($valueKey, 0, 0); + apcu_add($valueKey, 0, $this->ttl); $this->storeMetadata($data); $this->storeLabelKeys($data); } @@ -253,7 +261,7 @@ private function addItemToKey(string $key, string $item): void $_item = $this->encodeLabelKey($item); if (!array_key_exists($_item, $arr)) { $arr[$_item] = 1; - apcu_store($key, $arr, 0); + apcu_store($key, $arr, $this->ttl); } } @@ -335,7 +343,7 @@ private function scanAndBuildMetainfoCache(): array $arr[$type][] = ['key' => $metaKey, 'value' => $metaInfo]; } - apcu_store($this->metainfoCacheKey, $arr, 0); + apcu_store($this->metainfoCacheKey, $arr, $this->ttl); return $arr; } @@ -892,17 +900,17 @@ private function storeMetadata(array $data, bool $encoded = true): void $toStore = json_encode($metaData); } - $stored = apcu_add($metaKey, $toStore, 0); + $stored = apcu_add($metaKey, $toStore, $this->ttl); if (!$stored) { return; } - apcu_add($this->metaInfoCounterKey, 0, 0); + apcu_add($this->metaInfoCounterKey, 0, $this->ttl); $counter = apcu_inc($this->metaInfoCounterKey); $newCountedMetricKey = $this->metaCounterKey($counter); - apcu_store($newCountedMetricKey, $metaKey, 0); + apcu_store($newCountedMetricKey, $metaKey, $this->ttl); } private function metaCounterKey(int $counter): string