Skip to content

Commit 3f88926

Browse files
committed
Keep the last scope
1 parent a7e4718 commit 3f88926

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/EventListener/ConsoleListener.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class ConsoleListener
3737
*/
3838
private $commandHasErrors;
3939

40+
/**
41+
* @var int tracks how many commands have been invoked from other commands
42+
*/
43+
private $commandDepth;
44+
4045
/**
4146
* Constructor.
4247
*
@@ -60,6 +65,7 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
6065
$scope = $this->hub->pushScope();
6166
$command = $event->getCommand();
6267
$input = $event->getInput();
68+
++$this->commandDepth;
6369

6470
if (null !== $command && null !== $command->getName()) {
6571
$scope->setTag('console.command', $command->getName());
@@ -78,9 +84,10 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
7884
*/
7985
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
8086
{
81-
// We need to keep the last scope around even until command termination to retain
82-
// information when flushing buffered events.
83-
if (false === $this->commandHasErrors) {
87+
--$this->commandDepth;
88+
// If a command encountered an error, we want to keep the scope to be able to populate the event.
89+
// We also want to keep the last scope so that breadcrumbs are not deleted once the command terminates.
90+
if (false === $this->commandHasErrors && $this->commandDepth > 0) {
8491
$this->hub->popScope();
8592
}
8693
}

tests/Command/BreadcrumbTestCommandTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testBreadcrumbWithConsoleListener()
7979
$this->application->doRun(new ArgvInput(['bin/console', 'sentry:breadcrumb:test']), new NullOutput());
8080
$this->fail();
8181
} catch (\Throwable $e) {
82-
$this->assertSame($e->getMessage(), 'Breadcrumb error');
82+
$this->assertEquals('Breadcrumb error', $e->getMessage());
8383
}
8484

8585
$event = Event::createEvent();
@@ -119,8 +119,8 @@ public function testSubCommandBreadcrumbs()
119119
});
120120

121121
$this->assertNotNull($modifiedEvent);
122-
// We do not have breadcrumbs here because no error happened and the scope was popped.
123-
$this->assertCount(0, $modifiedEvent->getBreadcrumbs());
122+
// We have breadcrumbs but only from the root console command.
123+
$this->assertCount(2, $modifiedEvent->getBreadcrumbs());
124124
}
125125

126126
/**
@@ -140,7 +140,7 @@ public function testCrashingSubcommand()
140140
$this->application->doRun(new ArgvInput(['bin/console', 'sentry:subcommand:test']), new NullOutput());
141141
$this->fail();
142142
} catch (\Throwable $e) {
143-
$this->assertSame($e->getMessage(), 'Breadcrumb error');
143+
$this->assertEquals('Breadcrumb error', $e->getMessage());
144144
}
145145

146146
$event = Event::createEvent();
@@ -152,7 +152,7 @@ public function testCrashingSubcommand()
152152

153153
$this->assertNotNull($modifiedEvent);
154154
$this->assertCount(2, $modifiedEvent->getBreadcrumbs());
155-
$this->assertSame($modifiedEvent->getTags()['console.command'], 'sentry:breadcrumb:test');
155+
$this->assertEquals('sentry:breadcrumb:test', $modifiedEvent->getTags()['console.command']);
156156
}
157157

158158
/**
@@ -176,7 +176,7 @@ public function testRunSecondCommandAfterCrashingCommand()
176176
$this->application->doRun(new ArgvInput(['bin/console', 'sentry:subcommand:test']), new NullOutput());
177177
$this->fail();
178178
} catch (\Throwable $e) {
179-
$this->assertSame($e->getMessage(), 'Breadcrumb error');
179+
$this->assertEquals('Breadcrumb error', $e->getMessage());
180180
}
181181

182182
$command = new SentryDummyTestCommand($this->logger);
@@ -195,6 +195,31 @@ public function testRunSecondCommandAfterCrashingCommand()
195195
// Breadcrumbs contain all log entries until the sentry:dummy:test command crash.
196196
$this->assertCount(3, $modifiedEvent->getBreadcrumbs());
197197
// console.command tag is properly set to the last command
198-
$this->assertSame($modifiedEvent->getTags()['console.command'], 'sentry:dummy:test');
198+
$this->assertEquals('sentry:dummy:test', $modifiedEvent->getTags()['console.command']);
199+
}
200+
201+
/**
202+
* Tests that even if no errors occur, breadcrumb information is available.
203+
*
204+
* @return void
205+
* @throws \Throwable
206+
*/
207+
public function testBreadcrumbsAreAvailableAfterCommandTermination()
208+
{
209+
$command = new SentryDummyTestCommand($this->logger);
210+
$this->application->add($command);
211+
212+
$this->application->doRun(new ArgvInput(['bin/console', 'sentry:dummy:test']), new NullOutput());
213+
214+
$event = Event::createEvent();
215+
$modifiedEvent = null;
216+
$this->hub->configureScope(function (Scope $scope) use ($event, &$modifiedEvent) {
217+
$modifiedEvent = $scope->applyToEvent($event);
218+
});
219+
220+
$this->assertNotNull($modifiedEvent);
221+
$this->assertCount(1, $modifiedEvent->getBreadcrumbs());
222+
$this->assertEquals('This is a dummy message', $modifiedEvent->getBreadcrumbs()[0]->getMessage());
223+
$this->assertEquals('sentry:dummy:test', $modifiedEvent->getTags()['console.command']);
199224
}
200225
}

0 commit comments

Comments
 (0)