Profiler.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Profiler;
  8. use OC\AppFramework\Http\Request;
  9. use OC\SystemConfig;
  10. use OCP\AppFramework\Http\Response;
  11. use OCP\DataCollector\IDataCollector;
  12. use OCP\Profiler\IProfile;
  13. use OCP\Profiler\IProfiler;
  14. class Profiler implements IProfiler {
  15. /** @var array<string, IDataCollector> */
  16. private array $dataCollectors = [];
  17. private ?FileProfilerStorage $storage = null;
  18. private bool $enabled = false;
  19. public function __construct(SystemConfig $config) {
  20. $this->enabled = $config->getValue('profiler', false);
  21. if ($this->enabled) {
  22. $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/__profiler');
  23. }
  24. }
  25. public function add(IDataCollector $dataCollector): void {
  26. $this->dataCollectors[$dataCollector->getName()] = $dataCollector;
  27. }
  28. public function loadProfileFromResponse(Response $response): ?IProfile {
  29. if (!$token = $response->getHeaders()['X-Debug-Token']) {
  30. return null;
  31. }
  32. return $this->loadProfile($token);
  33. }
  34. public function loadProfile(string $token): ?IProfile {
  35. if ($this->storage) {
  36. return $this->storage->read($token);
  37. } else {
  38. return null;
  39. }
  40. }
  41. public function saveProfile(IProfile $profile): bool {
  42. if ($this->storage) {
  43. return $this->storage->write($profile);
  44. } else {
  45. return false;
  46. }
  47. }
  48. public function collect(Request $request, Response $response): IProfile {
  49. $profile = new Profile($request->getId());
  50. $profile->setTime(time());
  51. $profile->setUrl($request->getRequestUri());
  52. $profile->setMethod($request->getMethod());
  53. $profile->setStatusCode($response->getStatus());
  54. foreach ($this->dataCollectors as $dataCollector) {
  55. $dataCollector->collect($request, $response, null);
  56. // We clone for subrequests
  57. $profile->addCollector(clone $dataCollector);
  58. }
  59. return $profile;
  60. }
  61. /**
  62. * @return array[]
  63. */
  64. public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end,
  65. ?string $statusCode = null): array {
  66. if ($this->storage) {
  67. return $this->storage->find($url, $limit, $method, $start, $end, $statusCode);
  68. } else {
  69. return [];
  70. }
  71. }
  72. public function dataProviders(): array {
  73. return array_keys($this->dataCollectors);
  74. }
  75. public function isEnabled(): bool {
  76. return $this->enabled;
  77. }
  78. public function setEnabled(bool $enabled): void {
  79. $this->enabled = $enabled;
  80. }
  81. public function clear(): void {
  82. $this->storage->purge();
  83. }
  84. }