feat(comments): add basic OpenMetrics exporter

Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
This commit is contained in:
Benjamin Gaussorgues 2025-08-21 10:52:03 +02:00
parent c57c4843e8
commit 47de164946
No known key found for this signature in database
4 changed files with 58 additions and 0 deletions

View file

@ -38,6 +38,10 @@
</providers>
</activity>
<openmetrics>
<exporter>OCA\Comments\OpenMetrics\CommentsCountMetric</exporter>
</openmetrics>
<collaboration>
<plugins>
<plugin type="autocomplete-sort">OCA\Comments\Collaboration\CommentersSorter</plugin>

View file

@ -22,5 +22,6 @@ return array(
'OCA\\Comments\\MaxAutoCompleteResultsInitialState' => $baseDir . '/../lib/MaxAutoCompleteResultsInitialState.php',
'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
'OCA\\Comments\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
'OCA\\Comments\\OpenMetrics\\CommentsCountMetric' => $baseDir . '/../lib/OpenMetrics/CommentsCountMetric.php',
'OCA\\Comments\\Search\\CommentsSearchProvider' => $baseDir . '/../lib/Search/CommentsSearchProvider.php',
);

View file

@ -37,6 +37,7 @@ class ComposerStaticInitComments
'OCA\\Comments\\MaxAutoCompleteResultsInitialState' => __DIR__ . '/..' . '/../lib/MaxAutoCompleteResultsInitialState.php',
'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
'OCA\\Comments\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
'OCA\\Comments\\OpenMetrics\\CommentsCountMetric' => __DIR__ . '/..' . '/../lib/OpenMetrics/CommentsCountMetric.php',
'OCA\\Comments\\Search\\CommentsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/CommentsSearchProvider.php',
);

View file

@ -0,0 +1,52 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Comments\OpenMetrics;
use Generator;
use OC\DB\Connection;
use OCP\OpenMetrics\IMetricFamily;
use OCP\OpenMetrics\Metric;
use OCP\OpenMetrics\MetricType;
use Override;
class CommentsCountMetric implements IMetricFamily {
public function __construct(
private Connection $connection,
) {
}
#[Override]
public function name(): string {
return 'comments';
}
#[Override]
public function type(): MetricType {
return MetricType::gauge;
}
#[Override]
public function unit(): string {
return 'comments';
}
#[Override]
public function help(): string {
return 'Number of comments';
}
#[Override]
public function metrics(): Generator {
$qb = $this->connection->getQueryBuilder();
$result = $qb->select($qb->func()->count())
->from('comments')
->where($qb->expr()->eq('verb', $qb->expr()->literal('comment')))
->executeQuery();
yield new Metric($result->fetchOne(), [], time());
}
}