开通服务
1. 开通ARMS
开通ARMS地址 https://arms.console.aliyun.com/ (一般有15天试用)
2. 获得数据上报接入点url
进入 https://tracing.console.aliyun.com/#/globalSetting/cn-hangzhou/process 按照图示获得接入点url地址。
如果你的服务器在阿里云上可以用阿里云vpc网络接入点,本示例用的是阿里云公网接入点
安装
通过composer安装:
composer require openzipkin/zipkin
使用
1. 编写中间件
链路监控中间件 app\middleware\ArmsMiddleware.php
<?php
/**
* @desc 全链路监控中间件
* @author Tinywan(ShaoBo Wan)
* @date 2021/12/6 14:06
*/
declare(strict_types=1);
namespace app\middleware;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use support\Log;
use think\facade\Db;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use Zipkin\Reporters\Http;
use Zipkin\TracingBuilder;
use Zipkin\Samplers\BinarySampler;
use Zipkin\Endpoint;
use Workerman\Timer;
use const Zipkin\Tags\SQL_QUERY;
class ArmsMiddleware implements MiddlewareInterface
{
/**
* @desc: 方法描述
* @param Request $request
* @param callable $next
* @return Response
* @author Tinywan(ShaoBo Wan)
*/
public function process(Request $request, callable $next) : Response
{
static $tracing = null, $tracer = null;
if (!$tracing) {
$endpoint = Endpoint::create('开源技术小栈', $request->getRealIp(), null, 2555);
$logger = new Logger('log');
$logger->pushHandler(new ErrorLogHandler());
$reporter = new Http(['endpoint_url' => config('security')['endpoint_url']]);
$sampler = BinarySampler::createAsAlwaysSample();
$tracing = TracingBuilder::create()
->havingLocalEndpoint($endpoint)
->havingSampler($sampler)
->havingReporter($reporter)
->build();
$tracer = $tracing->getTracer();
// 55秒上报一次,尽量将上报对业务的影响减少到最低
Timer::add(55, function () use ($tracer) {
$tracer->flush();
});
register_shutdown_function(function () use ($tracer) {
$tracer->flush();
});
}
$rootSpan = $tracer->newTrace();
$rootSpan->setName($request->controller."::".$request->action);
$rootSpan->start();
$request->rootSpan = $rootSpan;
$request->tracer = $tracer;
$result = $next($request);
// 统计sql(日志在内存)
if (class_exists(Db::class)) {
$logs = Db::getDbLog(true);
if (!empty($logs['sql'])) {
foreach ($logs['sql'] as $sql) {
$sqlSpan = $tracer->newChild($rootSpan->getContext());
$sqlSpan->setName(SQL_QUERY);
$sqlSpan->start();
$sqlSpan->tag('db.statement', $sql);
$sqlSpan->finish();
}
}
}
$rootSpan->finish();
return $result;
}
}
2. 配置中间件
在 config/middleware.php
中添加全局中间件如下
return [
'' => [
\app\middleware\ArmsMiddleware::class,
],
...
];
3. 查看监控
访问地址 https://tracing.console.aliyun.com/ ,效果类似如下