调用链 php,PHP 使用 Zipkin 做调用链性能分析

先安装zipkin, 然后直接运行

docker pull openzipkin/zipkin

docker run -d -p 9411:9411 openzipkin/zipkin

$ composer require whitemerry/phpkin

运行下面的例子即可

namespace whitemerry\phpkin;

include './vendor/autoload.php';

$endpoint = new Endpoint(

'Passport', // Application name

'127.0.0.1', // Current application IP address

'80' // Current application port (default 80)

);

$logger = new Logger\SimpleHttpLogger([

'host' => 'http://127.0.0.1:9411' // Zipkin's API host with schema (http://) and without trailing slash

]);

$tracer = new Tracer(

'http://rpc/login', // Trace name

$endpoint, // Your application meta-information

$logger // Logger used to store/send traces

);

$tracer->setProfile(Tracer::FRONTEND);

$tracer->trace();

$requestStartTimestamp = zipkin_timestamp();

$spanIdentifier = new Identifier\SpanIdentifier();

/*

...

Request logic

Remember, you need to add B3 headers to your request:

X-B3-TraceId = TracerInfo::getTraceId();

X-B3-SpanId = $spanIdentifier;

X-B3-Sampled = TracerInfo::isSampled();

*/

sleep(1);

$endpoint = new Endpoint(

'rpc 1', // Name of service you're connecting with

'127.0.1.1', // This service Ip

'8000' // And port

);

$annotationBlock = new AnnotationBlock(

$endpoint,

$requestStartTimestamp

);

// 参数放到这里面

$metadata = new Metadata();

$metadata->set("file" , __FILE__);

$span = new Span(

$spanIdentifier,

'rpc 1',

$annotationBlock,

$metadata

);

$tracer->addSpan($span);

$tracer->trace();

$requestStartTimestamp = zipkin_timestamp();

$spanIdentifier = new Identifier\SpanIdentifier();

sleep(2);

/*

...

Request logic

Remember, you need to add B3 headers to your request:

X-B3-TraceId = TracerInfo::getTraceId();

X-B3-SpanId = $spanIdentifier;

X-B3-Sampled = TracerInfo::isSampled();

*/

$endpoint = new Endpoint(

'rpc 2', // Name of service you're connecting with

'127.0.1.1', // This service Ip

'8000' // And port

);

$annotationBlock = new AnnotationBlock(

$endpoint,

$requestStartTimestamp

);

$span = new Span(

$spanIdentifier,

'Rcp 2',

$annotationBlock

);

$tracer->addSpan($span);

$tracer->trace();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值