33namespace Sentry \Laravel \Features ;
44
55use Illuminate \Cache \Events ;
6+ use Illuminate \Foundation \Application ;
67use Illuminate \Contracts \Events \Dispatcher ;
78use Illuminate \Contracts \Session \Session ;
89use Illuminate \Redis \Events as RedisEvents ;
@@ -22,6 +23,15 @@ class CacheIntegration extends Feature
2223{
2324 use WorksWithSpans, TracksPushedScopesAndSpans, ResolvesEventOrigin;
2425
26+ /**
27+ * Indicates whether to attempt to detect the session key when running in the console.
28+ *
29+ * @internal this is mainly intended for testing purposes.
30+ *
31+ * @var bool
32+ */
33+ public static $ detectSessionKeyOnConsole = false ;
34+
2535 public function isApplicable (): bool
2636 {
2737 return $ this ->isTracingFeatureEnabled ('redis_commands ' , false )
@@ -259,7 +269,17 @@ private function maybeHandleCacheEventAsEndOfSpan(Events\CacheEvent $event): boo
259269 */
260270 private function getSessionKey (): ?string
261271 {
272+ $ container = $ this ->container ();
273+
262274 try {
275+ // A session key is highly unusal to be available when running in the console
276+ // So we skip trying to get the session key in that case to prevent booting up the session store unnecessarily
277+ // Doing this anyway can result in unnecessary database connections for example
278+ // See: https://github.com/getsentry/sentry-laravel/issues/1057
279+ if (!self ::$ detectSessionKeyOnConsole && $ container instanceof Application && $ container ->runningInConsole ()) {
280+ return null ;
281+ }
282+
263283 /** @var Session $sessionStore */
264284 $ sessionStore = $ this ->container ()->make ('session.store ' );
265285
0 commit comments