Profiler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 OC\SystemConfig;
  27. use OCP\AppFramework\Http\Response;
  28. use OCP\DataCollector\IDataCollector;
  29. use OCP\Profiler\IProfile;
  30. use OCP\Profiler\IProfiler;
  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. if ($this->storage) {
  53. return $this->storage->read($token);
  54. } else {
  55. return null;
  56. }
  57. }
  58. public function saveProfile(IProfile $profile): bool {
  59. if ($this->storage) {
  60. return $this->storage->write($profile);
  61. } else {
  62. return false;
  63. }
  64. }
  65. public function collect(Request $request, Response $response): IProfile {
  66. $profile = new Profile($request->getId());
  67. $profile->setTime(time());
  68. $profile->setUrl($request->getRequestUri());
  69. $profile->setMethod($request->getMethod());
  70. $profile->setStatusCode($response->getStatus());
  71. foreach ($this->dataCollectors as $dataCollector) {
  72. $dataCollector->collect($request, $response, null);
  73. // We clone for subrequests
  74. $profile->addCollector(clone $dataCollector);
  75. }
  76. return $profile;
  77. }
  78. /**
  79. * @return array[]
  80. */
  81. public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end,
  82. ?string $statusCode = null): array {
  83. if ($this->storage) {
  84. return $this->storage->find($url, $limit, $method, $start, $end, $statusCode);
  85. } else {
  86. return [];
  87. }
  88. }
  89. public function dataProviders(): array {
  90. return array_keys($this->dataCollectors);
  91. }
  92. public function isEnabled(): bool {
  93. return $this->enabled;
  94. }
  95. public function setEnabled(bool $enabled): void {
  96. $this->enabled = $enabled;
  97. }
  98. public function clear(): void {
  99. $this->storage->purge();
  100. }
  101. }