summaryrefslogtreecommitdiffstats
path: root/lib/private/AppFramework
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-01-19 23:30:34 +0100
committerCarl Schwan <carl@carlschwan.eu>2022-04-04 10:28:26 +0200
commit7d272c54d013538746d6731097ec37f360effb5d (patch)
treed754c4184926ea8011bd2b0fe276adebaf4082f6 /lib/private/AppFramework
parentcf4c77e064fd52d891bc842d78431cceb2f34952 (diff)
downloadnextcloud-server-7d272c54d013538746d6731097ec37f360effb5d.tar.gz
nextcloud-server-7d272c54d013538746d6731097ec37f360effb5d.zip
Add a built-in profiler inside Nextcloud
The webui is provided by a seperate application named profiler Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib/private/AppFramework')
-rw-r--r--lib/private/AppFramework/App.php33
-rw-r--r--lib/private/AppFramework/Utility/SimpleContainer.php8
2 files changed, 34 insertions, 7 deletions
diff --git a/lib/private/AppFramework/App.php b/lib/private/AppFramework/App.php
index 6c2f905afa5..feebb32d5bc 100644
--- a/lib/private/AppFramework/App.php
+++ b/lib/private/AppFramework/App.php
@@ -34,11 +34,16 @@ namespace OC\AppFramework;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Http\Dispatcher;
use OC\AppFramework\Http\Request;
+use OC\Diagnostics\EventLogger;
+use OCP\Profiler\IProfiler;
+use OC\Profiler\RoutingDataCollector;
+use OCP\AppFramework\QueryException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\ICallbackResponse;
use OCP\AppFramework\Http\IOutput;
-use OCP\AppFramework\QueryException;
+use OCP\Diagnostics\IEventLogger;
use OCP\HintException;
+use OCP\IConfig;
use OCP\IRequest;
/**
@@ -114,20 +119,30 @@ class App {
* @throws HintException
*/
public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) {
+ /** @var IProfiler $profiler */
+ $profiler = $container->get(IProfiler::class);
+ $config = $container->get(IConfig::class);
+ // Disable profiler on the profiler UI
+ $profiler->setEnabled($profiler->isEnabled() && !is_null($urlParams) && isset($urlParams['_route']) && !str_starts_with($urlParams['_route'], 'profiler.'));
+ if ($profiler->isEnabled()) {
+ \OC::$server->get(IEventLogger::class)->activate();
+ $profiler->add(new RoutingDataCollector($container['AppName'], $controllerName, $methodName));
+ }
+
if (!is_null($urlParams)) {
/** @var Request $request */
- $request = $container->query(IRequest::class);
+ $request = $container->get(IRequest::class);
$request->setUrlParameters($urlParams);
} elseif (isset($container['urlParams']) && !is_null($container['urlParams'])) {
/** @var Request $request */
- $request = $container->query(IRequest::class);
+ $request = $container->get(IRequest::class);
$request->setUrlParameters($container['urlParams']);
}
$appName = $container['AppName'];
// first try $controllerName then go for \OCA\AppName\Controller\$controllerName
try {
- $controller = $container->query($controllerName);
+ $controller = $container->get($controllerName);
} catch (QueryException $e) {
if (strpos($controllerName, '\\Controller\\') !== false) {
// This is from a global registered app route that is not enabled.
@@ -158,6 +173,16 @@ class App {
$io = $container[IOutput::class];
+ if ($profiler->isEnabled()) {
+ /** @var EventLogger $eventLogger */
+ $eventLogger = $container->get(IEventLogger::class);
+ $eventLogger->end('runtime');
+ $profile = $profiler->collect($container->get(IRequest::class), $response);
+ $profiler->saveProfile($profile);
+ $io->setHeader('X-Debug-Token:' . $profile->getToken());
+ $io->setHeader('Server-Timing: token;desc="' . $profile->getToken() . '"');
+ }
+
if (!is_null($httpHeaders)) {
$io->setHeader($httpHeaders);
}
diff --git a/lib/private/AppFramework/Utility/SimpleContainer.php b/lib/private/AppFramework/Utility/SimpleContainer.php
index 598c66b6aba..429382aa223 100644
--- a/lib/private/AppFramework/Utility/SimpleContainer.php
+++ b/lib/private/AppFramework/Utility/SimpleContainer.php
@@ -38,6 +38,7 @@ use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
+use ReflectionNamedType;
use function class_exists;
/**
@@ -78,12 +79,13 @@ class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
$resolveName = $parameter->getName();
// try to find out if it is a class or a simple parameter
- if ($parameterType !== null && !$parameterType->isBuiltin()) {
+ if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
$resolveName = $parameterType->getName();
}
try {
- $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin();
+ $builtIn = $parameter->hasType() && ($parameter->getType() instanceof ReflectionNamedType)
+ && $parameter->getType()->isBuiltin();
return $this->query($resolveName, !$builtIn);
} catch (QueryException $e) {
// Service not found, use the default value when available
@@ -91,7 +93,7 @@ class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
return $parameter->getDefaultValue();
}
- if ($parameterType !== null && !$parameterType->isBuiltin()) {
+ if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
$resolveName = $parameter->getName();
try {
return $this->query($resolveName);