nextcloud/lib/private/OpenMetrics/Exporters/Cached.php
Benjamin Gaussorgues c57c4843e8
feat(openmetrics): introduce OpenMetrics exporter
Expose a `/metrics` endpoint with some basic metrics

Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
2026-01-07 09:38:13 +01:00

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());
}
}