-
-
Notifications
You must be signed in to change notification settings - Fork 468
feat(monolog): add handler to only capture exceptions #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Litarnus
wants to merge
2
commits into
master
Choose a base branch
from
monolog-to-sentry-error-handler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Monolog; | ||
|
|
||
| use Monolog\Handler\AbstractHandler; | ||
| use Monolog\Level; | ||
| use Monolog\Logger; | ||
| use Monolog\LogRecord; | ||
| use Psr\Log\LogLevel; | ||
| use Sentry\State\HubInterface; | ||
| use Sentry\State\Scope; | ||
|
|
||
| /** | ||
| * This Monolog handler will collect monolog events and send them to sentry. | ||
| */ | ||
| class SentryExceptionHandler extends AbstractHandler | ||
| { | ||
| /** | ||
| * @var HubInterface | ||
| */ | ||
| private $hub; | ||
|
|
||
| /** | ||
| * @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level | ||
| */ | ||
| public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true) | ||
| { | ||
| $this->hub = $hub; | ||
|
|
||
| parent::__construct($level, $bubble); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed>|LogRecord $record | ||
| */ | ||
| public function handle($record): bool | ||
| { | ||
| $exception = $this->getExceptionFromRecord($record); | ||
|
|
||
| /** @phpstan-ignore-next-line */ | ||
| if ($exception === null || !$this->isHandling($record)) { | ||
| return false; | ||
| } | ||
|
|
||
| $this->hub->withScope(function (Scope $scope) use ($record, $exception): void { | ||
| $scope->setExtra('monolog.channel', $record['channel']); | ||
| $scope->setExtra('monolog.level', $record['level_name']); | ||
| $scope->setExtra('monolog.message', $record['message']); | ||
|
|
||
| $monologContextData = $this->getMonologContextData($this->getContextFromRecord($record)); | ||
|
|
||
| if ($monologContextData !== []) { | ||
| $scope->setExtra('monolog.context', $monologContextData); | ||
| } | ||
|
|
||
| $monologExtraData = $this->getExtraFromRecord($record); | ||
|
|
||
| if ($monologExtraData !== []) { | ||
| $scope->setExtra('monolog.extra', $monologExtraData); | ||
| } | ||
|
|
||
| $this->hub->captureException($exception); | ||
| }); | ||
|
|
||
| return $this->bubble === false; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed>|LogRecord $record | ||
| */ | ||
| private function getExceptionFromRecord($record): ?\Throwable | ||
| { | ||
| $exception = $this->getContextFromRecord($record)['exception'] ?? null; | ||
|
|
||
| if ($exception instanceof \Throwable) { | ||
| return $exception; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed>|LogRecord $record | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function getContextFromRecord($record): array | ||
| { | ||
| return $this->getArrayFieldFromRecord($record, 'context'); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed>|LogRecord $record | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function getExtraFromRecord($record): array | ||
| { | ||
| return $this->getArrayFieldFromRecord($record, 'extra'); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed>|LogRecord $record | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function getArrayFieldFromRecord($record, string $field): array | ||
| { | ||
| if (isset($record[$field]) && \is_array($record[$field])) { | ||
| return $record[$field]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $context | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function getMonologContextData(array $context): array | ||
| { | ||
| unset($context['exception']); | ||
|
|
||
| return $context; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tests\Monolog; | ||
|
|
||
| use Monolog\Logger; | ||
| use Monolog\LogRecord; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Sentry\ClientInterface; | ||
| use Sentry\Event; | ||
| use Sentry\Monolog\SentryExceptionHandler; | ||
| use Sentry\State\Hub; | ||
| use Sentry\State\Scope; | ||
|
|
||
| final class SentryExceptionHandlerTest extends TestCase | ||
| { | ||
| /** | ||
| * @dataProvider capturedRecordsDataProvider | ||
| * | ||
| * @param LogRecord|array<string, mixed> $record | ||
| * @param array<string, mixed> $expectedExtra | ||
| */ | ||
| public function testHandleCapturesExceptionAndAddsMetadata($record, \Throwable $exception, array $expectedExtra): void | ||
| { | ||
| /** @var ClientInterface&MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('captureException') | ||
| ->with( | ||
| $this->identicalTo($exception), | ||
| $this->callback(function (Scope $scopeArg) use ($expectedExtra): bool { | ||
| $event = $scopeArg->applyToEvent(Event::createEvent()); | ||
|
|
||
| $this->assertNotNull($event); | ||
| $this->assertSame($expectedExtra, $event->getExtra()); | ||
|
|
||
| return true; | ||
| }), | ||
| null | ||
| ); | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($client, new Scope())); | ||
|
|
||
| $this->assertTrue($handler->isHandling($record)); | ||
| $handler->handle($record); | ||
| } | ||
|
|
||
| public function testHandleReturnsFalseWhenBubblingEnabled(): void | ||
| { | ||
| $exception = new \RuntimeException('boom'); | ||
|
|
||
| /** @var ClientInterface&MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('captureException') | ||
| ->with($this->identicalTo($exception), $this->isInstanceOf(Scope::class), null); | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::WARNING); | ||
| $record = RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => $exception, | ||
| ], | ||
| [] | ||
| ); | ||
|
|
||
| $this->assertTrue($handler->isHandling($record)); | ||
| $this->assertFalse($handler->handle($record)); | ||
| } | ||
|
|
||
| public function testHandleReturnsTrueWhenBubblingDisabled(): void | ||
| { | ||
| $exception = new \RuntimeException('boom'); | ||
|
|
||
| /** @var ClientInterface&MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('captureException') | ||
| ->with($this->identicalTo($exception), $this->isInstanceOf(Scope::class), null); | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::WARNING, false); | ||
| $record = RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => $exception, | ||
| ], | ||
| [] | ||
| ); | ||
|
|
||
| $this->assertTrue($handler->isHandling($record)); | ||
| $this->assertTrue($handler->handle($record)); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider ignoredRecordsDataProvider | ||
| * | ||
| * @param LogRecord|array<string, mixed> $record | ||
| */ | ||
| public function testHandleIgnoresRecordsWithoutThrowable($record): void | ||
| { | ||
| /** @var ClientInterface&MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->never()) | ||
| ->method('captureException'); | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::DEBUG, false); | ||
|
|
||
| $this->assertTrue($handler->isHandling($record)); | ||
| $this->assertFalse($handler->handle($record)); | ||
| } | ||
|
|
||
| public function testHandleIgnoresRecordsBelowThreshold(): void | ||
| { | ||
| $exception = new \RuntimeException('boom'); | ||
|
|
||
| /** @var ClientInterface&MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->never()) | ||
| ->method('captureException'); | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($client, new Scope()), Logger::ERROR, false); | ||
| $record = RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => $exception, | ||
| ], | ||
| [] | ||
| ); | ||
|
|
||
| $this->assertFalse($handler->isHandling($record)); | ||
| $this->assertFalse($handler->handle($record)); | ||
| } | ||
|
|
||
| public function testLegacyIsHandlingUsesMinimalLevelRecord(): void | ||
| { | ||
| if (Logger::API >= 3) { | ||
| $this->markTestSkipped('Test only works for Monolog < 3'); | ||
| } | ||
|
|
||
| $handler = new SentryExceptionHandler(new Hub($this->createMock(ClientInterface::class), new Scope()), Logger::WARNING); | ||
|
|
||
| $this->assertTrue($handler->isHandling(['level' => Logger::WARNING])); | ||
| $this->assertFalse($handler->isHandling(['level' => Logger::INFO])); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<array{LogRecord|array<string, mixed>}> | ||
| */ | ||
| public static function ignoredRecordsDataProvider(): iterable | ||
| { | ||
| yield [ | ||
| RecordFactory::create('foo bar', Logger::WARNING, 'channel.foo', [], []), | ||
| ]; | ||
|
|
||
| yield [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => 'not an exception', | ||
| ], | ||
| [] | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<array{LogRecord|array<string, mixed>, \Throwable, array<string, mixed>}> | ||
| */ | ||
| public static function capturedRecordsDataProvider(): iterable | ||
| { | ||
| $exception = new \RuntimeException('exception message'); | ||
|
|
||
| yield 'with exception only' => [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => $exception, | ||
| ], | ||
| [] | ||
| ), | ||
| $exception, | ||
| [ | ||
| 'monolog.channel' => 'channel.foo', | ||
| 'monolog.level' => Logger::getLevelName(Logger::WARNING), | ||
| 'monolog.message' => 'foo bar', | ||
| ], | ||
| ]; | ||
|
|
||
| $exception = new \RuntimeException('exception message'); | ||
|
|
||
| yield 'with context and extra' => [ | ||
| RecordFactory::create( | ||
| 'foo bar', | ||
| Logger::WARNING, | ||
| 'channel.foo', | ||
| [ | ||
| 'exception' => $exception, | ||
| 'foo' => 'bar', | ||
| ], | ||
| [ | ||
| 'bar' => 'baz', | ||
| ] | ||
| ), | ||
| $exception, | ||
| [ | ||
| 'monolog.channel' => 'channel.foo', | ||
| 'monolog.level' => Logger::getLevelName(Logger::WARNING), | ||
| 'monolog.message' => 'foo bar', | ||
| 'monolog.context' => [ | ||
| 'foo' => 'bar', | ||
| ], | ||
| 'monolog.extra' => [ | ||
| 'bar' => 'baz', | ||
| ], | ||
| ], | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚲 : Not sure about the name, it's a bit redundant and generic so I'm up for suggestions