日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

链路追踪php,easyswoole链路追踪

發布時間:2025/3/8 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链路追踪php,easyswoole链路追踪 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Tracker

Easyswoole提供了一個基礎的追蹤組件,方便用戶實現基礎的服務器狀態監控,與調用鏈記錄。

組件要求

php: >=7.1.0

ext-swoole: ^4.4.0

easyswoole/component: ^2.0

安裝方法

composer require easyswoole/tracker

倉庫地址

調用鏈

Easyswoole的調用鏈跟蹤是一個以類似有序的樹狀鏈表的解構實現的,解構如下:

struct Point{

struct Point* nextPoint;

struct Point[] subPoints;

const END_SUCCESS = 'success';

const END_FAIL = 'fail';

const END_UNKNOWN = 'unknown';

int startTime;

mixed startArg;

int endTime;

string pointName;

string endStatus = self::END_UNKNOWN;

mixed endArg;

string pointId;

string parentId;

int depth = 0;

bool isNext

}

基本使用

use EasySwoole\Tracker\Point;

use EasySwoole\Component\WaitGroup;

use EasySwoole\Tracker\PointContext;

/*

* 假設我們的調用鏈是這樣的

* onRequest ->> actionOne ->> actionOne call remote Api(1,2) ->> afterAction

*/

go(function (){

/*

* 創建入口

*/

$onRequest = new Point('onRequest');

//記錄請求參數,并模擬access log

\co::sleep(0.01);

$onRequest->setStartArg([

'requestArg' => 'requestArgxxxxxxxx',

'accessLogId'=>'logIdxxxxxxxxxx'

]);

//onRequest完成

$onRequest->end();

//進入 next actionOne

$actionOne = $onRequest->next('actionOne');

//action one 進入子環節調用

$waitGroup = new WaitGroup();

//sub pointOne

$waitGroup->add();

$subOne = $actionOne->appendChild('subOne');

go(function ()use($subOne,$waitGroup){

\co::sleep(0.1);

$subOne->end();

$waitGroup->done();

});

//sub pointTwo,并假設失敗

$waitGroup->add();

$subTwo = $actionOne->appendChild('subTwo');

go(function ()use($subTwo,$waitGroup){

\co::sleep(1);

$subTwo->end($subTwo::END_FAIL,['failMsg'=>'timeout']);

$waitGroup->done();

});

$waitGroup->wait();

$actionOne->end();

//actionOne結束,進入afterAction

$afterAction = $actionOne->next('afterAction');

//模擬響應記錄

\co::sleep(0.01);

$afterAction->end($afterAction::END_SUCCESS,['log'=>'success']);

/*

* 從入口開始打印調用鏈

*/

echo Point::toString($onRequest);

});

//以上代碼等價于如下

go(function (){

PointContext::getInstance()->createStart('onRequest')->next('actionOne')->next('afterAction');

//記錄請求參數,并模擬access log

\co::sleep(0.01);

PointContext::getInstance()->find('onRequest')->setStartArg([

'requestArg' => 'requestArgxxxxxxxx',

'accessLogId'=>'logIdxxxxxxxxxx'

])->end();

$subOne = PointContext::getInstance()->find('actionOne')->appendChild('subOne');

$subTwo = PointContext::getInstance()->find('actionOne')->appendChild('subTwo');

$waitGroup = new WaitGroup();

$waitGroup->add();

go(function ()use($subOne,$waitGroup){

\co::sleep(0.1);

$subOne->end();

$waitGroup->done();

});

//sub pointTwo,并假設失敗

$waitGroup->add();

go(function ()use($subTwo,$waitGroup){

\co::sleep(1);

$subTwo->end($subTwo::END_FAIL,['failMsg'=>'timeout']);

$waitGroup->done();

});

$waitGroup->wait();

PointContext::getInstance()->find('actionOne')->end();

//模擬響應記錄

\co::sleep(0.01);

PointContext::getInstance()->find('afterAction')->end(Point::END_SUCCESS,['log'=>'success']);

/*

* 從入口開始打印調用鏈

*/

echo Point::toString(PointContext::getInstance()->startPoint());

});

以上代碼輸出結果:

#

PointName:onRequest

Status:success

PointId:AoRVFMgrsbNwukBZc7

Depth:0

IsNext:false

Start:1561736477.2808

StartArg:{"requestArg":"requestArgxxxxxxxx","accessLogId":"logIdxxxxxxxxxx"}

End:1561736477.2939

EndArg:null

ChildCount:0

Children:None

NextPoint:

#

PointName:actionOne

Status:success

PointId:2zOWG1SvMbyBcnRmje

Depth:0

IsNext:true

Start:1561736477.2809

StartArg:null

End:1561736478.2993

EndArg:null

ChildCount:2

Children:

#

PointName:subOne

Status:success

PointId:0wU31l8brpfCnXdTxH

Depth:1

IsNext:false

Start:1561736477.2939

StartArg:null

End:1561736477.4006

EndArg:null

ChildCount:0

Children:None

NextPoint:None

#

PointName:subTwo

Status:fail

PointId:Jphr6RD8KSHmYbt70A

Depth:1

IsNext:false

Start:1561736477.2939

StartArg:null

End:1561736478.2993

EndArg:{"failMsg":"timeout"}

ChildCount:0

Children:None

NextPoint:None

NextPoint:

#

PointName:afterAction

Status:success

PointId:oPnGNrkj6qwb381BQl

