-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
356 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Tracing; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Sentry\SentrySdk; | ||
use Sentry\State\Hub; | ||
use Sentry\Tracing\SpanContext; | ||
use Sentry\Tracing\TransactionContext; | ||
|
||
class Middleware | ||
{ | ||
/** | ||
* The current active transaction. | ||
* | ||
* @var \Sentry\Tracing\Transaction|null | ||
*/ | ||
protected $transaction; | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (app()->bound('sentry')) { | ||
$this->startTransaction($request, app('sentry')); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
/** | ||
* Handle the application termination. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Illuminate\Http\Response $response | ||
* | ||
* @return void | ||
*/ | ||
public function terminate($request, $response): void | ||
{ | ||
if ($this->transaction !== null && app()->bound('sentry')) { | ||
$this->transaction->finish(); | ||
} | ||
} | ||
|
||
private function startTransaction(Request $request, Hub $sentry): void | ||
{ | ||
$path = '/' . ltrim($request->path(), '/'); | ||
$fallbackTime = microtime(true); | ||
$sentryTraceHeader = $request->header('sentry-trace'); | ||
|
||
$context = $sentryTraceHeader | ||
? TransactionContext::fromTraceparent($sentryTraceHeader) | ||
: new TransactionContext; | ||
|
||
$context->op = 'http.server'; | ||
$context->name = $path; | ||
$context->data = [ | ||
'url' => $path, | ||
'method' => strtoupper($request->method()), | ||
]; | ||
$context->startTimestamp = $request->server('REQUEST_TIME_FLOAT', $fallbackTime); | ||
|
||
$this->transaction = $sentry->startTransaction($context); | ||
|
||
// Setting the Transaction on the Hub | ||
SentrySdk::getCurrentHub()->setSpan($this->transaction); | ||
|
||
if (!$this->addBootTimeSpans()) { | ||
// @TODO: We might want to move this together with the `RouteMatches` listener to some central place and or do this from the `EventHandler` | ||
app()->booted(function () use ($request, $fallbackTime): void { | ||
$spanContextStart = new SpanContext(); | ||
$spanContextStart->op = 'app.bootstrap'; | ||
$spanContextStart->startTimestamp = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT', $fallbackTime); | ||
$spanContextStart->endTimestamp = microtime(true); | ||
$this->transaction->startChild($spanContextStart); | ||
}); | ||
} | ||
} | ||
|
||
private function addBootTimeSpans(): bool | ||
{ | ||
if (!defined('LARAVEL_START') || !LARAVEL_START) { | ||
return false; | ||
} | ||
|
||
if (!defined('SENTRY_AUTOLOAD') || !SENTRY_AUTOLOAD) { | ||
return false; | ||
} | ||
|
||
if (!defined('SENTRY_BOOTSTRAP') || !SENTRY_BOOTSTRAP) { | ||
return false; | ||
} | ||
|
||
$spanContextStart = new SpanContext(); | ||
$spanContextStart->op = 'autoload'; | ||
$spanContextStart->startTimestamp = LARAVEL_START; | ||
$spanContextStart->endTimestamp = SENTRY_AUTOLOAD; | ||
$this->transaction->startChild($spanContextStart); | ||
|
||
$spanContextStart = new SpanContext(); | ||
$spanContextStart->op = 'bootstrap'; | ||
$spanContextStart->startTimestamp = SENTRY_AUTOLOAD; | ||
$spanContextStart->endTimestamp = SENTRY_BOOTSTRAP; | ||
$this->transaction->startChild($spanContextStart); | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.