Skip to content

Commit

Permalink
Merge pull request #17 from netlogix/feature/editorconfig
Browse files Browse the repository at this point in the history
FEATURE: Add editorconfig & reformat code
  • Loading branch information
stephanschuler authored Nov 19, 2021
2 parents 08e823d + 63be766 commit 22cb8e5
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 85 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The IDE is assumed to use PSR-2 for PHP files.

root = true

[*]
indent_style = space
indent_size = 4
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{yml,yaml}]
indent_size = 2
tab_width = 2
2 changes: 1 addition & 1 deletion Classes/Command/SentryCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Netlogix\Sentry\Command;

use Neos\Flow\Cli\CommandController;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Netlogix\Sentry\Exception\Test;
use Netlogix\Sentry\Scope\ScopeProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace Netlogix\Sentry\ExceptionHandler;

use Neos\Flow\Error\AbstractExceptionHandler;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Error\AbstractExceptionHandler;
use RuntimeException;
use Throwable;

/**
Expand Down Expand Up @@ -38,14 +39,14 @@ protected function echoExceptionWeb($exception)
self::throwWhenUsed();
}

protected function echoExceptionCli(\Throwable $exception)
protected function echoExceptionCli(Throwable $exception)
{
self::throwWhenUsed();
}

private static function throwWhenUsed(): void
{
throw new \RuntimeException('This Exception Handler should not be used!', 1612044864);
throw new RuntimeException('This Exception Handler should not be used!', 1612044864);
}

}
2 changes: 1 addition & 1 deletion Classes/Integration/NetlogixIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static function configureScopeForEvent(Event $event, EventHint $hint): v
return;
}

$configureEvent = function() use ($event, $scopeProvider) {
$configureEvent = function () use ($event, $scopeProvider) {
$event->setEnvironment($scopeProvider->collectEnvironment());
$event->setExtra($scopeProvider->collectExtra());
$event->setRelease($scopeProvider->collectRelease());
Expand Down
23 changes: 14 additions & 9 deletions Classes/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();

$dispatcher->connect(ConfigurationManager::class, 'configurationManagerReady', static function(ConfigurationManager $configurationManager) {
$dsn = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Netlogix.Sentry.dsn');
$dispatcher->connect(
ConfigurationManager::class,
'configurationManagerReady',
static function (ConfigurationManager $configurationManager) {
$dsn = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
'Netlogix.Sentry.dsn');

init([
'dsn' => $dsn,
'integrations' => [
new NetlogixIntegration(),
]
]);
});
init([
'dsn' => $dsn,
'integrations' => [
new NetlogixIntegration(),
]
]);
}
);
}

}
3 changes: 1 addition & 2 deletions Classes/Scope/ScopeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ public function withThrowable(Throwable $t, callable $do): void
}

/**
* @api
*
* @return Throwable|null
* @api
*/
public function getCurrentThrowable(): ?Throwable
{
Expand Down
39 changes: 23 additions & 16 deletions Classes/ThrowableStorage/CompoundStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

namespace Netlogix\Sentry\ThrowableStorage;

use Closure;
use InvalidArgumentException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Annotations as Flow;
use RuntimeException;
use Throwable;

/**
* @Flow\Proxy(false)
Expand All @@ -22,17 +25,17 @@ final class CompoundStorage implements ThrowableStorageInterface
private $initialized = false;

/**
* @var \Closure
* @var Closure
*/
private $initializeStoragesClosure;

/**
* @var \Closure
* @var Closure
*/
protected $requestInformationRenderer;

/**
* @var \Closure
* @var Closure
*/
protected $backtraceRenderer;

Expand All @@ -56,10 +59,12 @@ public static function createWithOptions(array $options): ThrowableStorageInterf

foreach ($storagesFromOptions as $storageClassName) {
if (!is_a($storageClassName, ThrowableStorageInterface::class, true)) {
throw new InvalidArgumentException(sprintf('Class "%s" must implement ThrowableStorageInterface', $storageClassName), 1612095174);
throw new InvalidArgumentException(sprintf('Class "%s" must implement ThrowableStorageInterface',
$storageClassName), 1612095174);
}
if (is_a($storageClassName, CompoundStorage::class, true)) {
throw new InvalidArgumentException('Cannot use CompoundStorage as Storage for CompoundStorage', 1612096699);
throw new InvalidArgumentException('Cannot use CompoundStorage as Storage for CompoundStorage',
1612096699);
}
$storageClassNames[] = $storageClassName;
}
Expand All @@ -69,7 +74,7 @@ public static function createWithOptions(array $options): ThrowableStorageInterf
return new CompoundStorage($primaryStorageClassName, ... $storageClassNames);
}

