美文网首页
php zipkin客户端和阿里云链路追踪的使用

php zipkin客户端和阿里云链路追踪的使用

作者: lodestar | 来源:发表于2019-07-29 21:20 被阅读0次

    选用阿里云的链路追踪优势在于不用自己搭建和维护这套系统,且支持jaeger、zipkin、skywalking客户端。jaeger和zipkin目前都需要在业务中嵌入代码,skywalking无代码侵入。目前php版本的skywalking客户端不支持authentication。本着先有后优的原则,先用zipkin作为客户端。

    //Trace.class.php
    public static function getTracing($endpointName, $ipv4)
     {
            //$endpointName可以设置为一个应用的名字
            $endpoint = \Zipkin\Endpoint::create($endpointName, $ipv4, null, 2555);
            //阿里云中的接入点
            $url = Config::TRACE_URL;
    
            //修改endpoint_url,否则默认为http://localhost:9411/api/v2/spans
            $options = ["endpoint_url"=>$url,"timeout"=>1];
            $reporter = new \Zipkin\Reporters\Http(\Zipkin\Reporters\Http\CurlFactory::create(), $options);
            $sampler = \Zipkin\Samplers\BinarySampler::createAsAlwaysSample();
            $tracing = \Zipkin\TracingBuilder::create()
                ->havingLocalEndpoint($endpoint)
                ->havingSampler($sampler)
                ->havingReporter($reporter)
                ->build();
            return $tracing;
        }
       
    //服务端调用:
    public function startServerTrace($serviceName, $functionName)
    {
            $localIp = Common::getLocalIp();
            $tracing = self::getTracing($serviceName, $localIp);
            $headers = Common::getHeaders();
            $extractor = $tracing->getPropagation()->getExtractor(new \Zipkin\Propagation\Map());
            $extractedContext = $extractor($headers);
            $this->tracer = $tracing->getTracer();
            $this->span = $this->tracer->nextSpan($extractedContext);
            $this->span->start();
            $this->span->setName("Server:" . $functionName);
            $this->span->setKind(\Zipkin\Kind\SERVER);
            return [$this->tracer, $this->span];
    }
    public function stopServerTrace()
    {
            $this->span->finish();
            $this->tracer->flush();
    }
    
    //客户端加入链路追踪
    public function getUser()
    {
            $localIp = Common::getLocalIp();
            $tracing = Trace::getTracing("codebase", $localIp);
            $tracer = $tracing->getTracer();
            $span = $tracer->newTrace();
            $span->start();
            //可以设置成调用的函数名
            $span->setName("someMethod");
            $span->setKind(\Zipkin\Kind\CLIENT);
            $span->tag(\Zipkin\Tags\HTTP_PATH, $url);
            $span->tag(\Zipkin\Tags\HTTP_STATUS_CODE, $httpCode);
           
            // 加入rpc请求头中
            $tmpHeaders = [];
            $injector = $tracing->getPropagation()->getInjector(new \Zipkin\Propagation\Map());
            $injector($span->getContext(), $tmpHeaders);
            $header = $tmpHeaders;
            foreach ($tmpHeaders as $key => $value) {
                    $headers[] = $key . ":" . $value;
            }
            //$client = new \Yar_Client($url);
            //...
    
           $span->finish();
           $tracer->flush();
    }
    

    服务端获取客户端header

    public function someMethod($params){
            $trace = new Trace();
            $trace->startServerTrace("sign", __FUNCTION__);
            //加入链路中
            //...
            $trace->stopServerTrace();
    }
    

    阿里云应用列表:


    1564405353221.jpg

    某个应用接口列表:


    WX20190729-211109@2x.png

    参考:
    https://help.aliyun.com/document_detail/96187.html
    https://github.com/openzipkin/zipkin-php

    相关文章

      网友评论

          本文标题:php zipkin客户端和阿里云链路追踪的使用

          本文链接:https://www.haomeiwen.com/subject/hmwirctx.html