0

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

neo 发布于1年前 阅读数 84

先安装zipkin, 然后直接运行

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

$ composer require whitemerry/phpkin

运行下面的例子即可

<?php
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();
举报
回复