Skip to content

Commit

Permalink
Octane fixes (#921)
Browse files Browse the repository at this point in the history
* Do not hold reference to app instance but retrieve it when needed

* Guard against route name possibly being a Closure
  • Loading branch information
stayallive authored Jul 17, 2024
1 parent f987314 commit bea6615
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
27 changes: 8 additions & 19 deletions src/Sentry/Laravel/Tracing/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Sentry\Laravel\Tracing;

use Closure;
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
use Illuminate\Http\Request;
use Laravel\Lumen\Application as LumenApplication;
use Sentry\SentrySdk;
Expand Down Expand Up @@ -41,13 +40,6 @@ class Middleware
*/
private $bootedTimestamp;

/**
* The Laravel or Lumen application instance.
*
* @var LaravelApplication|LumenApplication
*/
private $app;

/**
* Whether we should continue tracing after the response has been sent to the client.
*
Expand All @@ -71,27 +63,24 @@ class Middleware

/**
* Construct the Sentry tracing middleware.
*
* @param LaravelApplication|LumenApplication $app
*/
public function __construct($app, bool $continueAfterResponse = true)
public function __construct(bool $continueAfterResponse = true)
{
$this->app = $app;
$this->continueAfterResponse = $continueAfterResponse;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($this->app->bound(HubInterface::class)) {
$this->startTransaction($request, $this->app->make(HubInterface::class));
if (app()->bound(HubInterface::class)) {
$this->startTransaction($request, app(HubInterface::class));
}

return $next($request);
Expand All @@ -101,14 +90,14 @@ public function handle(Request $request, Closure $next)
* Handle the application termination.
*
* @param \Illuminate\Http\Request $request
* @param mixed $response
* @param mixed $response
*
* @return void
*/
public function terminate(Request $request, $response): void
{
// If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
if ($this->transaction === null || !$this->app->bound(HubInterface::class)) {
if ($this->transaction === null || !app()->bound(HubInterface::class)) {
return;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sentry\Laravel\Tracing\Routing;

use Closure;
use Illuminate\Routing\Route;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;
Expand All @@ -17,9 +18,13 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
return $dispatch();
}

// The action name can be a Closure curiously enough... so we guard againt that here
// @see: https://github.com/getsentry/sentry-laravel/issues/917
$action = $route->getActionName() instanceof Closure ? 'Closure' : $route->getActionName();

$context = new SpanContext;
$context->setOp('http.route');
$context->setDescription($route->getActionName());
$context->setDescription($action);

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

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/Tracing/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function register(): void
$continueAfterResponse = false;
}

return new Middleware($this->app, $continueAfterResponse);
return new Middleware($continueAfterResponse);
});
}

Expand Down

0 comments on commit bea6615

Please sign in to comment.