PHP code example of sirix / sentry-psr

1. Go to this page and download the library: Download sirix/sentry-psr library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sirix / sentry-psr example snippets


return [
    'sentry' => [
        'dsn' => 'https://<key>@sentry.io/<project>',
        'environment' => 'production',
        'release' => '1.2.3',
    ],

    'sentry_psr' => [
        'isolate_http_scope' => true,
        'isolate_console_scope' => true,
        'set_current_hub' => true,
        'default_integrations' => false,
        'flush_on_http_error' => false,
        'flush_on_console_terminate' => true,
        'flush_timeout' => 2,
        'capture_http_request_context' => true,
        'capture_console_input' => true,
        'log_console_command_start' => true,
        'exception_filter' => [
            'enabled' => true,
            'ignore_classes' => [],
            'ignore_http_statuses' => [],
            'ignore_codes' => [],
            'ignore_message_patterns' => [],
            'inspect_previous' => true,
        ],
        'redaction' => [
            'replacement' => '[Filtered]',
            'sensitive_key_pattern' => '/password|passwd|secret|token|api[_-]?key|authorization|cookie/i',
            'max_depth' => 8,
            'max_items_per_container' => 100,
            'max_total_nodes' => 5000,
            'use_default_rules' => false,
            'rules' => [],
            'regex_rules' => [],
        ],
        'http_context' => [
            'enabled' => true,
            'capture_headers' => false,
            'capture_query_string' => false,
            'allowed_headers' => [
                'User-Agent',
                'X-Request-Id',
            ],
            'request_id_headers' => [
                'X-Request-Id',
                'X-Correlation-Id',
            ],
            'request_id_attributes' => [
                'request_id',
                'requestId',
                'correlation_id',
                'correlationId',
            ],
            'allowed_attributes' => [
                'route',
                'route_name',
                'request_id',
                'correlation_id',
            ],
        ],
    ],
];

'exception_filter' => [
    'enabled' => true,
    'ignore_classes' => [
        AuthExpiredException::class,
        UnauthorizedException::class,
    ],
    'ignore_http_statuses' => [
        401,
    ],
    'ignore_codes' => [],
    'ignore_message_patterns' => [
        '/token expired/i',
    ],
    'inspect_previous' => true,
],

use Sirix\SentryPsr\ExceptionFilter\ExceptionFilterContext;
use Sirix\SentryPsr\ExceptionFilter\ExceptionFilterInterface;

final readonly class AuthExceptionFilter implements ExceptionFilterInterface
{
    public function shouldCapture(\Throwable $throwable, ExceptionFilterContext $context): bool
    {
        if ($throwable instanceof AuthExpiredException) {
            return false;
        }

        if (
            ExceptionFilterContext::SOURCE_HTTP === $context->source
            && 401 === $throwable->getCode()
        ) {
            return false;
        }

        return true;
    }
}

'redaction' => [
    'replacement' => '*',
    'sensitive_key_pattern' => '/password|passwd|secret|token|api[_-]?key|authorization|cookie/i',
    'max_depth' => 8,
    'max_items_per_container' => 100,
    'max_total_nodes' => 5000,
    'use_default_rules' => false,
    'rules' => [
        'email' => ['type' => 'email'],
        'phone' => ['type' => 'phone'],
        'card_number' => ['type' => 'start_end', 'start' => 6, 'end' => 4],
    ],
    'regex_rules' => [
        [
            'pattern' => '/customer[_-]?name/i',
            'rule' => ['type' => 'name'],
        ],
    ],
],

'sentry_psr' => [
    'default_integrations' => true,
],

use Psr\Container\ContainerInterface;
use Sirix\SentryPsr\Middleware\SentryErrorMiddleware;

return function (App $app, ContainerInterface $container): void {
    $app->pipe(SentryErrorMiddleware::class);

    // routes and application middleware after Sentry
};

return [
    'sentry_psr' => [
        'isolate_http_scope' => true,
        'flush_on_http_error' => false,
        'flush_timeout' => 2,
    ],
];

use Sirix\SentryPsr\Lifecycle\SentryLifecycle;
use Sirix\SentryPsr\Reporter\SentryReporter;

final readonly class JobRunner
{
    public function __construct(
        private SentryLifecycle $lifecycle,
        private SentryReporter $reporter,
    ) {}

    public function run(Job $job): void
    {
        $this->lifecycle->withIsolatedScope(function () use ($job): void {
            $this->reporter->setTag('job', $job->name());
            $this->reporter->setContext('job', ['id' => $job->id()]);

            $job->handle();
        });

        $this->lifecycle->flush();
    }
}

use Sentry\State\HubInterface;
use Sirix\SentryPsr\ConsoleEventDispatcher\ConsoleEventDispatcherFactory;
use Sirix\SentryPsr\Lifecycle\SentryLifecycle;
use Sirix\SentryPsr\Listener\SentryCommandListener;
use Sirix\SentryPsr\Listener\SentryCommandListenerFactory;
use Symfony\Component\EventDispatcher\EventDispatcher;

return [
    'dependencies' => [
        'factories' => [
            HubInterface::class => Sirix\SentryPsr\Hub\SentryHubFactory::class,
            SentryLifecycle::class => Sirix\SentryPsr\Lifecycle\SentryLifecycleFactory::class,
            SentryCommandListener::class => SentryCommandListenerFactory::class,
            EventDispatcher::class => ConsoleEventDispatcherFactory::class,
        ],
    ],
];

use Sirix\SentryPsr\Reporter\SentryReporter;

final readonly class CheckoutService
{
    public function __construct(private SentryReporter $sentry) {}

    public function checkout(): void
    {
        $this->sentry->setTag('feature', 'checkout');
        $this->sentry->setUser(['id' => '123']);

        try {
            // ...
        } catch (\Throwable $exception) {
            $this->sentry->captureException($exception, ['step' => 'payment']);
            throw $exception;
        }
    }
}

'sentry_psr' => [
    'set_current_hub' => false,
],