/
Middleware.php
170 lines (141 loc) · 5.34 KB
/
Middleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Sentry\Laravel\Tracing;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Route;
use Sentry\Laravel\Integration;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\TransactionContext;
class Middleware
{
/**
* The current active transaction.
*
* @var \Sentry\Tracing\Transaction|null
*/
protected $transaction;
/**
* The span for the `app.handle` part of the application.
*
* @var \Sentry\Tracing\Span|null
*/
protected $appSpan;
/**
* 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')) {
if ($this->appSpan !== null) {
$this->appSpan->finish();
}
// Make sure we set the transaction and not have a child span in the Sentry SDK
// If the transaction is not on the scope during finish, the trace.context is wrong
SentrySdk::getCurrentHub()->setSpan($this->transaction);
if ($request instanceof Request) {
$this->hydrateRequestData($request);
}
if ($response instanceof Response) {
$this->hydrateResponseData($response);
}
$this->transaction->finish();
}
}
private function startTransaction(Request $request, HubInterface $sentry): void
{
$path = '/' . ltrim($request->path(), '/');
$fallbackTime = microtime(true);
$sentryTraceHeader = $request->header('sentry-trace');
$context = $sentryTraceHeader
? TransactionContext::fromTraceparent($sentryTraceHeader)
: new TransactionContext;
$context->setOp('http.server');
$context->setName($path);
$context->setData([
'url' => $path,
'method' => strtoupper($request->method()),
]);
$context->setStartTimestamp($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->setOp('app.bootstrap');
$spanContextStart->setStartTimestamp(defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT', $fallbackTime));
$spanContextStart->setEndTimestamp(microtime(true));
$this->transaction->startChild($spanContextStart);
$appContextStart = new SpanContext();
$appContextStart->setOp('app.handle');
$appContextStart->setStartTimestamp(microtime(true));
$this->appSpan = $this->transaction->startChild($appContextStart);
SentrySdk::getCurrentHub()->setSpan($this->appSpan);
});
}
}
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->setOp('autoload');
$spanContextStart->setStartTimestamp(LARAVEL_START);
$spanContextStart->setEndTimestamp(SENTRY_AUTOLOAD);
$this->transaction->startChild($spanContextStart);
$spanContextStart = new SpanContext();
$spanContextStart->setOp('bootstrap');
$spanContextStart->setStartTimestamp(SENTRY_AUTOLOAD);
$spanContextStart->setEndTimestamp(SENTRY_BOOTSTRAP);
$this->transaction->startChild($spanContextStart);
return true;
}
private function hydrateRequestData(Request $request): void
{
$route = $request->route();
if ($route instanceof Route) {
$routeName = Integration::extractNameForRoute($route) ?? '<unlabeled transaction>';
$this->transaction->setName($routeName);
$this->transaction->setData([
'name' => $route->getName(),
'action' => $route->getActionName(),
'method' => $request->getMethod(),
]);
}
}
private function hydrateResponseData(Response $response): void
{
$this->transaction->setHttpStatus($response->status());
}
}