Skip to content

Commit ac7f0ed

Browse files
committed
Remove usage of apcu_inc for counters
Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent 0ced5ab commit ac7f0ed

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Prometheus/Storage/APC.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,21 @@ public function updateGauge(array $data): void
9191
*/
9292
public function updateCounter(array $data): void
9393
{
94-
$new = apcu_add($this->valueKey($data), 0);
95-
if ($new) {
94+
$valueKey = $this->valueKey($data);
95+
// Check if value key already exists
96+
if (apcu_exists($this->valueKey($data)) === false) {
97+
apcu_add($this->valueKey($data), 0);
9698
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
9799
}
98-
apcu_inc($this->valueKey($data), $data['value']);
100+
101+
// Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91
102+
$done = false;
103+
while (!$done) {
104+
$old = apcu_fetch($valueKey);
105+
if ($old !== false) {
106+
$done = apcu_cas($valueKey, $old, $this->toInteger($this->fromInteger($old) + $data['value']));
107+
}
108+
}
99109
}
100110

101111
/**
@@ -180,7 +190,7 @@ private function collectCounters(): array
180190
'name' => $metaData['name'],
181191
'labelNames' => [],
182192
'labelValues' => $this->decodeLabelValues($labelValues),
183-
'value' => $value['value'],
193+
'value' => $this->fromInteger($value['value']),
184194
];
185195
}
186196
$this->sortSamples($data['samples']);

0 commit comments

Comments
 (0)