Skip to content

Commit

Permalink
add unreportable exception and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjorgedev committed Dec 7, 2021
1 parent c32bf4b commit 5a657b8
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 7 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
1 change: 1 addition & 0 deletions config/config_default.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
ErrorException::class,
];

$config[SentryConstants::APPLICATION_VERSION] = '1.0.0';
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace TurbineKreuzberg\Service\Sentry\Exception;

use Exception;
use Throwable;

class UnreportableException extends Exception
{
/**
* @param \Throwable $throwable
*
* @return static
*/
public static function fromThrowable(Throwable $throwable): self
{
return new self($throwable->getMessage(), $throwable->getCode(), $throwable->getPrevious());
}
}
6 changes: 3 additions & 3 deletions src/TurbineKreuzberg/Service/Sentry/Gateway/SentryGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/TurbineKreuzberg/Service/Sentry/SentryConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getIgnoredExceptions(): array
*/
public function getReleaseVersion(): string
{
return getenv('APP_VERSION') ?: 'development';
return $this->get(SentryConstants::APPLICATION_VERSION, 'development');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions src/TurbineKreuzberg/Shared/Sentry/SentryConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
20 changes: 19 additions & 1 deletion tests/TurbineTest/Service/Sentry/SentryServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 5a657b8

Please sign in to comment.