Merge pull request #59720 from nextcloud/feature/59718/appinfo-metrics

feat(openmetrics): expose app information per-app
This commit is contained in:
Carl Schwan 2026-04-20 17:14:03 +02:00 committed by GitHub
commit 5a728a4fb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 64 additions and 153 deletions

View file

@ -1950,8 +1950,7 @@ return array(
'OC\\OpenMetrics\\ExporterManager' => $baseDir . '/lib/private/OpenMetrics/ExporterManager.php',
'OC\\OpenMetrics\\Exporters\\ActiveSessions' => $baseDir . '/lib/private/OpenMetrics/Exporters/ActiveSessions.php',
'OC\\OpenMetrics\\Exporters\\ActiveUsers' => $baseDir . '/lib/private/OpenMetrics/Exporters/ActiveUsers.php',
'OC\\OpenMetrics\\Exporters\\AppsCount' => $baseDir . '/lib/private/OpenMetrics/Exporters/AppsCount.php',
'OC\\OpenMetrics\\Exporters\\AppsInfo' => $baseDir . '/lib/private/OpenMetrics/Exporters/AppsInfo.php',
'OC\\OpenMetrics\\Exporters\\AppEnabled' => $baseDir . '/lib/private/OpenMetrics/Exporters/AppEnabled.php',
'OC\\OpenMetrics\\Exporters\\Cached' => $baseDir . '/lib/private/OpenMetrics/Exporters/Cached.php',
'OC\\OpenMetrics\\Exporters\\FilesByType' => $baseDir . '/lib/private/OpenMetrics/Exporters/FilesByType.php',
'OC\\OpenMetrics\\Exporters\\InstanceInfo' => $baseDir . '/lib/private/OpenMetrics/Exporters/InstanceInfo.php',

View file

@ -1991,8 +1991,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\OpenMetrics\\ExporterManager' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/ExporterManager.php',
'OC\\OpenMetrics\\Exporters\\ActiveSessions' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/ActiveSessions.php',
'OC\\OpenMetrics\\Exporters\\ActiveUsers' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/ActiveUsers.php',
'OC\\OpenMetrics\\Exporters\\AppsCount' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/AppsCount.php',
'OC\\OpenMetrics\\Exporters\\AppsInfo' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/AppsInfo.php',
'OC\\OpenMetrics\\Exporters\\AppEnabled' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/AppEnabled.php',
'OC\\OpenMetrics\\Exporters\\Cached' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/Cached.php',
'OC\\OpenMetrics\\Exporters\\FilesByType' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/FilesByType.php',
'OC\\OpenMetrics\\Exporters\\InstanceInfo' => __DIR__ . '/../../..' . '/lib/private/OpenMetrics/Exporters/InstanceInfo.php',

View file

@ -12,8 +12,7 @@ namespace OC\OpenMetrics;
use Generator;
use OC\OpenMetrics\Exporters\ActiveSessions;
use OC\OpenMetrics\Exporters\ActiveUsers;
use OC\OpenMetrics\Exporters\AppsCount;
use OC\OpenMetrics\Exporters\AppsInfo;
use OC\OpenMetrics\Exporters\AppEnabled;
use OC\OpenMetrics\Exporters\FilesByType;
use OC\OpenMetrics\Exporters\InstanceInfo;
use OC\OpenMetrics\Exporters\LogLevel;
@ -44,8 +43,7 @@ class ExporterManager {
$exporters = [
// Basic exporters
InstanceInfo::class,
AppsInfo::class,
AppsCount::class,
AppEnabled::class,
Maintenance::class,
LogLevel::class,

View file

@ -17,9 +17,9 @@ use OCP\OpenMetrics\MetricType;
use Override;
/**
* Export information about enabled applications
* Export information about installed applications
*/
class AppsInfo implements IMetricFamily {
class AppEnabled implements IMetricFamily {
public function __construct(
private IAppManager $appManager,
) {
@ -27,12 +27,12 @@ class AppsInfo implements IMetricFamily {
#[Override]
public function name(): string {
return 'apps_info';
return 'app_enabled';
}
#[Override]
public function type(): MetricType {
return MetricType::info;
return MetricType::gauge;
}
#[Override]
@ -42,15 +42,15 @@ class AppsInfo implements IMetricFamily {
#[Override]
public function help(): string {
return 'Enabled applications in Nextcloud';
return 'Information about the installed Nextcloud applications';
}
#[Override]
public function metrics(): Generator {
$apps = [];
foreach ($this->appManager->getAppInstalledVersions(true) as $appId => $version) {
$apps[str_replace('-', '_', $appId)] = $version;
$enabledApps = $this->appManager->getEnabledApps();
foreach ($this->appManager->getAppInstalledVersions(false) as $appId => $version) {
yield new Metric(in_array($appId, $enabledApps, true) ? 1 : 0, ['app_id' => $appId, 'version' => $version]);
}
yield new Metric(1, $apps, time());
}
}

View file

@ -1,62 +0,0 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\OpenMetrics\Exporters;
use Generator;
use OCP\App\IAppManager;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use Override;
/**
* Export statistics about apps
*/
class AppsCount implements IMetricFamily {
public function __construct(
private IAppManager $appManager,
) {
}
#[Override]
public function name(): string {
return 'installed_applications';
}
#[Override]
public function type(): MetricType {
return MetricType::gauge;
}
#[Override]
public function unit(): string {
return 'applications';
}
#[Override]
public function help(): string {
return 'Number of applications installed in Nextcloud';
}
#[Override]
public function metrics(): Generator {
$installedAppsCount = count($this->appManager->getAppInstalledVersions(false));
$enabledAppsCount = count($this->appManager->getEnabledApps());
$disabledAppsCount = $installedAppsCount - $enabledAppsCount;
yield new Metric(
$disabledAppsCount,
['status' => 'disabled'],
);
yield new Metric(
$enabledAppsCount,
['status' => 'enabled'],
);
}
}

View file

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\OpenMetrics\Exporters;
use OC\OpenMetrics\Exporters\AppEnabled;
use OCP\App\IAppManager;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
class AppEnabledTest extends ExporterTestCase {
private IAppManager $appManager;
private array $appList = [
'appA' => '0.1.2',
'appB' => '1.2.3 beta 4',
'appC' => '6.2.1',
'appD' => '3.141.5',
];
private array $installedAppsList = [
'appA', 'appB', 'appD'
];
protected function getExporter():IMetricFamily {
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->method('getAppInstalledVersions')
->with(false)
->willReturn($this->appList);
$this->appManager->method('getEnabledApps')
->willReturn($this->installedAppsList);
return new AppEnabled($this->appManager);
}
public function testMetrics(): void {
$this->assertCount(4, $this->metrics);
foreach ($this->appList as $appId => $appVersion) {
$metricForApp = array_find($this->metrics, function (Metric $metric) use ($appId) {
return $metric->label('app_id') === $appId;
});
$expectedMetricValue = in_array($appId, $this->installedAppsList) ? 1 : 0;
$this->assertEquals($expectedMetricValue, $metricForApp->value);
$this->assertSame(['app_id' => $appId, 'version' => $appVersion], $metricForApp->labels);
}
}
}

View file

@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\OpenMetrics\Exporters;
use OC\OpenMetrics\Exporters\AppsCount;
use OCP\App\IAppManager;
use OCP\OpenMetrics\IMetricFamily;
class AppsCountTest extends ExporterTestCase {
private IAppManager $appManager;
protected function getExporter():IMetricFamily {
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->method('getAppInstalledVersions')
->with(false)
->willReturn(['app1', 'app2', 'app3', 'app4', 'app5']);
$this->appManager->method('getEnabledApps')
->willReturn(['app1', 'app2', 'app3']);
return new AppsCount($this->appManager);
}
public function testMetrics(): void {
foreach ($this->metrics as $metric) {
$expectedValue = match ($metric->label('status')) {
'disabled' => 2,
'enabled' => 3,
};
$this->assertEquals($expectedValue, $metric->value);
}
}
}

View file

@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\OpenMetrics\Exporters;
use OC\OpenMetrics\Exporters\AppsInfo;
use OCP\App\IAppManager;
use OCP\OpenMetrics\IMetricFamily;
class AppsInfoTest extends ExporterTestCase {
private IAppManager $appManager;
private array $appList = [
'appA' => '0.1.2',
'appB' => '1.2.3 beta 4',
];
protected function getExporter():IMetricFamily {
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->method('getAppInstalledVersions')
->with(true)
->willReturn($this->appList);
return new AppsInfo($this->appManager);
}
public function testMetrics(): void {
$this->assertCount(1, $this->metrics);
$metric = array_pop($this->metrics);
$this->assertSame($this->appList, $metric->labels);
}
}