Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions core/Controller/OpenMetricsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ private function generate(): \Generator {

$elapsed = (string)(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
yield <<<SUMMARY
# TYPE nextcloud_exporter_duration gauge
# UNIT nextcloud_exporter_duration seconds
# HELP nextcloud_exporter_duration Exporter run time
nextcloud_exporter_duration $elapsed

# TYPE nextcloud_exporter_run_seconds gauge
# UNIT nextcloud_exporter_run_seconds seconds
# HELP nextcloud_exporter_run_seconds Exporter run time
nextcloud_exporter_run_seconds $elapsed
# EOF

SUMMARY;
Expand All @@ -112,7 +111,6 @@ private function formatFamily(IMetricFamily $family): string {
}
$output .= "\n";
}
$output .= "\n";

return $output;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/private/OpenMetrics/Exporters/AppsCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(

#[Override]
public function name(): string {
return 'apps_count';
return 'installed_applications';
}

#[Override]
Expand All @@ -42,7 +42,7 @@ public function unit(): string {

#[Override]
public function help(): string {
return 'Number of apps in Nextcloud';
return 'Number of applications installed in Nextcloud';
}

#[Override]
Expand Down
2 changes: 1 addition & 1 deletion lib/private/OpenMetrics/Exporters/RunningJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(

#[Override]
public function name(): string {
return 'jobs_running';
return 'running_jobs';
}

#[Override]
Expand Down
31 changes: 26 additions & 5 deletions tests/Core/Controller/OpenMetricsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

namespace Tests\Core\Controller;

use Generator;
use OC\Core\Controller\OpenMetricsController;
use OC\OpenMetrics\ExporterManager;
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StreamTraversableResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
Expand All @@ -34,10 +38,23 @@ protected function setUp(): void {
->willReturn('192.168.1.1');
$this->config = $this->createMock(IConfig::class);
$this->exporterManager = $this->createMock(ExporterManager::class);
$this->exporterManager->method('export')->willReturnCallback([$this, 'getFakeMetrics']);
$this->logger = $this->createMock(LoggerInterface::class);
$this->controller = new OpenMetricsController('core', $this->request, $this->config, $this->exporterManager, $this->logger);
}

public function getFakeMetrics(): Generator {
$metric = $this->createMock(IMetricFamily::class);
$metric->method('type')->willReturn(MetricType::gauge);
$metric->method('unit')->willReturn('fake');
$metric->method('name')->willReturn('fake_count');
$metric->method('help')->willReturn('A fake count used for tests');
$metric->method('metrics')->willReturnCallback(function () {
yield new Metric(42, ['type' => 'used']);
yield new Metric(24, ['type' => 'unused']);
});
yield $metric;
}
public function testGetMetrics(): void {
$output = $this->createMock(IOutput::class);
$fullOutput = '';
Expand All @@ -54,11 +71,15 @@ public function testGetMetrics(): void {
$this->assertEquals('200', $response->getStatus());
$this->assertEquals('application/openmetrics-text; version=1.0.0; charset=utf-8', $response->getHeaders()['Content-Type']);
$expected = <<<EXPECTED
# TYPE nextcloud_exporter_duration gauge
# UNIT nextcloud_exporter_duration seconds
# HELP nextcloud_exporter_duration Exporter run time
nextcloud_exporter_duration %f

# TYPE nextcloud_fake_count gauge
# UNIT nextcloud_fake_count fake
# HELP nextcloud_fake_count A fake count used for tests
nextcloud_fake_count{type="used"} 42
nextcloud_fake_count{type="unused"} 24
# TYPE nextcloud_exporter_run_seconds gauge
# UNIT nextcloud_exporter_run_seconds seconds
# HELP nextcloud_exporter_run_seconds Exporter run time
nextcloud_exporter_run_seconds %f
# EOF

EXPECTED;
Expand Down
21 changes: 19 additions & 2 deletions tests/lib/OpenMetrics/Exporters/ExporterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@ protected function setUp(): void {
}

public function testNotEmptyData(): void {
$this->assertNotEmpty($this->exporter->name());
$this->assertNotEmpty($this->metrics);
}

public function testValidNames(): void {
public function testValidExporterName(): void {
$exporterName = $this->exporter->name();
$this->assertMatchesRegularExpression('/^[a-z_:][a-z0-9_:]*$/i', $exporterName, );

$unit = $this->exporter->unit();
if ($unit === '') {
return;
}
// Unit name must follow metric name format
$this->assertMatchesRegularExpression('/^[a-z_:][a-z0-9_:]*$/i', $unit);
// Unit name must be a suffix in exporter name
$this->assertMatchesRegularExpression(
'/(^|_)' . $unit . '$/',
$exporterName,
'Metric name "' . $exporterName . '" must contains unit "' . $unit . '" as a suffix',
);
}

public function testValidLabelKey(): void {
$labelNames = [];
foreach ($this->metrics as $metric) {
foreach ($metric->labels as $label => $value) {
Expand Down
Loading