|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace OC\Core\Controller; |
| 9 | + |
| 10 | +use OC\OpenMetrics\ExporterManager; |
| 11 | +use OC\Security\Ip\Address; |
| 12 | +use OC\Security\Ip\Range; |
| 13 | +use OCP\AppFramework\Controller; |
| 14 | +use OCP\AppFramework\Http; |
| 15 | +use OCP\AppFramework\Http\Attribute\FrontpageRoute; |
| 16 | +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; |
| 17 | +use OCP\AppFramework\Http\Attribute\PublicPage; |
| 18 | +use OCP\IConfig; |
| 19 | +use OCP\IRequest; |
| 20 | +use OCP\OpenMetrics\IMetricFamily; |
| 21 | +use OCP\OpenMetrics\Metric; |
| 22 | +use OCP\OpenMetrics\MetricType; |
| 23 | +use OCP\OpenMetrics\MetricValue; |
| 24 | +use Psr\Log\LoggerInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * OpenMetrics controller |
| 28 | + * |
| 29 | + * Gather and display metrics |
| 30 | + * |
| 31 | + * @package OC\Core\Controller |
| 32 | + */ |
| 33 | +class OpenMetricsController extends Controller { |
| 34 | + public function __construct( |
| 35 | + string $appName, |
| 36 | + IRequest $request, |
| 37 | + private IConfig $config, |
| 38 | + private ExporterManager $exporterManager, |
| 39 | + private LoggerInterface $logger, |
| 40 | + ) { |
| 41 | + parent::__construct($appName, $request); |
| 42 | + } |
| 43 | + |
| 44 | + #[NoCSRFRequired] |
| 45 | + #[PublicPage] |
| 46 | + #[FrontpageRoute(verb: 'GET', url: '/metrics')] |
| 47 | + public function export(): Http\Response { |
| 48 | + if (!$this->isRemoteAddressAllowed()) { |
| 49 | + return new Http\Response(Http::STATUS_FORBIDDEN); |
| 50 | + } |
| 51 | + |
| 52 | + return new Http\StreamTraversableResponse( |
| 53 | + $this->generate(), |
| 54 | + Http::STATUS_OK, |
| 55 | + [ |
| 56 | + 'Content-Type' => 'application/openmetrics-text; version=1.0.0; charset=utf-8', |
| 57 | + ] |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private function isRemoteAddressAllowed(): bool { |
| 62 | + $clientAddress = new Address($this->request->getRemoteAddress()); |
| 63 | + $allowedRanges = $this->config->getSystemValue('openmetrics_allowed_clients', ['127.0.0.0/16', '::1/128']); |
| 64 | + if (!is_array($allowedRanges)) { |
| 65 | + $this->logger->warning('Invalid configuration for "openmetrics_allowed_clients"'); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($allowedRanges as $range) { |
| 70 | + $range = new Range($range); |
| 71 | + if ($range->contains($clientAddress)) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + private function generate(): \Generator { |
| 80 | + foreach ($this->exporterManager->export() as $family) { |
| 81 | + yield $this->formatFamily($family); |
| 82 | + } |
| 83 | + |
| 84 | + $elapsed = (string)(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']); |
| 85 | + yield <<<SUMMARY |
| 86 | + # TYPE nextcloud_exporter_duration gauge |
| 87 | + # UNIT nextcloud_exporter_duration seconds |
| 88 | + # HELP nextcloud_exporter_duration Exporter run time |
| 89 | + nextcloud_exporter_duration $elapsed |
| 90 | +
|
| 91 | + # EOF |
| 92 | +
|
| 93 | + SUMMARY; |
| 94 | + } |
| 95 | + |
| 96 | + private function formatFamily(IMetricFamily $family): string { |
| 97 | + $output = ''; |
| 98 | + $name = $family->name(); |
| 99 | + if ($family->type() !== MetricType::unknown) { |
| 100 | + $output = '# TYPE nextcloud_' . $name . ' ' . $family->type()->name . "\n"; |
| 101 | + } |
| 102 | + if ($family->unit() !== '') { |
| 103 | + $output .= '# UNIT nextcloud_' . $name . ' ' . $family->unit() . "\n"; |
| 104 | + } |
| 105 | + if ($family->help() !== '') { |
| 106 | + $output .= '# HELP nextcloud_' . $name . ' ' . $family->help() . "\n"; |
| 107 | + } |
| 108 | + foreach ($family->metrics() as $metric) { |
| 109 | + $output .= 'nextcloud_' . $name . $this->formatLabels($metric) . ' ' . $this->formatValue($metric); |
| 110 | + if ($metric->timestamp !== null) { |
| 111 | + $output .= ' ' . $this->formatTimestamp($metric); |
| 112 | + } |
| 113 | + $output .= "\n"; |
| 114 | + } |
| 115 | + $output .= "\n"; |
| 116 | + |
| 117 | + return $output; |
| 118 | + } |
| 119 | + |
| 120 | + private function formatLabels(Metric $metric): string { |
| 121 | + if (empty($metric->labels)) { |
| 122 | + return ''; |
| 123 | + } |
| 124 | + |
| 125 | + $labels = []; |
| 126 | + foreach ($metric->labels as $label => $value) { |
| 127 | + $labels[] .= $label . '=' . $this->escapeString((string)$value); |
| 128 | + } |
| 129 | + |
| 130 | + return '{' . implode(',', $labels) . '}'; |
| 131 | + } |
| 132 | + |
| 133 | + 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 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + private function formatValue(Metric $metric): string { |
| 142 | + if (is_bool($metric->value)) { |
| 143 | + return $metric->value ? '1' : '0'; |
| 144 | + } |
| 145 | + if ($metric->value instanceof MetricValue) { |
| 146 | + return $metric->value->value; |
| 147 | + } |
| 148 | + |
| 149 | + return (string)$metric->value; |
| 150 | + } |
| 151 | + |
| 152 | + private function formatTimestamp(Metric $metric): string { |
| 153 | + return (string)$metric->timestamp; |
| 154 | + } |
| 155 | +} |
0 commit comments