|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2024 Julius Knorr <jus@bitgrid.net> |
| 7 | + * |
| 8 | + * @author 2024 Julius Knorr <jus@bitgrid.net> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\Sentry\DAV; |
| 27 | + |
| 28 | +use Sabre\DAV\Server; |
| 29 | +use Sabre\Dav\ServerPlugin; |
| 30 | +use Sentry\SentrySdk; |
| 31 | +use Sentry\Tracing\SpanStatus; |
| 32 | +use Sentry\Tracing\Transaction; |
| 33 | +use Sentry\Tracing\TransactionContext; |
| 34 | +use function Sentry\startTransaction; |
| 35 | + |
| 36 | +class PerformanceMonitoringPlugin extends ServerPlugin { |
| 37 | + |
| 38 | + private Server $server; |
| 39 | + private ?Transaction $transaction = null; |
| 40 | + |
| 41 | + public function initialize(Server $server) { |
| 42 | + $this->server = $server; |
| 43 | + |
| 44 | + $this->server->on('method:*', [$this, 'beforeMethod']); |
| 45 | + $this->server->on('afterMethod:*', [$this, 'afterMethod']); |
| 46 | + } |
| 47 | + |
| 48 | + public function beforeMethod($method) { |
| 49 | + $partParts = explode('/', $method->getPath()); |
| 50 | + $serviceName = array_shift($partParts); |
| 51 | + $name = $method->getMethod() . '::' . $serviceName; |
| 52 | + $transactionContext = new TransactionContext(); |
| 53 | + $transactionContext->setName($name); |
| 54 | + $transactionContext->setOp('http.request'); |
| 55 | + $this->transaction = startTransaction($transactionContext); |
| 56 | + SentrySdk::getCurrentHub()->setSpan($this->transaction); |
| 57 | + } |
| 58 | + |
| 59 | + public function afterMethod($method) { |
| 60 | + if ($this->transaction !== null) { |
| 61 | + $this->transaction->setStatus(SpanStatus::ok()); |
| 62 | + $this->transaction->finish(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments