Skip to content

Commit bea6615

Browse files
authored
Octane fixes (#921)
* Do not hold reference to app instance but retrieve it when needed * Guard against route name possibly being a Closure
1 parent f987314 commit bea6615

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Sentry\Laravel\Tracing;
44

55
use Closure;
6-
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
76
use Illuminate\Http\Request;
87
use Laravel\Lumen\Application as LumenApplication;
98
use Sentry\SentrySdk;
@@ -41,13 +40,6 @@ class Middleware
4140
*/
4241
private $bootedTimestamp;
4342

44-
/**
45-
* The Laravel or Lumen application instance.
46-
*
47-
* @var LaravelApplication|LumenApplication
48-
*/
49-
private $app;
50-
5143
/**
5244
* Whether we should continue tracing after the response has been sent to the client.
5345
*
@@ -71,27 +63,24 @@ class Middleware
7163

7264
/**
7365
* Construct the Sentry tracing middleware.
74-
*
75-
* @param LaravelApplication|LumenApplication $app
7666
*/
77-
public function __construct($app, bool $continueAfterResponse = true)
67+
public function __construct(bool $continueAfterResponse = true)
7868
{
79-
$this->app = $app;
8069
$this->continueAfterResponse = $continueAfterResponse;
8170
}
8271

8372
/**
8473
* Handle an incoming request.
8574
*
8675
* @param \Illuminate\Http\Request $request
87-
* @param \Closure $next
76+
* @param \Closure $next
8877
*
8978
* @return mixed
9079
*/
9180
public function handle(Request $request, Closure $next)
9281
{
93-
if ($this->app->bound(HubInterface::class)) {
94-
$this->startTransaction($request, $this->app->make(HubInterface::class));
82+
if (app()->bound(HubInterface::class)) {
83+
$this->startTransaction($request, app(HubInterface::class));
9584
}
9685

9786
return $next($request);
@@ -101,14 +90,14 @@ public function handle(Request $request, Closure $next)
10190
* Handle the application termination.
10291
*
10392
* @param \Illuminate\Http\Request $request
104-
* @param mixed $response
93+
* @param mixed $response
10594
*
10695
* @return void
10796
*/
10897
public function terminate(Request $request, $response): void
10998
{
11099
// If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
111-
if ($this->transaction === null || !$this->app->bound(HubInterface::class)) {
100+
if ($this->transaction === null || !app()->bound(HubInterface::class)) {
112101
return;
113102
}
114103

@@ -137,7 +126,7 @@ public function terminate(Request $request, $response): void
137126
// dispatched using dispatch(...)->afterResponse(). This middleware is called
138127
// before the terminating callbacks so we are 99.9% sure to be the last one
139128
// to run except if another terminating callback is registered after ours.
140-
$this->app->terminating(function () {
129+
app()->terminating(function () {
141130
$this->finishTransaction();
142131
});
143132

@@ -290,7 +279,7 @@ private function shouldRouteBeIgnored(Request $request): bool
290279
{
291280
// Laravel Lumen doesn't use `illuminate/routing`.
292281
// Instead we use the route available on the request to detect if a route was matched.
293-
if ($this->app instanceof LumenApplication) {
282+
if (app() instanceof LumenApplication) {
294283
return $request->route() === null && config('sentry.tracing.missing_routes', false) === false;
295284
}
296285

src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\Laravel\Tracing\Routing;
44

5+
use Closure;
56
use Illuminate\Routing\Route;
67
use Sentry\SentrySdk;
78
use Sentry\Tracing\SpanContext;
@@ -17,9 +18,13 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
1718
return $dispatch();
1819
}
1920

21+
// The action name can be a Closure curiously enough... so we guard againt that here
22+
// @see: https://github.com/getsentry/sentry-laravel/issues/917
23+
$action = $route->getActionName() instanceof Closure ? 'Closure' : $route->getActionName();
24+
2025
$context = new SpanContext;
2126
$context->setOp('http.route');
22-
$context->setDescription($route->getActionName());
27+
$context->setDescription($action);
2328

2429
$span = $parentSpan->startChild($context);
2530

src/Sentry/Laravel/Tracing/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function register(): void
6868
$continueAfterResponse = false;
6969
}
7070

71-
return new Middleware($this->app, $continueAfterResponse);
71+
return new Middleware($continueAfterResponse);
7272
});
7373
}
7474

0 commit comments

Comments
 (0)