Skip to content

Commit 1885283

Browse files
authored
Correctly cleanup state in middleware (#997)
1 parent 624f1dc commit 1885283

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(bool $continueAfterResponse = true)
7373
public function handle(Request $request, Closure $next)
7474
{
7575
if (app()->bound(HubInterface::class)) {
76-
$this->startTransaction($request, app(HubInterface::class));
76+
$this->startTransaction($request);
7777
}
7878

7979
return $next($request);
@@ -89,12 +89,14 @@ public function handle(Request $request, Closure $next)
8989
*/
9090
public function terminate(Request $request, $response): void
9191
{
92-
// If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
93-
if ($this->transaction === null || !app()->bound(HubInterface::class)) {
92+
// If there is no transaction there is nothing for us to do
93+
if ($this->transaction === null) {
9494
return;
9595
}
9696

9797
if ($this->shouldRouteBeIgnored($request)) {
98+
$this->discardTransaction();
99+
98100
return;
99101
}
100102

@@ -132,10 +134,12 @@ public function setBootedTimestamp(?float $timestamp = null): void
132134
$this->bootedTimestamp = $timestamp ?? microtime(true);
133135
}
134136

135-
private function startTransaction(Request $request, HubInterface $sentry): void
137+
private function startTransaction(Request $request): void
136138
{
139+
$hub = SentrySdk::getCurrentHub();
140+
137141
// Prevent starting a new transaction if we are already in a transaction
138-
if ($sentry->getTransaction() !== null) {
142+
if ($hub->getTransaction() !== null) {
139143
return;
140144
}
141145

@@ -168,9 +172,9 @@ private function startTransaction(Request $request, HubInterface $sentry): void
168172
'http.request.method' => strtoupper($request->method()),
169173
]);
170174

171-
$transaction = $sentry->startTransaction($context);
175+
$transaction = $hub->startTransaction($context);
172176

173-
SentrySdk::getCurrentHub()->setSpan($transaction);
177+
$hub->setSpan($transaction);
174178

175179
// If this transaction is not sampled, we can stop here to prevent doing work for nothing
176180
if (!$transaction->getSampled()) {
@@ -188,7 +192,17 @@ private function startTransaction(Request $request, HubInterface $sentry): void
188192
->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true))
189193
);
190194

191-
SentrySdk::getCurrentHub()->setSpan($this->appSpan);
195+
$hub->setSpan($this->appSpan);
196+
}
197+
198+
private function discardTransaction(): void
199+
{
200+
$this->appSpan = null;
201+
$this->transaction = null;
202+
$this->didRouteMatch = false;
203+
$this->bootedTimestamp = null;
204+
205+
SentrySdk::getCurrentHub()->setSpan(null);
192206
}
193207

194208
private function addAppBootstrapSpan(): ?Span

0 commit comments

Comments
 (0)