Skip to content

Commit a49a962

Browse files
authored
Fix not accounting for Lumen application instance in tracing middleware (#724)
1 parent 39eb126 commit a49a962

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/Sentry/Laravel/Tracing/Middleware.php

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

55
use Closure;
6-
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
77
use Illuminate\Http\Request;
8+
use Laravel\Lumen\Application as LumenApplication;
89
use Sentry\SentrySdk;
910
use Sentry\State\HubInterface;
1011
use Sentry\Tracing\Span;
@@ -37,9 +38,9 @@ class Middleware
3738
private $bootedTimestamp;
3839

3940
/**
40-
* The Laravel application instance.
41+
* The Laravel or Lumen application instance.
4142
*
42-
* @var Application|null
43+
* @var LaravelApplication|LumenApplication
4344
*/
4445
private $app;
4546

@@ -53,9 +54,9 @@ class Middleware
5354
/**
5455
* Construct the Sentry tracing middleware.
5556
*
56-
* @param Application|null $app
57+
* @param LaravelApplication|LumenApplication $app
5758
*/
58-
public function __construct(?Application $app)
59+
public function __construct($app)
5960
{
6061
$this->app = $app;
6162
}
@@ -106,26 +107,22 @@ public function terminate(Request $request, $response): void
106107
$this->hydrateResponseData($response);
107108
}
108109

109-
if ($this->app === null) {
110-
$this->finishTransaction();
111-
} else {
112-
// Ensure we do not register the terminating callback multiple times since there is no point in doing so
113-
if ($this->registeredTerminatingCallback) {
114-
return;
115-
}
116-
117-
// We need to finish the transaction after the response has been sent to the client
118-
// so we register a terminating callback to do so, this allows us to also capture
119-
// spans that are created during the termination of the application like queue
120-
// dispatched using dispatch(...)->afterResponse(). This middleware is called
121-
// before the terminating callbacks so we are 99.9% sure to be the last one
122-
// to run except if another terminating callback is registered after ours.
123-
$this->app->terminating(function () {
124-
$this->finishTransaction();
125-
});
126-
127-
$this->registeredTerminatingCallback = true;
110+
// Ensure we do not register the terminating callback multiple times since there is no point in doing so
111+
if ($this->registeredTerminatingCallback) {
112+
return;
128113
}
114+
115+
// We need to finish the transaction after the response has been sent to the client
116+
// so we register a terminating callback to do so, this allows us to also capture
117+
// spans that are created during the termination of the application like queue
118+
// dispatched using dispatch(...)->afterResponse(). This middleware is called
119+
// before the terminating callbacks so we are 99.9% sure to be the last one
120+
// to run except if another terminating callback is registered after ours.
121+
$this->app->terminating(function () {
122+
$this->finishTransaction();
123+
});
124+
125+
$this->registeredTerminatingCallback = true;
129126
}
130127

131128
/**

src/Sentry/Laravel/Tracing/ServiceProvider.php

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

55
use Illuminate\Contracts\Container\BindingResolutionException;
66
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Contracts\Foundation\Application;
78
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
89
use Illuminate\Contracts\View\Engine;
910
use Illuminate\Contracts\View\View;
@@ -59,7 +60,9 @@ public function boot(): void
5960

6061
public function register(): void
6162
{
62-
$this->app->singleton(Middleware::class);
63+
$this->app->singleton(Middleware::class, function () {
64+
return new Middleware($this->app);
65+
});
6366

6467
$this->app->singleton(BacktraceHelper::class, function () {
6568
/** @var \Sentry\State\Hub $sentry */

0 commit comments

Comments
 (0)