/
Middleware.php
186 lines (150 loc) · 5.71 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Sentry\Laravel\Tracing;
use Closure;
use Illuminate\Http\Request;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
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;
/**
* The timestamp of application bootstrap completion.
*
* @var float|null
*/
private $bootedTimestamp;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (app()->bound(HubInterface::class)) {
$this->startTransaction($request, app(HubInterface::class));
}
return $next($request);
}
/**
* Handle the application termination.
*
* @param \Illuminate\Http\Request $request
* @param mixed $response
*
* @return void
*/
public function terminate(Request $request, $response): void
{
if ($this->transaction !== null && app()->bound(HubInterface::class)) {
// We stop here if a route has not been matched unless we are configured to trace missing routes
if (config('sentry.tracing.missing_routes', false) === false && $request->route() === null) {
return;
}
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 ($response instanceof SymfonyResponse) {
$this->hydrateResponseData($response);
}
$this->transaction->finish();
}
}
/**
* Set the timestamp of application bootstrap completion.
*
* @param float|null $timestamp The unix timestamp of the booted event, default to `microtime(true)` if not `null`.
*
* @return void
*
* @internal This method should only be invoked right after the application has finished "booting".
*/
public function setBootedTimestamp(?float $timestamp = null): void
{
$this->bootedTimestamp = $timestamp ?? microtime(true);
}
private function startTransaction(Request $request, HubInterface $sentry): void
{
$requestStartTime = $request->server('REQUEST_TIME_FLOAT', microtime(true));
$context = TransactionContext::fromHeaders(
$request->header('sentry-trace', ''),
$request->header('baggage', '')
);
$requestPath = '/' . ltrim($request->path(), '/');
$context->setOp('http.server');
$context->setName($requestPath);
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($requestStartTime);
$context->setData([
'url' => $requestPath,
'method' => strtoupper($request->method()),
]);
$this->transaction = $sentry->startTransaction($context);
// Setting the Transaction on the Hub
SentrySdk::getCurrentHub()->setSpan($this->transaction);
$bootstrapSpan = $this->addAppBootstrapSpan($request);
$appContextStart = new SpanContext;
$appContextStart->setOp('middleware.handle');
$appContextStart->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true));
$this->appSpan = $this->transaction->startChild($appContextStart);
SentrySdk::getCurrentHub()->setSpan($this->appSpan);
}
private function addAppBootstrapSpan(Request $request): ?Span
{
if ($this->bootedTimestamp === null) {
return null;
}
$laravelStartTime = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT');
if ($laravelStartTime === null) {
return null;
}
$spanContextStart = new SpanContext;
$spanContextStart->setOp('app.bootstrap');
$spanContextStart->setStartTimestamp($laravelStartTime);
$spanContextStart->setEndTimestamp($this->bootedTimestamp);
$span = $this->transaction->startChild($spanContextStart);
// Consume the booted timestamp, because we don't want to report the bootstrap span more than once
$this->bootedTimestamp = null;
// Add more information about the bootstrap section if possible
$this->addBootDetailTimeSpans($span);
return $span;
}
private function addBootDetailTimeSpans(Span $bootstrap): void
{
// This constant should be defined right after the composer `autoload.php` require statement in `public/index.php`
// define('SENTRY_AUTOLOAD', microtime(true));
if (!defined('SENTRY_AUTOLOAD') || !SENTRY_AUTOLOAD) {
return;
}
$autoload = new SpanContext();
$autoload->setOp('app.php.autoload');
$autoload->setStartTimestamp($bootstrap->getStartTimestamp());
$autoload->setEndTimestamp(SENTRY_AUTOLOAD);
$bootstrap->startChild($autoload);
}
private function hydrateResponseData(SymfonyResponse $response): void
{
$this->transaction->setHttpStatus($response->getStatusCode());
}
}