webvork/yii-sentry
Composer 安装命令:
composer require webvork/yii-sentry
包简介
A Sentry integration for Yii Framework
关键字:
README 文档
README
Yii Sentry
The package provides Sentry integration for Yii Framework
Installation
The package needs PSR-compatible HTTP client and factories so require it additionally to this package:
composer require httpsoft/http-message composer require php-http/guzzle7-adapter composer require webvork/yii-sentry
The first two can be replaced to other packages of your choice.
For handling console errors yii-console and yii-event packages are required additionally:
composer require yiisoft/yii-console composer require yiisoft/yii-event
Configure HTTP factories and client (usually that is config/common/sentry.php):
<?php 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), ], ], ];
If you want to trace Guzzle requests and add Sentry headers to external queries, add the following:
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, ]); },
Configure:
Add the following code block to your params.php and define DSN. Also you can set "environment" and "release". Good example is to use TAG from gitlab.ci for it.
'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, ], ]
Add APP_START_TIME constant into index.php and yii.php:
define('APP_START_TIME', microtime(true));
Add log targets for breadcrumbs and tracing to app/config/common/logger.php or another config file with logger settings:
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 ]); } ];
Note: If you want to see your logs in sentry timeline, you need to use keys (float)'time' and (float)'elapsed' in log context array.
Add DB log decorator for tracing db queries in app/config/params.php:
(now it is available only for postgres, it will work with another db, but can't separate system queries from user queries correctly)
'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 **/ ] ]
Add SetRequestIpMiddleware to app/config/params.php, "middleware" section:
'middlewares' => [ ErrorCatcher::class, \Yiisoft\Yii\Sentry\Http\SetRequestIpMiddleware::class, //add this Router::class, ],
Add SentryTraceMiddleware to app/config/common/router.php:
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); },
If your transaction is too heavy, you can slice it to several transactions with clearing log buffer. Use SentryConsoleTransactionAdapter or SentryWebTransactionAdapter. For example:
/** 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 */
In this example all new transactions will linked to transaction with $sentryTraceString.
In options you can also pass additional Sentry configuration. See
official Sentry docs for keys and values.
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/infection
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
webvork/yii-sentry 适用场景与选型建议
webvork/yii-sentry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 435 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sentry」 「yii」 「sentry-sdk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webvork/yii-sentry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webvork/yii-sentry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webvork/yii-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 Sentry integration for Yii Framework
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Simple Yii2 Sentry target for logging (http://getsentry.com)
Monitoring for scheduled jobs
统计信息
- 总下载量: 435
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2024-01-05