Skip to content

Commit 66e2ac0

Browse files
authored
Do not try to detect the session key on the CLI (#1058)
1 parent f220d88 commit 66e2ac0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Sentry/Laravel/Features/CacheIntegration.php

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

55
use Illuminate\Cache\Events;
6+
use Illuminate\Foundation\Application;
67
use Illuminate\Contracts\Events\Dispatcher;
78
use Illuminate\Contracts\Session\Session;
89
use 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

test/Sentry/Features/CacheIntegrationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
use Illuminate\Support\Facades\Cache;
77
use Sentry\Laravel\Tests\TestCase;
88
use Sentry\Tracing\Span;
9+
use Sentry\Laravel\Features\CacheIntegration;
910

1011
class CacheIntegrationTest extends TestCase
1112
{
1213
protected $defaultSetupConfig = [
1314
'session.driver' => 'array',
1415
];
1516

17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
// Ensure that session keys can be detected in the tests that are running from the console
22+
CacheIntegration::$detectSessionKeyOnConsole = true;
23+
}
24+
1625
public function testCacheBreadcrumbForWriteAndHitIsRecorded(): void
1726
{
1827
Cache::put($key = 'foo', 'bar');

0 commit comments

Comments
 (0)