Sentry integration
What is Sentry
Sentry is a tool for monitoring and debugging application stability and performance. Sentry gives you access to the events that you send there from your application.
Most often, Sentry is used for monitoring errors (exceptions). You can enrich errors with context to better understand the problem:
- Request arguments
- Tags for grouping exceptions
- Environment state: environment variables, application state, and other global attributes
You can find the full list of features on the official website: https://sentry.io/welcome/
Installation
Install the package
Install the required package yiisoft/yii-sentry with the following command:
composer require yiisoft/yii-sentry --prefer-distInstall an HTTP driver
The getsentry/sentry-php library requires the php-http/httplug package and any HTTP driver. In the example below we’ll use the Guzzle adapter.
You can find the list of all adapters on this page.
To install the packages, run the following command:
composer require php-http/httplug php-http/guzzle7-adapter --prefer-distConfiguration
Get and store the token
Next, configure the application.
First, register at Sentry and create a project.
Then, in the project settings on the “General Settings” tab, find the “Security Token” field and copy its value.
Now put this token into the package configuration. By default, the config is located at config/packages/yiisoft/yii-sentry/config/params.php. Set the copied token as the value of the array element at yiisoft/yii-sentry => options => dsn. Example:
'yiisoft/yii-sentry' => [
'enabled' => true,
'options' => [
- 'dsn' => '',
+ 'dsn' => 'TOKEN',
],
],Configure the HTTP client
After installing the HTTP client, configure it.
Create the file config/common/sentry.php and put the following code into it:
<?php
declare(strict_types=1);
return [
\Http\Client\HttpClient::class => \GuzzleHttp\Client::class,
\Http\Client\HttpAsyncClient::class => [
'class' => \Http\Adapter\Guzzle7\Client::class,
'__construct()' => [
\Yiisoft\Factory\Definition\Reference::to(\Http\Client\HttpClient::class),
],
],
];Integration
Web
Sentry support for web is implemented as middleware.
That means you only need to add SentryMiddleware to the global middleware list in config/web/application.php:
return [
Yiisoft\Yii\Web\Application::class => [
'__construct()' => [
'dispatcher' => DynamicReference::to(static function (Injector $injector) {
return ($injector->make(MiddlewareDispatcher::class))
->withMiddlewares(
[
Router::class,
SubFolder::class,
+ SentryMiddleware::class,
ErrorCatcher::class,
]
);
}),
'fallbackHandler' => Reference::to(NotFoundHandler::class),
],
],
];Console
Sentry supports console via a handler for the ConsoleEvents::ERROR event.
The package provides a configuration file that automatically subscribes the application to this event.