cltv/yii3sentry

1.0.2 2022-08-07 21:05 UTC

This package is auto-updated.

Last update: 2024-09-08 22:44:52 UTC


README

此实现使用日志进行sentry跟踪。

安装

针对gitlab

你应该将仓库添加到你的项目中

    composer config repositories.<gitlab domain>/109 '{"type": "composer", "url": "https://<gitlab domain>/api/v4/group/109/-/packages/composer/packages.json"}'

如果你遇到404错误,你应该通过命令在composer.json目录中添加auth.json文件,并将其添加到.gitignore文件中

    composer config gitlab-token.<DOMAIN-NAME> <personal_access_token>

配置

将以下代码块添加到你的params.php中,并输入你的DSN;你也可以定义你的环境和版本,例如从gitlab.ci中的TAG

    'sentry' =>
        [
            'options' => [
                'dsn' => '',
                'environment' => 'local', //SENTRY_ENVIRONMENT, //YII_ENV,
                'release' => 'dev',  //SENTRY_RELEASE, //TAG
                // @see: https://docs.sentry.io/platforms/php/configuration/options/#send-default-pii
                'send_default_pii' => true,
                'traces_sample_rate' => 1.0,
            ],
            'log_level' => 'warning',
            'tracing'          => [
                // Indicates if the tracing integrations supplied by Sentry should be loaded
                'default_integrations'   => true,
            ],
        ]

将APP_START_TIME常量添加到index.php和yii.php中

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

在app/config/common/logger.php或具有日志设置的其他配置文件中添加日志目标以显示面包屑和跟踪

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

如果你想在sentry时间轴中查看你的日志,你需要在日志上下文数组中使用键(float)'time'和(float)'elapsed'

在app/config/params.php中添加数据库日志装饰器以跟踪数据库查询

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

在app/config/params.php的中间件部分添加SetRequestIpMiddleware

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

在app/config/common/router.php中添加跟踪中间件

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

        return new RouteCollection($collector);
    },

如果你想跟踪guzzle请求并添加sentry头到外部查询,请将其添加到你的config/httpClient.php或另一个工厂配置文件中

    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,
        ]);
    },

如果你的事务太重,你可以通过清除日志缓冲区将事务分割成几个事务。

使用SentryConsoleTransactionAdapter或SentryWebTransactionAdapter

例如

        /** 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 */

对于这个示例,所有新事务都将链接到具有$sentryTraceString的事务