private function __construct(string $primaryStorageClassName, string ... $additionalStorageClassNames)
private function __construct(string $primaryStorageClassName, string ...$additionalStorageClassNames)
{
$this->initializeStoragesClosure = function () use ($primaryStorageClassName, $additionalStorageClassNames) {
$this->primaryStorage = $this->createStorage($primaryStorageClassName);
Expand All @@ -81,7 +86,7 @@ private function __construct(string $primaryStorageClassName, string ... $additi
};
}

public function logThrowable(\Throwable $throwable, array $additionalData = [])
public function logThrowable(Throwable $throwable, array $additionalData = [])
{
if (!$this->initializeStorages()) {
// could not initialize storages, throw exception
Expand All @@ -90,33 +95,35 @@ public function logThrowable(\Throwable $throwable, array $additionalData = [])

$message = $this->primaryStorage->logThrowable($throwable, $additionalData);

array_walk($this->additionalStorages, static function(ThrowableStorageInterface $storage) use ($throwable, $additionalData) {
$storage->logThrowable($throwable, $additionalData);
});
array_walk($this->additionalStorages,
static function (ThrowableStorageInterface $storage) use ($throwable, $additionalData) {
$storage->logThrowable($throwable, $additionalData);
});

return $message;
}

public function setRequestInformationRenderer(\Closure $requestInformationRenderer)
public function setRequestInformationRenderer(Closure $requestInformationRenderer)
{
$this->requestInformationRenderer = $requestInformationRenderer;
}

public function setBacktraceRenderer(\Closure $backtraceRenderer)
public function setBacktraceRenderer(Closure $backtraceRenderer)
{
$this->backtraceRenderer = $backtraceRenderer;
}

private function createStorage(string $storageClassName): ThrowableStorageInterface
{
if (!Bootstrap::$staticObjectManager) {
throw new \RuntimeException('Bootstrap::$staticObjectManager is not set yet', 1612434395);
throw new RuntimeException('Bootstrap::$staticObjectManager is not set yet', 1612434395);
}

assert(is_a($storageClassName, ThrowableStorageInterface::class, true));
$bootstrap = Bootstrap::$staticObjectManager->get(Bootstrap::class);
$configurationManager = $bootstrap->getEarlyInstance(ConfigurationManager::class);
$settings = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Flow');
$settings = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
'Neos.Flow');
$storageOptions = $settings['log']['throwables']['optionsByImplementation'][$storageClassName] ?? [];

$storage = $storageClassName::createWithOptions($storageOptions);
Expand All @@ -138,7 +145,7 @@ private function initializeStorages(): bool

try {
($this->initializeStoragesClosure)();
} catch (\Throwable $t) {
} catch (Throwable $t) {
return false;
}

Expand Down
9 changes: 5 additions & 4 deletions Classes/ThrowableStorage/SentryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace Netlogix\Sentry\ThrowableStorage;

use Neos\Flow\Log\ThrowableStorageInterface;
use Closure;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\ThrowableStorageInterface;
use Sentry\State\Scope;
use Throwable;
use function Sentry\captureException;
Expand All @@ -24,7 +25,7 @@ public static function createWithOptions(array $options): ThrowableStorageInterf

public function logThrowable(Throwable $throwable, array $additionalData = [])
{
withScope(function(Scope $scope) use (&$eventId, $throwable, $additionalData) {
withScope(function (Scope $scope) use (&$eventId, $throwable, $additionalData) {
$scope->setExtras($additionalData);
$eventId = captureException($throwable);
});
Expand All @@ -36,11 +37,11 @@ public function logThrowable(Throwable $throwable, array $additionalData = [])
return '';
}

public function setRequestInformationRenderer(\Closure $requestInformationRenderer)
public function setRequestInformationRenderer(Closure $requestInformationRenderer)
{
}

public function setBacktraceRenderer(\Closure $backtraceRenderer)
public function setBacktraceRenderer(Closure $backtraceRenderer)
{
}

Expand Down
55 changes: 32 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Netlogix.Sentry

## About
This package provides a Flow integration for the [sentry.io](https://sentry.io) PHP SDK. Some basic
information about the Flow application is added to the sentry Event by default, but you can easily
configure and extend this package to fit your needs.

This package provides a Flow integration for the [sentry.io](https://sentry.io) PHP SDK. Some basic information about
the Flow application is added to the sentry Event by default, but you can easily configure and extend this package to
fit your needs.

## Installation

`composer require netlogix/sentry`

Currently the following Flow versions are supported:
Expand All @@ -17,29 +19,32 @@ Currently the following Flow versions are supported:
## Setup

The sentry DSN Client Key has to be configured. Get it from your project settings (SDK Setup -> Client Keys (DSN)).

```yaml
Netlogix:
Sentry:
dsn: 'https://[email protected]/123456789'
```
Then simply run `./flow sentry:test` to log an exception to sentry.
While this is technically all you **have to** do, you might want to adjust the providers - see below.
Then simply run `./flow sentry:test` to log an exception to sentry. While this is technically all you **have to** do,
you might want to adjust the providers - see below.

## Configuration

This package allows you to configure which data should be added to the sentry event by changing the providers
for each scope. Currently, the available scopes are `environment`, `extra`, `release`, `tags` and `user`.

Providers can be sorted using the [PositionalArraySorter](https://github.com/neos/utility-arrays/blob/master/Classes/PositionalArraySorter.php#L15) position strings.
For the scopes `extra`, `tags` and `user`, all data provided will be merged together. The scopes `environment` and `release` only support a **single** value (you can still configure more than one provider, but the last one wins).
This package allows you to configure which data should be added to the sentry event by changing the providers for each
scope. Currently, the available scopes are `environment`, `extra`, `release`, `tags` and `user`.

Providers can be sorted using
the [PositionalArraySorter](https://github.com/neos/utility-arrays/blob/master/Classes/PositionalArraySorter.php#L15)
position strings. For the scopes `extra`, `tags` and `user`, all data provided will be merged together. The
scopes `environment` and `release` only support a **single** value (you can still configure more than one provider, but
the last one wins).

```yaml
Netlogix:
Sentry:
scope:
extra: []
extra: [ ]
release:
# If you don't need a specific order, you can simply set the provider to true
Expand All @@ -57,8 +62,10 @@ Netlogix:
```

## Environments
The sentry SDK will search for the environment variable `SENTRY_ENVIRONMENT` and use it's value as the current environment. This is still the default, however
you can configure the `Netlogix\Sentry\Scope\Environment\FlowSettings` provider to use a different value:

The sentry SDK will search for the environment variable `SENTRY_ENVIRONMENT` and use it's value as the current
environment. This is still the default, however you can configure the `Netlogix\Sentry\Scope\Environment\FlowSettings`
provider to use a different value:

```yaml
Netlogix:
Expand All @@ -68,10 +75,11 @@ Netlogix:
```

## Release tracking
You can use the `Netlogix\Sentry\Scope\Release\PathPattern` `ReleaseProvider` to extract your current release from
the app directory. By default, the configured `pathPattern` is matched against the `FLOW_PATH_ROOT` constant:

````yaml
You can use the `Netlogix\Sentry\Scope\Release\PathPattern` `ReleaseProvider` to extract your current release from the
app directory. By default, the configured `pathPattern` is matched against the `FLOW_PATH_ROOT` constant:

```yaml
Netlogix:
Sentry:
Expand All @@ -83,10 +91,10 @@ Netlogix:
# Pattern to extract current release from file path
# This pattern is matched against pathToMatch
pathPattern: '~/releases/(\d{14})$~'
````
```

You can also use the `Netlogix\Sentry\Scope\Release\FlowSettings` to set the Release
through Flow Configuration (`Netlogix.Sentry.release.setting`, set to `%env:SENTRY_RELEASE%` by default).
You can also use the `Netlogix\Sentry\Scope\Release\FlowSettings` to set the Release through Flow
Configuration (`Netlogix.Sentry.release.setting`, set to `%env:SENTRY_RELEASE%` by default).

## Custom Providers

Expand All @@ -101,6 +109,7 @@ For each scope, you can implement your own providers. Each scope requires it's o
Then simply add them to the configuration.

If you need access to the thrown exception, you can check `Netlogix\Sentry\Scope\ScopeProvider::getCurrentThrowable()`:

```php
<?php
Expand Down Expand Up @@ -167,8 +176,8 @@ class LoggingManually {

## Ignoring exceptions

If you need to skip sending a specific exception to sentry, you can use Flow's `renderingGroups`. Simply create one
that matches your exception and set `logException` to `false`:
If you need to skip sending a specific exception to sentry, you can use Flow's `renderingGroups`. Simply create one that
matches your exception and set `logException` to `false`:

```yaml
Neos:
Expand All @@ -178,8 +187,8 @@ Neos:
renderingGroups:
ignoredExceptions:
matchingStatusCodes: [418]
matchingExceptionClassNames: ['Your\Ignored\Exception']
matchingStatusCodes: [ 418 ]
matchingExceptionClassNames: [ 'Your\Ignored\Exception' ]
options:
logException: false
```
Expand Down
Loading

0 comments on commit 22cb8e5

Please sign in to comment.