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