PHP code example of webvork / yii-sentry

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

    

webvork / yii-sentry example snippets




declare(strict_types=1);

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle7\Client as GuzzleClientAdapter;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use HttpSoft\Message\UriFactory;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Yiisoft\Definitions\Reference;

return [
    // HTTP Factories
    StreamFactoryInterface::class => StreamFactory::class,
    RequestFactoryInterface::class => RequestFactory::class,
    LoggerInterface::class => NullLogger::class,
    UriFactoryInterface::class => UriFactory::class,
    ResponseFactoryInterface::class => ResponseFactory::class,
    // HTTP Client
    HttpClient::class => GuzzleClient::class,
    HttpAsyncClient::class => [
        'class' => GuzzleClientAdapter::class,
        '__construct()' => [
            Reference::to(HttpClient::class),
        ],
    ],
];
 
GuzzleHttp\Client::class => static function (ContainerInterface $container) {
    $stack = new HandlerStack();
    $stack->setHandler(new CurlHandler());
    $factory = $container->get(GuzzleMiddlewareFactory::class);
    $middleware = static function (callable $handler) use ($factory): callable {
        return $factory->factory($handler);
    };

    $stack->push($middleware);

    return new GuzzleHttp\Client([
        'handler' => $stack,
    ]);
},
 
'yiisoft/yii-sentry' => [
    'options' => [
        'dsn' => '',
        'environment' => 'local', //SENTRY_ENVIRONMENT, //YII_ENV,
        'release' => 'dev',  //SENTRY_RELEASE, //TAG
        // https://docs.sentry.io/platforms/php/configuration/options/#send-default-pii
        'send_default_pii' => true,
        'traces_sample_rate' => 1.0,
    ],
    'handleConsoleErrors' => true,
    'log_level' => 'warning',
    'tracing' => [
        // Indicates if the tracing integrations supplied by Sentry should be loaded
        'default_integrations'   => true,
        'guzzle_max_body' => 200,
    ],
]

define('APP_START_TIME', microtime(true));

return [
    LoggerInterface::class => static function (
        /** your_another_log_target $your_log_target */
        \Yiisoft\Yii\Sentry\SentryBreadcrumbLogTarget $sentryLogTarget,
        Yiisoft\Yii\Sentry\Tracing\SentryTraceLogTarget $sentryTraceLogTarget
    ) {
        return new Logger([
        /** $your_log_target */
            $sentryLogTarget,
            $sentryTraceLogTarget
        ]);
    }
];

'yiisoft/yii-cycle' => [
    // DBAL config
    'dbal' => [
        // SQL query logger. Definition of Psr\Log\LoggerInterface
        // For example, \Yiisoft\Yii\Cycle\Logger\StdoutQueryLogger::class
        'query-logger' => \Yiisoft\Yii\Sentry\PostgresLoggerDecorator::class,
        /**
         * ...
         * your another db settings 
         **/
    ]
]

    'middlewares' => [
        ErrorCatcher::class,
        \Yiisoft\Yii\Sentry\Http\SetRequestIpMiddleware::class, //add this
        Router::class,
    ],

  RouteCollectionInterface::class => static function (RouteCollectorInterface $collector) use ($config) {
        $collector
            ->middleware(FormatDataResponse::class)
            ->middleware(JsonParseMiddleware::class)
            ->middleware(ExceptionMiddleware::class)
            ->middleware(\Yiisoft\Yii\Sentry\Tracing\SentryTraceMiddleware::class) // add this
            ->addGroup(
                Group::create('')
                    ->routes(...$config->get('routes'))
            );

        return new RouteCollection($collector);
    },
 

/** some code with default transaction */
/** commit default transaction and send data to sentry server */
$sentryTraceString = $this->sentryTransactionAdapter->commit();
while ($currentDate <= $endDate) {
    $this->sentryTransactionAdapter->begin($sentryTraceString)
        ->setName('my_heavy_operation/iteration')
        ->setData(['date' => $currentDate->format('Y-m-d')]);

    $this->process($currentDate, $sentryTraceString);
    $this->sentryTransactionAdapter->commit();
}

$this->sentryTransactionAdapter->begin($sentryTraceString)
    ->setName('my_heavy_operation done, terminating application');
/** transaction will commit when application is terminated */