Skip to content

Commit

Permalink
perf: Log requests using high amount of memory as warning
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Knorr <[email protected]>
  • Loading branch information
juliusknorr committed Feb 13, 2025
1 parent f2b25a4 commit d31bc88
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -828,6 +829,21 @@ public static function init(): void {
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']);
}
});
}

/**
Expand Down

0 comments on commit d31bc88

Please sign in to comment.