Skip to content

fix for APCng prometheus storage - very high ttl for metrics entries … #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
@@ -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 $decimalPrecision = 3, int $ttl = 10368000)
{
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);
}
@@ -115,7 +123,7 @@ public function updateHistogram(array $data): void
// Initialize and increment the bucket
$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
if (!apcu_exists($bucketKey)) {
apcu_add($bucketKey, 0);
apcu_add($bucketKey, $this->ttl);
}
apcu_inc($bucketKey);
}
@@ -137,7 +145,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);
@@ -173,7 +181,7 @@ public function updateGauge(array $data): void
if ($data['command'] === Adapter::COMMAND_SET) {
$new = $this->convertToIncrementalInteger($data['value']);
if ($old === false) {
apcu_store($valueKey, $new, 0);
apcu_store($valueKey, $new, $this->ttl);
$this->storeMetadata($data);
$this->storeLabelKeys($data);

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

if ($old === false) {
apcu_add($valueKey, 0, 0);
apcu_add($valueKey, 0, $this->ttl);
$this->storeMetadata($data);
$this->storeLabelKeys($data);
}
@@ -220,7 +228,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);
}
@@ -274,7 +282,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);
}
}

@@ -356,7 +364,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;
}
@@ -915,17 +923,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