ivoglent/yii2-elastic-apm

Yii2 框架的 Elastic Agent

1.0.8 2020-09-27 12:00 UTC

This package is auto-updated.

Last update: 2024-08-27 22:51:41 UTC


README

Yii2 框架的 Elastic Agent

安装

通过 composer 安装此模块

composer require --prefer-dist --profile -vvv ivoglent/yii2-elastic-apm

配置

'apm' => [
    'class' => 'ivoglent\yii2\apm\Module',
    'configs' => [
        'agent' => [
            'serverUrl' => 'localhost:8200', //Host and port of APM server 
            'name' => 'Service name', //Service name 
            'token' => APM_TOKEN, //Token
        ],
        'skipExceptions' => [
            //List of exceptions which you want to ignore
            \yii\web\NotFoundHttpException::class,
            \yii\web\UnauthorizedHttpException::class,
        ],
        'skipCommands' => [
            //List of command you dont want to track
            'rabbitmq/consume',
        ],
    ],
    'enabled' => true // or false,
]

要启用数据库监控,请配置 DB 命令如下

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => '',
        'username' => '',
        'password' => '',
        'charset' => 'utf8mb4',
        'commandClass' => APM_ENABLED ? 'ivoglent\yii2\apm\components\db\mysql\Command' : '\yii\db\Command',
    ]
]

注意:记得将 apm 模块添加到 bootstrap 部分中

'bootstrap' => ['log', 'apm'],

事务

此模块将在 BEFORE_REQUEDT 事件后自动启动新事务。但您也可以在控制台应用程序中手动启动新事务,如消费者等

$transactionId = Uuid::uuid4()->toString();
$txtName = sprintf('consumer.%s', str_replace('-', '.', $queue->getName()));
Yii::$app->getModule('apm')->getAgent()->startTransaction($txtName, 'consumer', App::$app->getRequestId());
Yii::$app->getModule('apm')->getAgent()->setTransactionId($transactionId);

并停止

Yii::$app->getModule('apm')->getAgent()->stopTransaction();

跟踪

启动新跨度

$span = Yii::$app->getModule('apm')->getAgent()->startTrace('Process::'.$reflect->getShortName(), 'process');

并停止

Yii::$app->getModule('apm')->getAgent()->stopTrace($span->getId());

错误/异常通知

try {

} catch (\Exception $throwable) { Yii::$app->getModule('apm')->getAgent()->notifyException($throwable); }