Skip to content

Commit d31bc88

Browse files
committed
perf: Log requests using high amount of memory as warning
Signed-off-by: Julius Knorr <[email protected]>
1 parent f2b25a4 commit d31bc88

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/base.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\Server;
1919
use OCP\Share;
2020
use OCP\User\Events\UserChangedEvent;
21+
use OCP\Util;
2122
use Psr\Log\LoggerInterface;
2223
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
2324
use function OCP\Log\logger;
@@ -828,6 +829,21 @@ public static function init(): void {
828829
register_shutdown_function(function () use ($eventLogger) {
829830
$eventLogger->end('request');
830831
});
832+
833+
register_shutdown_function(function () {
834+
$memoryPeak = memory_get_peak_usage();
835+
$logLevel = match (true) {
836+
$memoryPeak > 500_000_000 => ILogger::FATAL,
837+
$memoryPeak > 400_000_000 => ILogger::ERROR,
838+
$memoryPeak > 300_000_000 => ILogger::WARN,
839+
default => null,
840+
};
841+
if ($logLevel !== null) {
842+
$message = 'Request used more than 300 MB of RAM: ' . Util::humanFileSize($memoryPeak);
843+
$logger = Server::get(LoggerInterface::class);
844+
$logger->log($logLevel, $message, ['app' => 'core']);
845+
}
846+
});
831847
}
832848

833849
/**

0 commit comments

Comments
 (0)