|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Tracing; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Sentry\SentrySdk; |
| 8 | +use Sentry\State\Hub; |
| 9 | +use Sentry\State\Scope; |
| 10 | +use Sentry\Tracing\SpanContext; |
| 11 | +use Sentry\Tracing\TransactionContext; |
| 12 | + |
| 13 | +class Middleware |
| 14 | +{ |
| 15 | + /** |
| 16 | + * The current active transaction. |
| 17 | + * |
| 18 | + * @var \Sentry\Tracing\Transaction|null |
| 19 | + */ |
| 20 | + protected $transaction; |
| 21 | + |
| 22 | + /** |
| 23 | + * Handle an incoming request. |
| 24 | + * |
| 25 | + * @param \Illuminate\Http\Request $request |
| 26 | + * @param \Closure $next |
| 27 | + * |
| 28 | + * @return mixed |
| 29 | + */ |
| 30 | + public function handle($request, Closure $next) |
| 31 | + { |
| 32 | + if (app()->bound('sentry')) { |
| 33 | + $this->startTransaction($request, app('sentry')); |
| 34 | + } |
| 35 | + |
| 36 | + return $next($request); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Handle the application termination. |
| 41 | + * |
| 42 | + * @param \Illuminate\Http\Request $request |
| 43 | + * @param \Illuminate\Http\Response $response |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function terminate($request, $response): void |
| 48 | + { |
| 49 | + if ($this->transaction !== null && app()->bound('sentry')) { |
| 50 | + $this->transaction->finish(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private function startTransaction(Request $request, Hub $sentry): void |
| 55 | + { |
| 56 | + $path = '/' . ltrim($request->path(), '/'); |
| 57 | + $fallbackTime = microtime(true); |
| 58 | + $sentryTraceHeader = $request->header('sentry-trace'); |
| 59 | + |
| 60 | + $context = $sentryTraceHeader |
| 61 | + ? TransactionContext::fromTraceparent($sentryTraceHeader) |
| 62 | + : new TransactionContext; |
| 63 | + |
| 64 | + $context->op = 'http.server'; |
| 65 | + $context->name = $path; |
| 66 | + $context->data = [ |
| 67 | + 'url' => $path, |
| 68 | + 'method' => strtoupper($request->method()), |
| 69 | + ]; |
| 70 | + $context->startTimestamp = $request->server('REQUEST_TIME_FLOAT', $fallbackTime); |
| 71 | + |
| 72 | + $this->transaction = $sentry->startTransaction($context); |
| 73 | + |
| 74 | + // Setting the Transaction on the Hub |
| 75 | + SentrySdk::getCurrentHub()->setSpan($this->transaction); |
| 76 | + |
| 77 | + if (!$this->addBootTimeSpans()) { |
| 78 | + // @TODO: We might want to move this together with the `RouteMatches` listener to some central place and or do this from the `EventHandler` |
| 79 | + app()->booted(function () use ($request, $fallbackTime): void { |
| 80 | + $spanContextStart = new SpanContext(); |
| 81 | + $spanContextStart->op = 'app.bootstrap'; |
| 82 | + $spanContextStart->startTimestamp = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT', $fallbackTime); |
| 83 | + $spanContextStart->endTimestamp = microtime(true); |
| 84 | + $this->transaction->startChild($spanContextStart); |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private function addBootTimeSpans(): bool |
| 90 | + { |
| 91 | + if (!defined('LARAVEL_START') || !LARAVEL_START) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + if (!defined('SENTRY_AUTOLOAD') || !SENTRY_AUTOLOAD) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + if (!defined('SENTRY_BOOTSTRAP') || !SENTRY_BOOTSTRAP) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + $spanContextStart = new SpanContext(); |
| 104 | + $spanContextStart->op = 'autoload'; |
| 105 | + $spanContextStart->startTimestamp = LARAVEL_START; |
| 106 | + $spanContextStart->endTimestamp = SENTRY_AUTOLOAD; |
| 107 | + $this->transaction->startChild($spanContextStart); |
| 108 | + |
| 109 | + $spanContextStart = new SpanContext(); |
| 110 | + $spanContextStart->op = 'bootstrap'; |
| 111 | + $spanContextStart->startTimestamp = SENTRY_AUTOLOAD; |
| 112 | + $spanContextStart->endTimestamp = SENTRY_BOOTSTRAP; |
| 113 | + $this->transaction->startChild($spanContextStart); |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | +} |
0 commit comments