1. Go to this page and download the library: Download alan/yii2-metrics 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/ */
alan / yii2-metrics example snippets
defined('YII_APP_NAME') or define('YII_APP_NAME', 'myYiiApplication');
namespace backend\controllers;
use yii\rest\Controller;
use yii2\metrics\MetricsTrait;
class MetricsController extends Controller {
use MetricsTrait;
public function behaviors(): array
{
$behaviors = parent::behaviors();
$this->fillIpControllerBehavior($behaviors, ['127.0.0.1']);//允许访问访路由的ip白名单
return $behaviors;
}
}
use yii\console\Controller;
use yii2\metrics\MetricsTrait;
class CrontabController extends Controller {
/**
* 上报rabbitMq任务剩余情况到prometheus
*/
public function actionRabbitMqMetrics()
{
//此该填写了backend模块的id
$factory = new \yii2\metrics\rabbitMq\Factory(YII_APP_NAME, 'app-backend');
try {
foreach ($factory->getIterator() as $task) {
\yii2\crontab\base\Crontab::instance()->withTargetData($task)->withHandler($task)->run();
}
}catch (\Exception $exception) {
echo sprintf("actionCheckQueues has exception msg:%s, at file:%s, at line: %d, trace: %s\n",
$exception->getMessage(), $exception->getFile(), $exception->getLine(), '');
}
}
}