Skip to content

Commit

Permalink
apcng collect - do not throw exception if a metakey is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
afilichev-SW committed Apr 19, 2024
1 parent a09ea80 commit 75f3f81
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ 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);
$this->storeMetadata($data);
$this->storeLabelKeys($data);
}

$this->storeMetadata($data);
$this->incrementKeyWithValue($sumKey, $data['value']);

// Figure out in which bucket the observation belongs
Expand Down Expand Up @@ -139,9 +139,9 @@ public function updateSummary(array $data): void
$valueKey = $this->valueKey($data);
$new = apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']), 0);
if ($new) {
$this->storeMetadata($data, false);
$this->storeLabelKeys($data);
}
$this->storeMetadata($data, false);
$sampleKeyPrefix = $valueKey . ':' . time();
$sampleCountKey = $sampleKeyPrefix . ':observations';

Expand Down Expand Up @@ -169,12 +169,12 @@ public function updateSummary(array $data): void
public function updateGauge(array $data): void
{
$valueKey = $this->valueKey($data);
$this->storeMetadata($data);
$old = apcu_fetch($valueKey);
if ($data['command'] === Adapter::COMMAND_SET) {
$new = $this->convertToIncrementalInteger($data['value']);
if ($old === false) {
apcu_store($valueKey, $new, 0);
$this->storeMetadata($data);
$this->storeLabelKeys($data);

return;
Expand All @@ -187,7 +187,6 @@ public function updateGauge(array $data): void
$old = apcu_fetch($valueKey);
if ($old === false) {
apcu_store($valueKey, $new, 0);
$this->storeMetadata($data);
$this->storeLabelKeys($data);

return;
Expand All @@ -199,7 +198,6 @@ public function updateGauge(array $data): void

if ($old === false) {
apcu_add($valueKey, 0, 0);
$this->storeMetadata($data);
$this->storeLabelKeys($data);
}

Expand All @@ -221,9 +219,9 @@ public function updateCounter(array $data): void

if ($old === false) {
apcu_add($valueKey, 0, 0);
$this->storeMetadata($data);
$this->storeLabelKeys($data);
}
$this->storeMetadata($data);

$this->incrementKeyWithValue($valueKey, $data['value']);
}
Expand Down Expand Up @@ -327,15 +325,11 @@ private function scanAndBuildMetainfoCache(): array
$metaKey = apcu_fetch($metaCounterKey);

if (!is_string($metaKey)) {
throw new UnexpectedValueException(
sprintf('Invalid meta counter key: %s', $metaCounterKey)
);
continue;
}

if (preg_match('/' . $this->prometheusPrefix . ':([^:]+):.*:meta/', $metaKey, $matches) !== 1) {
throw new UnexpectedValueException(
sprintf('Invalid meta key: %s', $metaKey)
);
continue;
}

$type = $matches[1];
Expand All @@ -348,9 +342,7 @@ private function scanAndBuildMetainfoCache(): array
$metaInfo = apcu_fetch($metaKey);

if ($metaInfo === false) {
throw new UnexpectedValueException(
sprintf('Meta info missing for meta key: %s', $metaKey)
);
continue;
}

$arr[$type][] = ['key' => $metaKey, 'value' => $metaInfo];
Expand Down

0 comments on commit 75f3f81

Please sign in to comment.