mirror of
https://github.com/nextcloud/server.git
synced 2026-05-20 17:15:46 -04:00
Expose a `/metrics` endpoint with some basic metrics Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
58 lines
1.1 KiB
PHP
58 lines
1.1 KiB
PHP
<?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\ICache;
|
|
use OCP\ICacheFactory;
|
|
use OCP\OpenMetrics\IMetricFamily;
|
|
use Override;
|
|
|
|
/**
|
|
* Cached metrics
|
|
*/
|
|
abstract class Cached implements IMetricFamily {
|
|
private readonly ICache $cache;
|
|
|
|
public function __construct(
|
|
ICacheFactory $cacheFactory,
|
|
) {
|
|
$this->cache = $cacheFactory->createDistributed('openmetrics');
|
|
}
|
|
|
|
/**
|
|
* Number of seconds to keep the results
|
|
*/
|
|
abstract public function getTTL(): int;
|
|
|
|
/**
|
|
* Actually gather the metrics
|
|
*
|
|
* @see metrics
|
|
*/
|
|
abstract public function gatherMetrics(): Generator;
|
|
|
|
#[Override]
|
|
public function metrics(): Generator {
|
|
$cacheKey = static::class;
|
|
if ($data = $this->cache->get($cacheKey)) {
|
|
yield from unserialize($data);
|
|
return;
|
|
}
|
|
|
|
$data = [];
|
|
foreach ($this->gatherMetrics() as $metric) {
|
|
yield $metric;
|
|
$data[] = $metric;
|
|
}
|
|
|
|
$this->cache->set($cacheKey, serialize($data), $this->getTTL());
|
|
}
|
|
}
|