Skip to content
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

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

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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this at the end, as it would be a breaking change otherwise.

{
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