diff --git a/analysis-baseline.toml b/analysis-baseline.toml index 2c8f8507a0..a7ab884716 100644 --- a/analysis-baseline.toml +++ b/analysis-baseline.toml @@ -684,6 +684,12 @@ code = "mixed-assignment" message = "Assigning `mixed` type to a variable may lead to unexpected behavior." count = 2 +[[issues]] +file = "src/Monolog/Handler.php" +code = "possibly-invalid-argument" +message = 'Possible argument type mismatch for argument #1 of `Monolog\Handler\AbstractProcessingHandler::handle`: expected `Monolog\LogRecord`, but possibly received `Monolog\LogRecord|array`.' +count = 1 + [[issues]] file = "src/Monolog/LogToSentryIssueHandler.php" code = "deprecated-class-constant" diff --git a/src/Monolog/ExceptionToSentryIssueHandler.php b/src/Monolog/ExceptionToSentryIssueHandler.php index 6f1d7cde58..5dc573afc0 100644 --- a/src/Monolog/ExceptionToSentryIssueHandler.php +++ b/src/Monolog/ExceptionToSentryIssueHandler.php @@ -18,6 +18,7 @@ class ExceptionToSentryIssueHandler extends AbstractProcessingHandler { use CompatibilityProcessingHandlerTrait; + use SentryContextFlagTrait; /** * @var HubInterface @@ -42,7 +43,7 @@ public function handle($record): bool $exception = $this->getExceptionFromRecord($record); /** @phpstan-ignore-next-line */ - if ($exception === null || !$this->isHandling($record)) { + if ($exception === null || !$this->isHandling($record) || self::isSentryIssueDisabled($this->getContextFromRecord($record))) { return false; } diff --git a/src/Monolog/Handler.php b/src/Monolog/Handler.php index ee1991f11f..8e9ae4f2c2 100644 --- a/src/Monolog/Handler.php +++ b/src/Monolog/Handler.php @@ -26,6 +26,7 @@ final class Handler extends AbstractProcessingHandler { use CompatibilityProcessingHandlerTrait; + use SentryContextFlagTrait; private const CONTEXT_EXCEPTION_KEY = 'exception'; @@ -52,6 +53,19 @@ public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bub $this->fillExtraContext = $fillExtraContext; } + /** + * @param array|LogRecord $record + */ + public function handle($record): bool + { + if (self::isSentryIssueDisabled($this->getArrayFieldFromRecord($record, 'context'))) { + return false; + } + + /** @phpstan-ignore-next-line */ + return parent::handle($record); + } + /** * @param array|LogRecord $record */ @@ -88,6 +102,20 @@ protected function doWrite($record): void }); } + /** + * @param array|LogRecord $record + * + * @return mixed[] + */ + private function getArrayFieldFromRecord($record, string $field): array + { + if (isset($record[$field]) && \is_array($record[$field])) { + return $record[$field]; + } + + return []; + } + /** * @param mixed[] $context * diff --git a/src/Monolog/LogToSentryIssueHandler.php b/src/Monolog/LogToSentryIssueHandler.php index 18dd6eab61..3509208eb2 100644 --- a/src/Monolog/LogToSentryIssueHandler.php +++ b/src/Monolog/LogToSentryIssueHandler.php @@ -20,6 +20,7 @@ class LogToSentryIssueHandler extends AbstractProcessingHandler { use CompatibilityProcessingHandlerTrait; + use SentryContextFlagTrait; private const CONTEXT_EXCEPTION_KEY = 'exception'; @@ -49,6 +50,10 @@ public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bub */ public function handle($record): bool { + if (self::isSentryIssueDisabled($this->getArrayFieldFromRecord($record, 'context'))) { + return false; + } + /** @phpstan-ignore-next-line */ if (!$this->isHandling($record) || $this->hasThrowable($record)) { return false; diff --git a/src/Monolog/SentryContextFlagTrait.php b/src/Monolog/SentryContextFlagTrait.php new file mode 100644 index 0000000000..a3c6d33f1f --- /dev/null +++ b/src/Monolog/SentryContextFlagTrait.php @@ -0,0 +1,28 @@ +warning('Invalid CSV row', ['sentry' => false]); + * + * The record is still handled by every other handler (including the + * {@see BreadcrumbHandler} and the {@see LogsHandler}), so it remains + * available as context should another record trigger an issue. + * + * @internal + */ +trait SentryContextFlagTrait +{ + /** + * @param mixed[] $context The context of the log record + */ + private static function isSentryIssueDisabled(array $context): bool + { + return ($context['sentry'] ?? null) === false; + } +} diff --git a/tests/Monolog/ExceptionToSentryIssueHandlerTest.php b/tests/Monolog/ExceptionToSentryIssueHandlerTest.php index 89151b340b..111eb2ed16 100644 --- a/tests/Monolog/ExceptionToSentryIssueHandlerTest.php +++ b/tests/Monolog/ExceptionToSentryIssueHandlerTest.php @@ -152,6 +152,29 @@ public function testHandleIgnoresRecordsWithoutThrowable($record): void $this->assertFalse($handler->handle($record)); } + public function testHandleIgnoresRecordsWithSentryDisabledInContext(): void + { + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->never()) + ->method('captureException'); + + $handler = new ExceptionToSentryIssueHandler(new Hub($client, new Scope()), Logger::DEBUG, false); + $record = RecordFactory::create( + 'foo bar', + Logger::ERROR, + 'channel.foo', + [ + 'exception' => new \RuntimeException('boom'), + 'sentry' => false, + ], + [] + ); + + $this->assertTrue($handler->isHandling($record)); + $this->assertFalse($handler->handle($record)); + } + public function testHandleIgnoresRecordsBelowThreshold(): void { $exception = new \RuntimeException('boom'); diff --git a/tests/Monolog/HandlerTest.php b/tests/Monolog/HandlerTest.php index bc1d4dd67f..3099ca69bc 100644 --- a/tests/Monolog/HandlerTest.php +++ b/tests/Monolog/HandlerTest.php @@ -49,6 +49,19 @@ public function testHandle(bool $fillExtraContext, $record, Event $expectedEvent $handler->handle($record); } + public function testHandleIgnoresRecordsWithSentryDisabledInContext(): void + { + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->never()) + ->method('captureEvent'); + + $handler = new Handler(new Hub($client, new Scope()), Logger::DEBUG, false); + + $this->assertFalse($handler->handle(RecordFactory::create('foo bar', Logger::WARNING, 'channel.foo', ['sentry' => false], []))); + $this->assertFalse($handler->handle(RecordFactory::create('foo bar', Logger::ERROR, 'channel.foo', ['exception' => new \RuntimeException('boom'), 'sentry' => false], []))); + } + public static function handleDataProvider(): iterable { $event = Event::createEvent(); diff --git a/tests/Monolog/LogToSentryIssueHandlerTest.php b/tests/Monolog/LogToSentryIssueHandlerTest.php index 7bbff023b4..4c6b1b33a4 100644 --- a/tests/Monolog/LogToSentryIssueHandlerTest.php +++ b/tests/Monolog/LogToSentryIssueHandlerTest.php @@ -8,10 +8,12 @@ use Monolog\LogRecord; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Sentry\Breadcrumb; use Sentry\ClientBuilder; use Sentry\ClientInterface; use Sentry\Event; use Sentry\EventHint; +use Sentry\Monolog\BreadcrumbHandler; use Sentry\Monolog\ExceptionToSentryIssueHandler; use Sentry\Monolog\LogToSentryIssueHandler; use Sentry\Severity; @@ -142,6 +144,82 @@ public function testHandleCapturesRecordsWithNonThrowableExceptionContext(): voi $this->assertTrue($handler->handle($record)); } + public function testHandleIgnoresRecordsWithSentryDisabledInContext(): void + { + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->never()) + ->method('captureEvent'); + + $handler = new LogToSentryIssueHandler(new Hub($client, new Scope()), Logger::DEBUG, false); + $record = RecordFactory::create('foo bar', Logger::WARNING, 'channel.foo', ['sentry' => false], []); + + $this->assertTrue($handler->isHandling($record)); + $this->assertFalse($handler->handle($record)); + } + + /** + * @dataProvider nonFalseSentryContextValueDataProvider + * + * @param mixed $value + */ + public function testHandleCapturesRecordsWhenSentryContextIsNotFalse($value): void + { + /** @var ClientInterface&MockObject $client */ + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('captureEvent'); + + $handler = new LogToSentryIssueHandler(new Hub($client, new Scope()), Logger::DEBUG, false); + $record = RecordFactory::create('foo bar', Logger::WARNING, 'channel.foo', ['sentry' => $value], []); + + $this->assertTrue($handler->handle($record)); + } + + /** + * @return iterable + */ + public static function nonFalseSentryContextValueDataProvider(): iterable + { + yield [true]; + yield [null]; + yield [0]; + yield ['']; + yield ['false']; + } + + public function testRecordWithSentryDisabledIsStillRecordedAsBreadcrumb(): void + { + $client = ClientBuilder::create() + ->setTransport(StubTransport::getInstance()) + ->getClient(); + $hub = new Hub($client, new Scope()); + + $logger = new Logger('channel.foo', [ + new LogToSentryIssueHandler($hub, Logger::WARNING), + new ExceptionToSentryIssueHandler($hub, Logger::WARNING), + new BreadcrumbHandler($hub), + ]); + + $logger->warning('ignored warning', ['sentry' => false]); + $logger->error('ignored exception', ['exception' => new \RuntimeException('boom'), 'sentry' => false]); + + $this->assertCount(0, StubTransport::$events); + + $logger->warning('reported warning'); + + $this->assertCount(1, StubTransport::$events); + + $event = StubTransport::$events[0]; + $this->assertSame('reported warning', $event->getMessage()); + + $breadcrumbMessages = array_map(static function (Breadcrumb $breadcrumb): ?string { + return $breadcrumb->getMessage(); + }, $event->getBreadcrumbs()); + + $this->assertSame(['ignored warning', 'ignored exception'], $breadcrumbMessages); + } + public function testHandleIgnoresRecordsBelowThreshold(): void { /** @var ClientInterface&MockObject $client */