定制 quinluong/tracing-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

quinluong/tracing-php

Composer 安装命令:

composer require quinluong/tracing-php

包简介

Distributed tracing wrapper for php

README 文档

README

Required reading

In order to understand the library, one must first be familiar with the OpenTracing project and specification more specifically.

Installation

This library can be installed via Composer:

composer require quinluong/tracing-php

Usage

Creating new tracer using TracerFactory

use Tracing\TracerFactory;

// This instance will be saved in TracerFactory internally by tracer_name, we can use it later by TracerFactory::getByName
$tracer = TracerFactory::create('tracer_name', [
    'enable' => true,
    'host_port' => 'agent_host:port',
    'sampler_type' => 'const', // const, probabilistic
    'sampler_value' => 1 // const: 0 / 1, probabilistic: 0 -> 1
]);

Extracting span context from request header

use OpenTracing\Formats;

$spanOptions = [
    'tags' => [
        'tag_key_1' => 'tag_value_1',
        'tag_key_2' => 'tag_value_2'
    ]
];

// Extract and use it for next span
$spanContext = $tracer->extract(Formats\TEXT_MAP, getallheaders());

if ($spanContext !== null) {
    $spanOptions['child_of'] = $spanContext;
}

$tracer->startActiveSpan('operation_name', $spanOptions);

Injecting span context into request header

use OpenTracing\Formats;

$tracer->inject($span->getContext(), Formats\TEXT_MAP, $arrHeader);

Creating span

For most use cases, it is recommended that you use the TracerInterface::startActiveSpan function for creating new spans.

An example of a linear, two level deep span tree using active spans looks like this in PHP code:

// At dispatcher level
$scope = $tracer->startActiveSpan('request');
...
$scope->close();
// At controller level
$scope = $tracer->startActiveSpan('controller');
...
$scope->close();
// At RPC calls level
$scope = $tracer->startActiveSpan('http');
file_get_contents('http://php.net');
$scope->close();

When using the TracerInterface::startActiveSpan function the underlying tracer uses an abstraction called scope manager to keep track of the currently active span.

Starting an active span will always use the currently active span as a parent. If no parent is available, then the newly created span is considered to be the root span of the trace.

Unless you are using asynchronous code that tracks multiple spans at the same time, such as when using cURL Multi Exec or MySQLi Polling it is recommended that you use TracerInterface::startActiveSpan everywhere in your application.

The currently active span gets automatically finished when you call $scope->close() as you can see in the previous examples.

Creating a child span using automatic active span management

$parent = $tracer->startActiveSpan('parent');
...
/*
 * Since the parent span has been created by using startActiveSpan we don't need
 * to pass a reference for this child span
 */
$child = $tracer->startActiveSpan('my_second_span');
...
$child->close();
...
$parent->close();

Creating a child span assigning parent manually

$parent = $tracer->startSpan('parent');
...
$child = $tracer->startSpan('child', [
    'child_of' => $parent
]);
...
$child->finish();
...
$parent->finish();

Tags and logs

// Tags are searchable in UI
$span->setTag('http.status', '200');
$span->setTag('http.url', 'abc.com/api/endpoint');

$tracer->startActiveSpan('my_span', [
    'tags' => [
        'foo-1' => 'bar-1',
        'foo-2' => 'bar-2'
        ...
    ]
]);
// Log information
$span->log([
    'error' => 'HTTP request timeout'
    'event' => 'soft error',
    'type' => 'cache timeout',
    'waiter.millis' => 1500
    ...
]);

Flushing spans

PHP as a request scoped language has no simple means to pass the collected spans data to a background process without blocking the main request thread/process. The OpenTracing API makes no assumptions about this, but for PHP that might cause problems for Tracer implementations. This is why the PHP API contains a flush method that allows to trigger a span sending out of process.

register_shutdown_function(function() {
    $tracer = TracerFactory::getByName('tracer_name');
    // Flush the tracer to the backend
    if ($tracer !== null) {
        $tracer->flush();
    }
});

Pause and resume

$tracer->pause();
...
// This function won't be instrumented
doSomething();
...
$tracer->resume();

Semantic conventions

quinluong/tracing-php 适用场景与选型建议

quinluong/tracing-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 261 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 quinluong/tracing-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 261
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-12-11