Merge pull request #48738 from nextcloud/perf/log-high-memory-requests

perf: Log requests using high amount of memory as warning
pull/50784/head
Julius Knorr 2025-02-13 19:50:32 +07:00 committed by GitHub
commit 8c69bf1219
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 0 deletions

@ -18,6 +18,7 @@ use OCP\Security\Bruteforce\IThrottler;
use OCP\Server;
use OCP\Share;
use OCP\User\Events\UserChangedEvent;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use function OCP\Log\logger;
@ -828,6 +829,21 @@ class OC {
register_shutdown_function(function () use ($eventLogger) {
$eventLogger->end('request');
});
register_shutdown_function(function () {
$memoryPeak = memory_get_peak_usage();
$logLevel = match (true) {
$memoryPeak > 500_000_000 => ILogger::FATAL,
$memoryPeak > 400_000_000 => ILogger::ERROR,
$memoryPeak > 300_000_000 => ILogger::WARN,
default => null,
};
if ($logLevel !== null) {
$message = 'Request used more than 300 MB of RAM: ' . Util::humanFileSize($memoryPeak);
$logger = Server::get(LoggerInterface::class);
$logger->log($logLevel, $message, ['app' => 'core']);
}
});
}
/**