PHP code example of tzabzlat / yii2-sentry
1. Go to this page and download the library: Download tzabzlat/yii2-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/ */
tzabzlat / yii2-sentry example snippets
'bootstrap' => ['sentry'],
'log' => [
'logger' => 'tzabzlat\yii2sentry\Logger',
]
'components' => [
'sentry' => [
'class' => 'tzabzlat\yii2sentry\SentryComponent',
'dsn' => 'https://[email protected] /project',
'environment' => YII_ENV,
// Sampling rate (percentage of requests for performance metrics collection)
'tracesSampleRatePercent' => YII_ENV_PROD ? 5 : 100,
// Additional tags for all events
'tags' => [
'application' => 'app-api',
'app_version' => '1.0.0',
],
],
]
// Simple span
Yii::$app->sentry->trace('Operation name', function() {
// Your code here
heavyOperation();
});
// With additional data
Yii::$app->sentry->trace(
'Data import',
function() {
// Import data
return $result;
},
'custom.import', // Operation type
[
'source' => 'api',
'records_count' => $count
]
);
// Span with exception handling
try {
Yii::$app->sentry->trace('Critical operation', function() {
// In case of an exception, the span will be marked as failed
throw new \Exception('Error!');
});
} catch (\Exception $e) {
// The exception will be caught here
// The span is already marked as failed in Sentry
}
'sentry' => [
'class' => 'tzabzlat\yii2sentry\SentryComponent',
'dsn' => env('SENTRY_DSN', ''),
'environment' => YII_ENV,
'tracesSampleRatePercent' => YII_ENV_PROD ? 20 : 100,
'collectorsConfig' => [
// LogCollector configuration
'logCollector' => [
'targetOptions' => [
'levels' => ['error', 'warning'], // Log levels to send
'except' => ['yii\web\HttpException:404'], // Exceptions
'exceptMessages' => [
'/^Informational message/' => true, // Exclude by pattern
],
],
],
// DbCollector configuration
'dbCollector' => [
'slowQueryThreshold' => 100, // Threshold in ms for slow queries
],
// HttpClientCollector with sensitive URL masking
'httpClientCollector' => [
'urlMaskPatterns' => [
'|https://api\.telegram\.org/bot([^/]+)/|' => 'https://api.telegram.org/bot[HIDDEN]/',
],
],
// RequestCollector configuration
'requestCollector' => [
'captureUser' => true, // Capture user ID
],
],
]
'collectorsConfig' => [
'dbCollector' => false, // Disables the database collector
'httpClientCollector' => false, // Disables the HTTP client collector
],
namespace app\components\sentry;
use tzabzlat\yii2sentry\collectors\BaseCollector;
use tzabzlat\yii2sentry\SentryComponent;
use Sentry\Breadcrumb;
use Sentry\State\Scope;
use Yii;
class MyCustomCollector extends BaseCollector
{
// Collector configuration
public $someOption = 'default';
/**
* Attaches the collector to Sentry
*/
public function attach(SentryComponent $sentryComponent): bool
{
parent::attach($sentryComponent);
// Connect to Yii2 events
\yii\base\Event::on(SomeClass::class, SomeClass::EVENT_NAME, function($event) {
$this->handleEvent($event);
});
return true;
}
/**
* Sets additional tags
*/
public function setTags(Scope $scope): void
{
$scope->setTag('custom_tag', 'value');
}
/**
* Handles a custom event
*/
protected function handleEvent($event)
{
// Create a span for tracking
$span = $this->sentryComponent->startSpan(
'My Custom Operation',
'custom.operation',
[
'key' => 'value',
'event_type' => get_class($event)
]
);
// Add a breadcrumb to the timeline
$this->addBreadcrumb(
'My Event Happened',
['data' => 'value'],
Breadcrumb::LEVEL_INFO,
'custom'
);
// Finish the span
if ($span) {
$this->sentryComponent->finishSpan($span, [
'result' => 'success',
'additional_data' => $someValue
]);
}
}
}
'sentry' => [
// ...
'collectorsConfig' => [
'myCustomCollector' => [
'class' => 'app\components\sentry\MyCustomCollector',
'someOption' => 'custom value',
],
],
],