From 5a657b878dc2d69486b61da377d6c57efbe8cccd Mon Sep 17 00:00:00 2001 From: Jorge Murta Date: Tue, 7 Dec 2021 08:08:11 +0000 Subject: [PATCH] add unreportable exception and documentation --- README.md | 19 +++++++++++++++++- config/config_default.dist.php | 1 + .../Exception/SentryExceptionHandler.php | 6 +++++- .../Exception/UnreportableException.php | 19 ++++++++++++++++++ .../Service/Sentry/Gateway/SentryGateway.php | 6 +++--- .../Service/Sentry/SentryConfig.php | 2 +- .../Sentry/SentryDependencyProvider.php | 1 + .../Shared/Sentry/SentryConstants.php | 1 + .../Service/Sentry/SentryServiceTest.php | 20 ++++++++++++++++++- 9 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/TurbineKreuzberg/Service/Sentry/Exception/UnreportableException.php diff --git a/README.md b/README.md index 5da81ef..b45253b 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,24 @@ class MonitoringDependencyProvider extends SprykerMonitoringDependencyProvider // ... ]; } - } +``` + +## Additional Configurations +You can adjust some configurations on the sentry module by adding some of this lines to your config file (ex. config_default.php): +```php +// You can ignore certain exceptions by adding them to this array +$config[SentryConstants::IGNORED_EXCEPTIONS] = [ + ErrorException::class, // Example +]; + +// You can set your application version so that it gets reported to sentry +$config[SentryConstants::APPLICATION_VERSION] = '1.0.0'; + +// You can even get it from an env variable +$config[SentryConstants::APPLICATION_VERSION] = getenv('MY_APP_VERSION'); +// You can set the percentage of your requests that are going to be traced for +// performance monitoring, value goes from 0 to 1, being 0.2 = 20% +$config[SentryConstants::TRACE_SAMPLE_RATE] = 0.4; ``` diff --git a/config/config_default.dist.php b/config/config_default.dist.php index 50a50ef..beb38cd 100644 --- a/config/config_default.dist.php +++ b/config/config_default.dist.php @@ -15,3 +15,4 @@ ErrorException::class, ]; +$config[SentryConstants::APPLICATION_VERSION] = '1.0.0'; diff --git a/src/TurbineKreuzberg/Service/Sentry/Exception/SentryExceptionHandler.php b/src/TurbineKreuzberg/Service/Sentry/Exception/SentryExceptionHandler.php index dd9af14..1698e28 100644 --- a/src/TurbineKreuzberg/Service/Sentry/Exception/SentryExceptionHandler.php +++ b/src/TurbineKreuzberg/Service/Sentry/Exception/SentryExceptionHandler.php @@ -39,7 +39,11 @@ public function captureException(Throwable $throwable): void return; } - $this->sentryGateway->captureException($throwable); + $eventId = $this->sentryGateway->captureException($throwable); + + if ($eventId === null) { + $this->sentryGateway->captureException(UnreportableException::fromThrowable($throwable)); + } } /** diff --git a/src/TurbineKreuzberg/Service/Sentry/Exception/UnreportableException.php b/src/TurbineKreuzberg/Service/Sentry/Exception/UnreportableException.php new file mode 100644 index 0000000..b9259be --- /dev/null +++ b/src/TurbineKreuzberg/Service/Sentry/Exception/UnreportableException.php @@ -0,0 +1,19 @@ +getMessage(), $throwable->getCode(), $throwable->getPrevious()); + } +} diff --git a/src/TurbineKreuzberg/Service/Sentry/Gateway/SentryGateway.php b/src/TurbineKreuzberg/Service/Sentry/Gateway/SentryGateway.php index 3a3dd23..620de1e 100644 --- a/src/TurbineKreuzberg/Service/Sentry/Gateway/SentryGateway.php +++ b/src/TurbineKreuzberg/Service/Sentry/Gateway/SentryGateway.php @@ -28,11 +28,11 @@ public function __construct(HubInterface $sentryHub) /** * @param \Throwable $exception * - * @return void + * @return \Sentry\EventId|null */ - public function captureException(Throwable $exception): void + public function captureException(Throwable $exception) { - $this->sentryHub->captureException($exception); + return $this->sentryHub->captureException($exception); } /** diff --git a/src/TurbineKreuzberg/Service/Sentry/SentryConfig.php b/src/TurbineKreuzberg/Service/Sentry/SentryConfig.php index 585a934..39406fb 100644 --- a/src/TurbineKreuzberg/Service/Sentry/SentryConfig.php +++ b/src/TurbineKreuzberg/Service/Sentry/SentryConfig.php @@ -36,7 +36,7 @@ public function getIgnoredExceptions(): array */ public function getReleaseVersion(): string { - return getenv('APP_VERSION') ?: 'development'; + return $this->get(SentryConstants::APPLICATION_VERSION, 'development'); } /** diff --git a/src/TurbineKreuzberg/Service/Sentry/SentryDependencyProvider.php b/src/TurbineKreuzberg/Service/Sentry/SentryDependencyProvider.php index a06ee79..c886850 100644 --- a/src/TurbineKreuzberg/Service/Sentry/SentryDependencyProvider.php +++ b/src/TurbineKreuzberg/Service/Sentry/SentryDependencyProvider.php @@ -48,6 +48,7 @@ private function addSentryClientBuilder(Container $container): Container $options = [ 'environment' => $this->getConfig()->getApplicationEnvironment(), 'dsn' => $this->getConfig()->getDataSourceName(), + 'release' => $this->getConfig()->getReleaseVersion(), 'traces_sample_rate' => $this->getConfig()->getTraceSampleRate(), 'capture_silenced_errors' => $this->getConfig()->getCaptureSilencedErrors(), 'error_types' => $this->getConfig()->getErrorTypes(), diff --git a/src/TurbineKreuzberg/Shared/Sentry/SentryConstants.php b/src/TurbineKreuzberg/Shared/Sentry/SentryConstants.php index 181a2ba..a0b3c52 100644 --- a/src/TurbineKreuzberg/Shared/Sentry/SentryConstants.php +++ b/src/TurbineKreuzberg/Shared/Sentry/SentryConstants.php @@ -7,4 +7,5 @@ interface SentryConstants public const DSN = 'SENTRY:CONFIG:DSN'; public const IGNORED_EXCEPTIONS = 'SENTRY:CONFIG:IGNORED_EXCEPTIONS'; public const TRACE_SAMPLE_RATE = 'SENTRY:CONFIG:TRACE_SAMPLE_RATE'; + public const APPLICATION_VERSION = 'SENTRY:CONFIG:APPLICATION_VERSION'; } diff --git a/tests/TurbineTest/Service/Sentry/SentryServiceTest.php b/tests/TurbineTest/Service/Sentry/SentryServiceTest.php index ee89668..44f61c2 100644 --- a/tests/TurbineTest/Service/Sentry/SentryServiceTest.php +++ b/tests/TurbineTest/Service/Sentry/SentryServiceTest.php @@ -4,6 +4,7 @@ use Codeception\Test\Unit; use Exception; +use Sentry\EventId; use Sentry\State\HubInterface; use Sentry\Tracing\Transaction; use Sentry\Tracing\TransactionContext; @@ -57,7 +58,24 @@ public function testCaptureExceptionReportsToSentry(): void $mock ->expects($this->once()) ->method('captureException') - ->with($exception); + ->with($exception) + ->willReturn(new EventId('3eaf8ee807450b5aea065aec50b95451')); + + $this->getService()->captureException($exception); + } + + /** + * @return void + */ + public function testCaptureUnreportableExceptionReportsToSentry(): void + { + $mock = $this->createHubMock(); + $exception = new Exception('example exception'); + + $mock + ->expects($this->exactly(2)) + ->method('captureException') + ->willReturn(null); $this->getService()->captureException($exception); }