pekopt/yii2-sentry
Composer 安装命令:
composer require pekopt/yii2-sentry
包简介
Sentry integration for Yii2 framework with performance monitoring and error tracking
README 文档
README
Read this in other languages: English, Русский
Complete Sentry integration for Yii2 framework: logging, tracing and profiling.
Features
- Tracking errors and exceptions through Yii2 logs
- Database performance monitoring (slow queries, transactions)
- HTTP request tracing (incoming and outgoing)
- Manual spans for tracking performance of critical operations
- Flexible data collection configuration through collector system
- Sanitization of sensitive data (passwords, tokens, API keys)
Installation
Install the package via composer:
composer require pekopt/yii2-sentry
For using performance profiling features, you need to install the PHP Excimer extension.
Configuration
Basic Configuration
Add to your application configuration (not common):
'bootstrap' => ['sentry'], 'log' => [ 'logger' => 'pekopt\yii2sentry\Logger', ] 'components' => [ 'sentry' => [ 'class' => 'pekopt\yii2sentry\SentryComponent', 'dsn' => 'https://your-sentry-dsn@sentry.io/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', ], ], ]
Built-in Collectors
The package includes four main collectors, each responsible for its own monitoring area:
1. LogCollector
Collects and sends Yii2 logs with error and warning levels to Sentry. Allows configuring which logs should be sent and which should be ignored.
2. DbCollector
Tracks SQL queries, measures their performance, and creates spans in Sentry for analysis. Automatically marks slow queries. Also tracks database transactions.
3. HttpClientCollector
Tracks outgoing HTTP requests made through Yii2 HttpClient. Measures response time, records response status, and creates spans for visualizing HTTP dependencies.
4. RequestCollector
Tracks incoming HTTP requests to your application. Creates the main transaction for each request and collects information about the controller, action, processing time, and response status.
Usage
Manual Spans for Custom Operations
To create spans manually, use the trace method:
// 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 }
Collector Configuration
You can configure each collector separately through the collectorsConfig parameter:
'sentry' => [ 'class' => 'pekopt\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 ], ], ]
Disabling Collectors
To disable a specific collector, set its configuration to false:
'collectorsConfig' => [ 'dbCollector' => false, // Disables the database collector 'httpClientCollector' => false, // Disables the HTTP client collector ],
How Collectors Work
LogCollector
Connects a special LogTarget that intercepts logs with specified levels and sends them to Sentry. Processes exceptions as a separate type of event. Also supports filtering by categories and message patterns.
DbCollector
Overrides the standard Yii2 DbCommand and connects to query profiling events. Measures the execution time of each SQL query, determines the query type (SELECT, INSERT, etc.), and creates spans for visualization in Sentry. Tracks transactions through Connection events.
HttpClientCollector
Subscribes to request sending events through HttpClient. For each request, it creates a span with details of URL, method, headers, and request body (with sanitization of sensitive data). Measures response time and adds information about the response status.
RequestCollector
Creates the main transaction for each incoming HTTP request. Collects information about the route, controller, action, request parameters, and response. Measures the total request processing time and peak memory usage.
Creating a Custom Collector
You can create your own collector by implementing the CollectorInterface or extending the BaseCollector class:
namespace app\components\sentry; use pekopt\yii2sentry\collectors\BaseCollector; use pekopt\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 ]); } } }
Then add your collector to the configuration:
'sentry' => [ // ... 'collectorsConfig' => [ 'myCustomCollector' => [ 'class' => 'app\components\sentry\MyCustomCollector', 'someOption' => 'custom value', ], ], ],
Contributing
If you found a bug or have suggestions for improvement, feel free to:
- Create an issue with a description of the problem or suggestion
- Propose pull requests with fixes or new features
License
MIT
pekopt/yii2-sentry 适用场景与选型建议
pekopt/yii2-sentry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 152 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「monitoring」 「sentry」 「error tracking」 「yii2」 「apm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pekopt/yii2-sentry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pekopt/yii2-sentry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pekopt/yii2-sentry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fork of a PHP client for Sentry (http://getsentry.com) that work also with php5.2
An implementation of the Sentry User Manager for Laravel.
A Symfony bundle for chameleon-system/sanitycheck
Simple Yii2 Sentry target for logging (http://getsentry.com)
SoftWax Health Check Bundle for Symfony framework
1Pilot client for Symfony
统计信息
- 总下载量: 152
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-04