whitemerry/phpkin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

whitemerry/phpkin

Composer 安装命令:

composer require whitemerry/phpkin

包简介

PHP Zipkin implementation

README 文档

README

Software License Latest Stable Version OpenTracing Badge Maintainability Test Coverage

First production ready, simple and full Zipkin implementation without dependencies.

Compatible with both front and back-end applications and respects B3 Propagation.

Installing via Composer

$ composer require whitemerry/phpkin

Documentation

Short implementation information

In this project BinaryAnnotations are Metadata and annotations are replaced by AnnotationBlock witch allow you to create Annotations for Spans faster, and cleaner. All of these methods have more parameters than used here, read PHPDocs and remember, you can change everything by implementing interfaces or extending classes.

Let's get started

First, very important step is defining your service meta-information for tracer:

$endpoint = new Endpoint(
    'My application', // Application name
    '127.0.0.1', // Current application IP address
    '80' // Current application port (default 80)
);

Next, define storage for traces - currently two types are supported - SimpleHttpLogger witch automatically sends trace data to Zipkin's service and FileLogger (you can read more about this below):

$logger = new SimpleHttpLogger([
    'host' => 'http://192.168.33.11:9411' // Zipkin's API host with schema (http://) and without trailing slash
]);

Now you can initialize Tracer!

For front-end applications (Source for TraceId, SpanId and Sampled for other microservices):

$tracer = new Tracer(
    'http://localhost/login', // Trace name
    $endpoint, // Your application meta-information
    $logger // Logger used to store/send traces
);
$tracer->setProfile(Tracer::FRONTEND);

For back-end applications / microservices (Consumer of existing TraceId, SpanId and Sampled)

$traceId = null;
if (!empty($_SERVER['HTTP_X_B3_TRACEID'])) {
    $traceId = new TraceIdentifier($_SERVER['HTTP_X_B3_TRACEID']);
}

$traceSpanId = null;
if (!empty($_SERVER['HTTP_X_B3_SPANID'])) {
    $traceSpanId = new SpanIdentifier($_SERVER['HTTP_X_B3_SPANID']);
}

$isSampled = null;
if (!empty($_SERVER['HTTP_X_B3_SAMPLED'])) {
    $isSampled = (bool) $_SERVER['HTTP_X_B3_SAMPLED'];
}

$tracer = new Tracer(
    'http://localhost/login',
    $endpoint,
    $logger,
    $sampled,
    $traceId,
    $traceSpanId
);
$tracer->setProfile(Tracer::BACKEND);

All these lines must be initialized as soon as possible, in frameworks bootstrap.php is good place.

There are more parameters with descriptions in PHPDocs! For example, if you are front-end application you can use PercentageSampler, tool for toggling tracing logs (You don't need to log everything).

As last step just trigger trace method from $tracer, for example in shutdown event of your framework, or at the end of index.php

$tracer->trace();

Now as you can see, you have new entries in the Zipkin's UI! :)

Adding spans to trace

As you already now, in Zipkin, you can store and visualize communication between 2 services (for example databases, microservices). So, you need to create Span (Zipkin's block of information about request):

// Before request - read current timestamp in zipkin format
$requestStartTimestamp = zipkin_timestamp();
$spanIdentifier = new 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();
*/

$endpoint = new Endpoint(
    'Accounts microservice', // 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,
    'Authorize user',
    $annotationBlock
);

And add to tracer

$tracer->addSpan($span);

Calling tracer statically

You can get access to tracer statically, in every place of your project, just init TracerProxy:

$tracer = new Tracer(...); // Your tracer instance
TracerProxy::init($tracer);

Now you have access to methods like:

TracerProxy::addSpan($span);
TracerProxy::trace();

Where do i have information about this trace?

All meta information are in static class TracerInfo

TracerInfo::getTraceId(); // TraceId - X-B3-TraceId
TracerInfo::getTraceSpanId(); // ParentId - X-B3-ParentId
TracerInfo::isSampled(); // Sampled - X-B3-Sampled

Making requests to other service

Take a look at our examples. You need to set B3 header by your own in yours rest/api/guzzle client.

Differences between loggers

SimpleHttpLogger - Allows you to try zipkin right away, by uploading logs at the end of user request to webiste. However, it will delay the response back to the user.

FileLogger - Allows you to setup asynchronous reporting to zipkin. While this is a synchronous write to disk, in practice latency impact to callers is minimal, but you need to write upload to zipkin tool by your own.

For more info read this ticket!

Are logs automatically uploaded to Zipkin?

For SimpleHttpLogger, short answer, yes

For FileLogger, bit logner answer, you need to upload logs from zipkin.log to Zipkin by your own, for example by cron working in background making POST's to the Zipkin (API)

Unit tests

Code Coverage (Generated by PHPUnit):

  • Lines: 70.35% (140 / 199)
  • Functions and Methods: 52.08% (25 / 48)
  • Classes and Traits: 58.33% (7 / 12)

TODO

  • AsyncHttpLogger (Based on CURL)
  • Upload to zipkin cron for FileLogger

Inspired by Tolerance

whitemerry/phpkin 适用场景与选型建议

whitemerry/phpkin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.51k 次下载、GitHub Stars 达 76, 最近一次更新时间为 2017 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「zipkin」 「tracing」 「openzipkin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 whitemerry/phpkin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 whitemerry/phpkin 我们能提供哪些服务?
定制开发 / 二次开发

基于 whitemerry/phpkin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 77
  • 点击次数: 30
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 76
  • Watchers: 3
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-02