From 99bac310d1deedb4d1e79d55357faa120f3ecf9f Mon Sep 17 00:00:00 2001 From: Benjamin Gaussorgues Date: Mon, 18 May 2026 12:10:07 +0200 Subject: [PATCH] feat(openmetrics): export more resilient if exception happens Signed-off-by: Benjamin Gaussorgues --- core/Controller/OpenMetricsController.php | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/Controller/OpenMetricsController.php b/core/Controller/OpenMetricsController.php index c6e6c1673aa73..ab7383f93a433 100644 --- a/core/Controller/OpenMetricsController.php +++ b/core/Controller/OpenMetricsController.php @@ -1,6 +1,7 @@ generate(), - Http::STATUS_OK, - [ - 'Content-Type' => 'application/openmetrics-text; version=1.0.0; charset=utf-8', - ] - ); + return new StreamTraversableResponse($this->generate(), Http::STATUS_OK, [ + 'Content-Type' => 'application/openmetrics-text; version=1.0.0; charset=utf-8', + ]); } private function isRemoteAddressAllowed(): bool { @@ -80,7 +77,16 @@ private function isRemoteAddressAllowed(): bool { private function generate(): \Generator { foreach ($this->exporterManager->export() as $family) { - yield $this->formatFamily($family); + try { + yield $this->formatFamily($family); + } catch (\Exception $e) { + // Skip family and return a valid result + $this->logger->error('Exception caught when exporting family {family}', [ + 'app' => 'metrics', + 'family' => $family->name(), + 'exception' => $e, + ]); + } } $elapsed = (string)(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']); @@ -131,11 +137,7 @@ private function formatLabels(Metric $metric): string { } private function escapeString(string $string): string { - return json_encode( - $string, - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, - 1 - ); + return json_encode($string, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, 1); } private function formatValue(Metric $metric): string {