Skip to content

Commit 16a1da0

Browse files
authored
Merge pull request #60501 from nextcloud/feat/resilient-openmetrics
feat(openmetrics): export more resilient if exception happens
2 parents c8ae619 + 99bac31 commit 16a1da0

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

core/Controller/OpenMetricsController.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
67
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -51,13 +52,9 @@ public function export(): Response {
5152
return new Response(Http::STATUS_FORBIDDEN);
5253
}
5354

54-
return new StreamTraversableResponse(
55-
$this->generate(),
56-
Http::STATUS_OK,
57-
[
58-
'Content-Type' => 'application/openmetrics-text; version=1.0.0; charset=utf-8',
59-
]
60-
);
55+
return new StreamTraversableResponse($this->generate(), Http::STATUS_OK, [
56+
'Content-Type' => 'application/openmetrics-text; version=1.0.0; charset=utf-8',
57+
]);
6158
}
6259

6360
private function isRemoteAddressAllowed(): bool {
@@ -80,7 +77,16 @@ private function isRemoteAddressAllowed(): bool {
8077

8178
private function generate(): \Generator {
8279
foreach ($this->exporterManager->export() as $family) {
83-
yield $this->formatFamily($family);
80+
try {
81+
yield $this->formatFamily($family);
82+
} catch (\Exception $e) {
83+
// Skip family and return a valid result
84+
$this->logger->error('Exception caught when exporting family {family}', [
85+
'app' => 'metrics',
86+
'family' => $family->name(),
87+
'exception' => $e,
88+
]);
89+
}
8490
}
8591

8692
$elapsed = (string)(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
@@ -131,11 +137,7 @@ private function formatLabels(Metric $metric): string {
131137
}
132138

133139
private function escapeString(string $string): string {
134-
return json_encode(
135-
$string,
136-
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
137-
1
138-
);
140+
return json_encode($string, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, 1);
139141
}
140142

141143
private function formatValue(Metric $metric): string {

0 commit comments

Comments
 (0)