You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Profiler.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2021 Carl Schwan <carl@carlschwan.eu>
  5. *
  6. * @author Carl Schwan <carl@carlschwan.eu>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Profiler;
  25. use OC\AppFramework\Http\Request;
  26. use OCP\AppFramework\Http\Response;
  27. use OCP\DataCollector\IDataCollector;
  28. use OCP\Profiler\IProfiler;
  29. use OCP\Profiler\IProfile;
  30. use OC\SystemConfig;
  31. class Profiler implements IProfiler {
  32. /** @var array<string, IDataCollector> */
  33. private array $dataCollectors = [];
  34. private ?FileProfilerStorage $storage = null;
  35. private bool $enabled = false;
  36. public function __construct(SystemConfig $config) {
  37. $this->enabled = $config->getValue('profiler', false);
  38. if ($this->enabled) {
  39. $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/profiler');
  40. }
  41. }
  42. public function add(IDataCollector $dataCollector): void {
  43. $this->dataCollectors[$dataCollector->getName()] = $dataCollector;
  44. }
  45. public function loadProfileFromResponse(Response $response): ?IProfile {
  46. if (!$token = $response->getHeaders()['X-Debug-Token']) {
  47. return null;
  48. }
  49. return $this->loadProfile($token);
  50. }
  51. public function loadProfile(string $token): ?IProfile {
  52. return $this->storage->read($token);
  53. }
  54. public function saveProfile(IProfile $profile): bool {
  55. return $this->storage->write($profile);
  56. }
  57. public function collect(Request $request, Response $response): IProfile {
  58. $profile = new Profile($request->getId());
  59. $profile->setTime(time());
  60. $profile->setUrl($request->getRequestUri());
  61. $profile->setMethod($request->getMethod());
  62. $profile->setStatusCode($response->getStatus());
  63. foreach ($this->dataCollectors as $dataCollector) {
  64. $dataCollector->collect($request, $response, null);
  65. // We clone for subrequests
  66. $profile->addCollector(clone $dataCollector);
  67. }
  68. return $profile;
  69. }
  70. /**
  71. * @return array[]
  72. */
  73. public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end,
  74. string $statusCode = null): array {
  75. return $this->storage->find($url, $limit, $method, $start, $end, $statusCode);
  76. }
  77. public function dataProviders(): array {
  78. return array_keys($this->dataCollectors);
  79. }
  80. public function isEnabled(): bool {
  81. return $this->enabled;
  82. }
  83. public function setEnabled(bool $enabled): void {
  84. $this->enabled = $enabled;
  85. }
  86. public function clear(): void {
  87. $this->storage->purge();
  88. }
  89. }