@@ -30,7 +30,7 @@ public function updateHistogram(array $data): void
3030 {
3131 // Initialize the sum
3232 $ sumKey = $ this ->histogramBucketValueKey ($ data , 'sum ' );
33- $ new = apcu_add ($ sumKey , $ this ->toInteger (0 ));
33+ $ new = apcu_add ($ sumKey , $ this ->toBinaryRepresentationAsInteger (0 ));
3434
3535 // If sum does not exist, assume a new histogram and store the metadata
3636 if ($ new ) {
@@ -43,7 +43,7 @@ public function updateHistogram(array $data): void
4343 while (!$ done ) {
4444 $ old = apcu_fetch ($ sumKey );
4545 if ($ old !== false ) {
46- $ done = apcu_cas ($ sumKey , $ old , $ this ->toInteger ($ this ->fromInteger ($ old ) + $ data ['value ' ]));
46+ $ done = apcu_cas ($ sumKey , $ old , $ this ->toBinaryRepresentationAsInteger ($ this ->fromBinaryRepresentationAsInteger ($ old ) + $ data ['value ' ]));
4747 }
4848 }
4949
@@ -68,10 +68,10 @@ public function updateGauge(array $data): void
6868 {
6969 $ valueKey = $ this ->valueKey ($ data );
7070 if ($ data ['command ' ] === Adapter::COMMAND_SET ) {
71- apcu_store ($ valueKey , $ this ->toInteger ($ data ['value ' ]));
71+ apcu_store ($ valueKey , $ this ->toBinaryRepresentationAsInteger ($ data ['value ' ]));
7272 apcu_store ($ this ->metaKey ($ data ), json_encode ($ this ->metaData ($ data )));
7373 } else {
74- $ new = apcu_add ($ valueKey , $ this ->toInteger (0 ));
74+ $ new = apcu_add ($ valueKey , $ this ->toBinaryRepresentationAsInteger (0 ));
7575 if ($ new ) {
7676 apcu_store ($ this ->metaKey ($ data ), json_encode ($ this ->metaData ($ data )));
7777 }
@@ -80,7 +80,7 @@ public function updateGauge(array $data): void
8080 while (!$ done ) {
8181 $ old = apcu_fetch ($ valueKey );
8282 if ($ old !== false ) {
83- $ done = apcu_cas ($ valueKey , $ old , $ this ->toInteger ($ this ->fromInteger ($ old ) + $ data ['value ' ]));
83+ $ done = apcu_cas ($ valueKey , $ old , $ this ->toBinaryRepresentationAsInteger ($ this ->fromBinaryRepresentationAsInteger ($ old ) + $ data ['value ' ]));
8484 }
8585 }
8686 }
@@ -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 ->toBinaryRepresentationAsInteger ($ this ->fromBinaryRepresentationAsInteger ($ 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 -> fromBinaryRepresentationAsInteger ( $ value ['value ' ]) ,
184194 ];
185195 }
186196 $ this ->sortSamples ($ data ['samples ' ]);
@@ -211,7 +221,7 @@ private function collectGauges(): array
211221 'name ' => $ metaData ['name ' ],
212222 'labelNames ' => [],
213223 'labelValues ' => $ this ->decodeLabelValues ($ labelValues ),
214- 'value ' => $ this ->fromInteger ($ value ['value ' ]),
224+ 'value ' => $ this ->fromBinaryRepresentationAsInteger ($ value ['value ' ]),
215225 ];
216226 }
217227
@@ -288,7 +298,7 @@ private function collectHistograms(): array
288298 'name ' => $ metaData ['name ' ] . '_sum ' ,
289299 'labelNames ' => [],
290300 'labelValues ' => $ decodedLabelValues ,
291- 'value ' => $ this ->fromInteger ($ histogramBuckets [$ labelValues ]['sum ' ]),
301+ 'value ' => $ this ->fromBinaryRepresentationAsInteger ($ histogramBuckets [$ labelValues ]['sum ' ]),
292302 ];
293303 }
294304 $ histograms [] = new MetricFamilySamples ($ data );
@@ -300,7 +310,7 @@ private function collectHistograms(): array
300310 * @param mixed $val
301311 * @return int
302312 */
303- private function toInteger ($ val ): int
313+ private function toBinaryRepresentationAsInteger ($ val ): int
304314 {
305315 return unpack ('Q ' , pack ('d ' , $ val ))[1 ];
306316 }
@@ -309,7 +319,7 @@ private function toInteger($val): int
309319 * @param mixed $val
310320 * @return float
311321 */
312- private function fromInteger ($ val ): float
322+ private function fromBinaryRepresentationAsInteger ($ val ): float
313323 {
314324 return unpack ('d ' , pack ('Q ' , $ val ))[1 ];
315325 }
0 commit comments