Depth:0

IsNext:true

Start:1561736477.2809

StartArg:null

End:1561736478.3119

EndArg:{"log":"success"}

ChildCount:0

Children:None

NextPoint:None

如果想以自己的格式記錄到數據庫,可以具體查看Point實現的方法,每個Point都有自己的Id

進階使用

HTTP API請求追蹤

EasySwooleEvent.php

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\Swoole\EventRegister;

use EasySwoole\EasySwoole\AbstractInterface\Event;

use EasySwoole\Http\Request;

use EasySwoole\Http\Response;

use EasySwoole\Tracker\Point;

use EasySwoole\Tracker\PointContext;

class EasySwooleEvent implements Event

{

public static function initialize()

{

// TODO: Implement initialize() method.

date_default_timezone_set('Asia/Shanghai');

}

public static function mainServerCreate(EventRegister $register)

{

}

public static function onRequest(Request $request, Response $response): bool

{

$point = PointContext::getInstance()->createStart('onRequest');

$point->setStartArg([

'uri'=>$request->getUri()->__toString(),

'get'=>$request->getQueryParams()

]);

return true;

}

public static function afterRequest(Request $request, Response $response): void

{

$point = PointContext::getInstance()->startPoint();

$point->end();

echo Point::toString($point);

$array = Point::toArray($point);

}

}

Index.php

namespace App\HttpController;

use EasySwoole\Component\WaitGroup;

use EasySwoole\Http\AbstractInterface\Controller;

use EasySwoole\Tracker\PointContext;

class Index extends Controller

{

protected function onRequest(?string $action): ?bool

{

/*

* 調用關系 HttpRequest->OnRequest

*/

$point = PointContext::getInstance()->next('ControllerOnRequest');

//假設這里進行了權限驗證,并模擬數據庫耗時

\co::sleep(0.01);

$point->setEndArg([

'userId'=>'xxxxxxxxxxx'

]);

$point->end();

return true;

}

function index()

{

//模擬調用第三方Api,調用關系 OnRequest->sub(subApi1,subApi2)

$actionPoint = PointContext::getInstance()->next('indexAction');

$wait = new WaitGroup();

$subApi = $actionPoint->appendChild('subOne');

$wait->add();

go(function ()use($wait,$subApi){

\co::sleep(1);

$subApi->end();

$wait->done();

});

$subApi = $actionPoint->appendChild('subTwo');

$wait->add();

go(function ()use($wait,$subApi){

\co::sleep(0.3);

$subApi->end($subApi::END_FAIL);

$wait->done();

});

$wait->wait();

$actionPoint->end();

$this->response()->write('hello world');

}

}

以上每次請求會輸出如下格式:

#

PointName:onRequest

Status:success

PointId:1561743038GyV4lnus

ParentId:

Depth:0

IsNext:false

Start:1561743038.7011

StartArg:{"uri":"http://127.0.0.1:9501/","get":[]}

End:1561743039.7152

EndArg:null

ChildCount:0

Children:None

NextPoint:

#

PointName:ControllerOnRequest

Status:success

PointId:15617430386f0OQDsS

ParentId:1561743038GyV4lnus

Depth:0

IsNext:true

Start:1561743038.7025

StartArg:null

End:1561743038.713

EndArg:null

ChildCount:0

Children:None

NextPoint:

#

PointName:indexAction

Status:success

PointId:1561743038XEmF0M49

ParentId:15617430386f0OQDsS

Depth:0

IsNext:true

Start:1561743038.7131

StartArg:null

End:1561743039.7151

EndArg:null

ChildCount:2

Children:

#

PointName:subOne

Status:success

PointId:1561743038uIkzYgcS

ParentId:1561743038XEmF0M49

Depth:1

IsNext:false

Start:1561743038.7135

StartArg:null

End:1561743039.7151

EndArg:null

ChildCount:0

Children:None

NextPoint:None

#

PointName:subTwo

Status:fail

PointId:1561743038PslVSY4n

ParentId:1561743038XEmF0M49

Depth:1

IsNext:false

Start:1561743038.7136

StartArg:null

End:1561743039.0149

EndArg:null

ChildCount:0

Children:None

NextPoint:None

NextPoint:None

Api調用鏈記錄

$array = Point::toArray($point);

可以把一個入口點轉為一個數組。例如我們可以在MYSQL數據庫中存儲以下關鍵結構:

CREATE TABLE `api_tracker_point_list` (

`pointd` varchar(18) NOT NULL,

`pointName` varchar(45) DEFAULT NULL,

`parentId` varchar(18) DEFAULT NULL,

`depth` int(11) NOT NULL DEFAULT '0',

`isNext` int(11) NOT NULL DEFAULT '0',

`startTime` varchar(14) NOT NULL,

`endTime` varchar(14) DEFAULT NULL,

`status` varchar(10) NOT NULL,

PRIMARY KEY (`pointd`),

UNIQUE KEY `trackerId_UNIQUE` (`pointd`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

其余請求參數可以自己記錄。

核心字段在pointId,parentId與isNext,status 這四個個字段,例如,我想得到哪次調用鏈超時,那么就是直接

where status = fail

如果想看哪次調用耗時多少,那么可以

where spendTime > 3

spendTime 是用startTime和endTime計算

相關倉庫

EasySwoole之鏈路追蹤 簡單demo

總結

以上是生活随笔為你收集整理的链路追踪php,easyswoole链路追踪的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。