PHP code example of compono-kit / sentry-client

1. Go to this page and download the library: Download compono-kit/sentry-client 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/ */

    

compono-kit / sentry-client example snippets


use ComponoKit\SentryClient\SentryClient;
use ComponoKit\SentryClient\SentryClientConfig;

$client = new SentryClient(new SentryClientConfig([
    'dsn'             => 'https://[email protected]/456',
    'environment'     => 'production',
    'release'         => '[email protected]',
    'error-reporting' => E_ALL,
    'display-errors'  => false,
]));

$client->install(); // applies error_reporting + display_errors

use ComponoKit\SentryClient\Interfaces\InformsSentry;

class OrderService
{
    public function __construct(private InformsSentry $sentry) {}

    public function place(Order $order): void
    {
        try {
            // ...
        } catch (\Throwable $exception) {
            $this->sentry->captureException($exception);
            throw $exception;
        }
    }
}

use ComponoKit\SentryClient\NullSentryClient;

$service = new OrderService(new NullSentryClient());

use ComponoKit\SentryClient\Models\Types\Severity;

$client->captureException($exception);

// with severity
$client->captureException($exception, Severity::fatal());

use ComponoKit\SentryClient\Models\Types\Message;
use ComponoKit\SentryClient\Models\Types\Severity;

$client->captureMessage(new Message('Queue depth exceeded threshold'), Severity::warning());

@file_get_contents('/missing/file');

$client->captureLastError();                       // severity defaults to error
$client->captureLastError(Severity::warning());

use ComponoKit\SentryClient\Models\Tags;

$client->useTags(Tags::fromKeyValueArray([
    'app'     => 'checkout-service',
    'version' => '2.1.0',
]));

use ComponoKit\SentryClient\Models\User;

$client->setUser(new User(
    id:        (string) $user->id,
    email:     $user->email,
    username:  $user->name,
));

use ComponoKit\SentryClient\Models\Context;
use ComponoKit\SentryClient\Models\ContextEntry;
use ComponoKit\SentryClient\Models\ContextGroup;

$context = new Context([
    new ContextGroup('order', [
        new ContextEntry('id',     '#1042'),
        new ContextEntry('amount', '149.90 EUR'),
    ]),
]);

$client->captureException($exception, Severity::error(), $context);
$client->captureMessage(new Message('Payment declined'), Severity::warning(), $context);

$context = Context::fromKeyValueArray([
    'order'   => ['id' => '#1042', 'amount' => '149.90 EUR'],
    'payment' => ['provider' => 'stripe', 'status' => 'declined'],
]);

use ComponoKit\SentryClient\Models\Breadcrumb;

$client->addBreadcrumb(new Breadcrumb('User clicked checkout',    'ui',   'info'));
$client->addBreadcrumb(new Breadcrumb('Sending payment request',  'http', 'debug', ['url' => '/pay']));
$client->addBreadcrumb(new Breadcrumb('Payment gateway timeout',  'http', 'error'));

$client->captureException($exception);

use ComponoKit\SentryClient\Models\Fingerprint;

$client->captureException(
    $exception,
    Severity::error(),
    null,
    new Fingerprint('PaymentService', 'timeout'),
);

use ComponoKit\SentryClient\Interfaces\ConfiguresSentryClient;
use ComponoKit\SentryClient\Models\Types\Dsn;
use ComponoKit\SentryClient\Models\Types\Environment;
use ComponoKit\SentryClient\Models\Types\Release;

class EnvSentryConfig implements ConfiguresSentryClient
{
    public function getDsn(): Dsn                   { return new Dsn($_ENV['SENTRY_DSN']); }
    public function getEnvironment(): Environment   { return new Environment($_ENV['APP_ENV']); }
    public function getRelease(): Release           { return new Release($_ENV['APP_VERSION']); }
    public function getErrorReporting(): int        { return E_ALL; }
    public function shouldDisplayErrors(): bool     { return false; }
    public function getExtraOptions(): array        { return []; }
}

new SentryClientConfig([
    // ...traces_sample_rate' => 0.2,
        'before_send'        => function (\Sentry\Event $event): ?\Sentry\Event {
            // drop events from bots
            if (str_contains((string) $event->getRequest()?->getHeaders()['User-Agent'] ?? '', 'bot')) {
                return null;
            }
            return $event;
        },
    ],
]);