diff --git a/src/Prometheus/Storage/APCng.php b/src/Prometheus/Storage/APCng.php index 5448fc95..416a46f9 100644 --- a/src/Prometheus/Storage/APCng.php +++ b/src/Prometheus/Storage/APCng.php @@ -254,7 +254,7 @@ private function storeLabelKeys(array $data): void $data['name'], $label, 'label' - ]), isset($data['labelValues']) ? $data['labelValues'][$seq] : ''); // may not need the isset check + ]), isset($data['labelValues']) ? (string)$data['labelValues'][$seq] : ''); // may not need the isset check } } @@ -860,7 +860,7 @@ private function sortSamples(array &$samples): void */ private function encodeLabelValues(array $values): string { - $json = json_encode($values); + $json = json_encode(array_map("strval", $values)); if (false === $json) { throw new RuntimeException(json_last_error_msg()); } diff --git a/tests/Test/Prometheus/Storage/APCngTest.php b/tests/Test/Prometheus/Storage/APCngTest.php index 8bf2ea26..02be614c 100644 --- a/tests/Test/Prometheus/Storage/APCngTest.php +++ b/tests/Test/Prometheus/Storage/APCngTest.php @@ -42,6 +42,26 @@ public function itShouldNotClearWholeAPCacheOnFlush(): void ); } + /** + * @test + */ + public function nonStringLabelValuesAreCastToStrings(): void + { + apcu_clear_cache(); + + $adapter = new APCng(); + $registry = new CollectorRegistry($adapter); + $registry->getOrRegisterCounter( + 'ns', + 'int_label_values', + 'test int label values', + ['int_label'], + )->incBy(3, [3]); + + $counter = apcu_fetch("prom:counter:ns_int_label_values:WyIzIl0=:value"); + self::assertSame(3000, $counter); + } + /** * @test */