Skip to content

Commit

Permalink
fix for APCng prometheus storage - 0 ttl for entries does not work we…
Browse files Browse the repository at this point in the history
…ll with apcu entry expiration algorithm when low global ttl is set, entry ttl needs to be very high

Signed-off-by: Rastusik <[email protected]>
  • Loading branch information
Rastusik committed Nov 29, 2022
1 parent d10c3be commit 018a336
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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');
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 018a336

Please sign in to comment.