diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-04-04 13:56:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 13:56:37 +0300 |
commit | 135bdb3d5830672214149fa0b75fadd29b20b844 (patch) | |
tree | f2c1bc68c733829c90090f71d712f78efbc77083 /apps/user_ldap | |
parent | 498d3aea060ed496e2b5351108718e198b021d00 (diff) | |
parent | 7d272c54d013538746d6731097ec37f360effb5d (diff) |
Merge pull request #30823 from nextcloud/work/profiler
Built-in profiler
This adds the required API for collecting information about requests. This information
can then be displayed with the new 'profiler' app.
Diffstat (limited to 'apps/user_ldap')
-rw-r--r-- | apps/user_ldap/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/user_ldap/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | apps/user_ldap/lib/DataCollector/LdapDataCollector.php | 50 | ||||
-rw-r--r-- | apps/user_ldap/lib/LDAP.php | 32 |
4 files changed, 75 insertions, 9 deletions
diff --git a/apps/user_ldap/composer/composer/autoload_classmap.php b/apps/user_ldap/composer/composer/autoload_classmap.php index 12ede37a941..005f1a70a4e 100644 --- a/apps/user_ldap/composer/composer/autoload_classmap.php +++ b/apps/user_ldap/composer/composer/autoload_classmap.php @@ -26,6 +26,7 @@ return array( 'OCA\\User_LDAP\\ConnectionFactory' => $baseDir . '/../lib/ConnectionFactory.php', 'OCA\\User_LDAP\\Controller\\ConfigAPIController' => $baseDir . '/../lib/Controller/ConfigAPIController.php', 'OCA\\User_LDAP\\Controller\\RenewPasswordController' => $baseDir . '/../lib/Controller/RenewPasswordController.php', + 'OCA\\User_LDAP\\DataCollector\\LdapDataCollector' => $baseDir . '/../lib/DataCollector/LdapDataCollector.php', 'OCA\\User_LDAP\\Events\\GroupBackendRegistered' => $baseDir . '/../lib/Events/GroupBackendRegistered.php', 'OCA\\User_LDAP\\Events\\UserBackendRegistered' => $baseDir . '/../lib/Events/UserBackendRegistered.php', 'OCA\\User_LDAP\\Exceptions\\AttributeNotSet' => $baseDir . '/../lib/Exceptions/AttributeNotSet.php', diff --git a/apps/user_ldap/composer/composer/autoload_static.php b/apps/user_ldap/composer/composer/autoload_static.php index ecf5e4167f6..f888b46ad9d 100644 --- a/apps/user_ldap/composer/composer/autoload_static.php +++ b/apps/user_ldap/composer/composer/autoload_static.php @@ -41,6 +41,7 @@ class ComposerStaticInitUser_LDAP 'OCA\\User_LDAP\\ConnectionFactory' => __DIR__ . '/..' . '/../lib/ConnectionFactory.php', 'OCA\\User_LDAP\\Controller\\ConfigAPIController' => __DIR__ . '/..' . '/../lib/Controller/ConfigAPIController.php', 'OCA\\User_LDAP\\Controller\\RenewPasswordController' => __DIR__ . '/..' . '/../lib/Controller/RenewPasswordController.php', + 'OCA\\User_LDAP\\DataCollector\\LdapDataCollector' => __DIR__ . '/..' . '/../lib/DataCollector/LdapDataCollector.php', 'OCA\\User_LDAP\\Events\\GroupBackendRegistered' => __DIR__ . '/..' . '/../lib/Events/GroupBackendRegistered.php', 'OCA\\User_LDAP\\Events\\UserBackendRegistered' => __DIR__ . '/..' . '/../lib/Events/UserBackendRegistered.php', 'OCA\\User_LDAP\\Exceptions\\AttributeNotSet' => __DIR__ . '/..' . '/../lib/Exceptions/AttributeNotSet.php', diff --git a/apps/user_ldap/lib/DataCollector/LdapDataCollector.php b/apps/user_ldap/lib/DataCollector/LdapDataCollector.php new file mode 100644 index 00000000000..cb61de96e37 --- /dev/null +++ b/apps/user_ldap/lib/DataCollector/LdapDataCollector.php @@ -0,0 +1,50 @@ +<?php declare(strict_types = 1); +/** + * @copyright 2022 Carl Schwan <carl@carlschwan.eu> + * + * @author Carl Schwan <carl@carlschwan.eu> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\User_LDAP\DataCollector; + +use OC\AppFramework\Http\Request; +use OCP\AppFramework\Http\Response; +use OCP\DataCollector\AbstractDataCollector; + +class LdapDataCollector extends AbstractDataCollector { + public function startLdapRequest(string $query, array $args): void { + $this->data[] = [ + 'start' => microtime(true), + 'query' => $query, + 'args' => $args, + 'end' => microtime(true), + ]; + } + + public function stopLastLdapRequest(): void { + $this->data[count($this->data) - 1]['end'] = microtime(true); + } + + public function getName(): string { + return 'ldap'; + } + + public function collect(Request $request, Response $response, \Throwable $exception = null): void { + } +} diff --git a/apps/user_ldap/lib/LDAP.php b/apps/user_ldap/lib/LDAP.php index 18a9476128d..3c579596941 100644 --- a/apps/user_ldap/lib/LDAP.php +++ b/apps/user_ldap/lib/LDAP.php @@ -14,6 +14,7 @@ * @author Robin McCorkell <robin@mccorkell.me.uk> * @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roger Szabo <roger.szabo@web.de> + * @author Carl Schwan <carl@carlschwan.eu> * * @license AGPL-3.0 * @@ -32,7 +33,9 @@ */ namespace OCA\User_LDAP; +use OCP\Profiler\IProfiler; use OC\ServerNotAvailableException; +use OCA\User_LDAP\DataCollector\LdapDataCollector; use OCA\User_LDAP\Exceptions\ConstraintViolationException; use OCA\User_LDAP\PagedResults\IAdapter; use OCA\User_LDAP\PagedResults\Php73; @@ -45,9 +48,18 @@ class LDAP implements ILDAPWrapper { /** @var IAdapter */ protected $pagedResultsAdapter; + private ?LdapDataCollector $dataCollector = null; + public function __construct(string $logFile = '') { $this->pagedResultsAdapter = new Php73(); $this->logFile = $logFile; + + /** @var IProfiler $profiler */ + $profiler = \OC::$server->get(IProfiler::class); + if ($profiler->isEnabled()) { + $this->dataCollector = new LdapDataCollector(); + $profiler->add($this->dataCollector); + } } /** @@ -295,24 +307,26 @@ class LDAP implements ILDAPWrapper { if ($this->isResultFalse($result)) { $this->postFunctionCall(); } + if ($this->dataCollector !== null) { + $this->dataCollector->stopLastLdapRequest(); + } return $result; } return null; } - /** - * @param string $functionName - * @param array $args - */ - private function preFunctionCall($functionName, $args) { + private function preFunctionCall(string $functionName, array $args): void { $this->curFunc = $functionName; $this->curArgs = $args; + if ($this->dataCollector !== null) { + $args = array_map(fn ($item) => (!$this->isResource($item) ? $item : '(resource)'), $this->curArgs); + + $this->dataCollector->startLdapRequest($this->curFunc, $args); + } + if ($this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) { - $args = array_reduce($this->curArgs, static function (array $carry, $item): array { - $carry[] = !is_resource($item) ? $item : '(resource)'; - return $carry; - }, []); + $args = array_map(fn ($item) => (!$this->isResource($item) ? $item : '(resource)'), $this->curArgs); file_put_contents( $this->logFile, $this->curFunc . '::' . json_encode($args) . "\n", |