mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Support json-rpc for tracer component. (#2625)
* Update SpanStarter.php * Added JsonRpcAspect. Co-authored-by: jimmy <you@example.com> Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
a044700936
commit
8f33ddbdf9
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -12,7 +12,7 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
php-version: ['7.3', '7.4']
|
||||
php-version: ['7.2', '7.3', '7.4']
|
||||
mysql-version: ['5.7', '8.0']
|
||||
max-parallel: 6
|
||||
steps:
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
## Added
|
||||
|
||||
- [#2625](https://github.com/hyperf/hyperf/pull/2625) Added aspect `Hyperf\Tracer\Aspect\JsonRpcAspect` which support json-rpc for tracer component.
|
||||
- [#2709](https://github.com/hyperf/hyperf/pull/2709) [#2733](https://github.com/hyperf/hyperf/pull/2733) Added `@mixin` into Model, you can use static methods friendly.
|
||||
- [#2726](https://github.com/hyperf/hyperf/pull/2726) [#2733](https://github.com/hyperf/hyperf/pull/2733) Added option `--with-ide` which used to generate ide file.
|
||||
|
||||
|
@ -183,6 +183,22 @@ return [
|
||||
|
||||
关于 Jaeger 的更多配置可以在 [[这里](https://github.com/jonahgeorge/jaeger-client-php)] 查看。
|
||||
|
||||
#### 配置 JsonRPC 追踪开关
|
||||
|
||||
JsonRPC 的链路追踪并不在统一配置当中,暂时还属于 `Beta` 版本的功能。
|
||||
|
||||
我们只需要配置 `aspects.php`,加入以下 `Aspect` 即可开启。
|
||||
|
||||
> 提示:不要忘了在对端,添加对应的 TraceMiddleware
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
return [
|
||||
Hyperf\Tracer\Aspect\JsonRpcAspect::class,
|
||||
];
|
||||
```
|
||||
|
||||
### 配置中间件
|
||||
|
||||
配置完驱动之后,采集信息还需要配置一下中间件才能启用采集功能。
|
||||
@ -199,6 +215,7 @@ return [
|
||||
],
|
||||
];
|
||||
```
|
||||
|
||||
### 配置 Span tag
|
||||
|
||||
`1.1.11` 版本后增加了 Span Tag 配置的功能,对于一些 Hyperf 自动收集追踪信息的 Span Tag 名称,可以通过更改 Span Tag 配置来更改对应的名称,只需在配置文件 `config/autolaod/opentracing.php` 内增加 `tags` 配置即可,参考配置如下。如配置项存在,则以配置项的值为准,如配置项不存在,则以组件的默认值为准。
|
||||
|
106
src/tracer/src/Aspect/JsonRpcAspect.php
Normal file
106
src/tracer/src/Aspect/JsonRpcAspect.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Tracer\Aspect;
|
||||
|
||||
use Hyperf\Di\Aop\AroundInterface;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
use Hyperf\Rpc\Context;
|
||||
use Hyperf\RpcClient\AbstractServiceClient;
|
||||
use Hyperf\RpcClient\Client;
|
||||
use Hyperf\Tracer\SpanStarter;
|
||||
use Hyperf\Tracer\SpanTagManager;
|
||||
use Hyperf\Tracer\SwitchManager;
|
||||
use Hyperf\Utils\Context as CT;
|
||||
use OpenTracing\Tracer;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Zipkin\Span;
|
||||
use const OpenTracing\Formats\TEXT_MAP;
|
||||
|
||||
class JsonRpcAspect implements AroundInterface
|
||||
{
|
||||
use SpanStarter;
|
||||
|
||||
public $classes = [
|
||||
AbstractServiceClient::class . '::__generateRpcPath',
|
||||
Client::class . '::send',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* @var Tracer
|
||||
*/
|
||||
private $tracer;
|
||||
|
||||
/**
|
||||
* @var SwitchManager
|
||||
*/
|
||||
private $switchManager;
|
||||
|
||||
/**
|
||||
* @var SpanTagManager
|
||||
*/
|
||||
private $spanTagManager;
|
||||
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->tracer = $container->get(Tracer::class);
|
||||
$this->switchManager = $container->get(SwitchManager::class);
|
||||
$this->spanTagManager = $container->get(SpanTagManager::class);
|
||||
$this->context = $container->get(Context::class);
|
||||
}
|
||||
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
||||
{
|
||||
if ($proceedingJoinPoint->methodName === '__generateRpcPath') {
|
||||
$path = $proceedingJoinPoint->process();
|
||||
$key = "JsonRPC send [{$path}]";
|
||||
$span = $this->startSpan($key);
|
||||
if ($this->spanTagManager->has('rpc', 'path')) {
|
||||
$span->setTag($this->spanTagManager->get('rpc', 'path'), $path);
|
||||
}
|
||||
$carrier = [];
|
||||
// Injects the context into the wire
|
||||
$this->tracer->inject(
|
||||
$span->getContext(),
|
||||
TEXT_MAP,
|
||||
$carrier
|
||||
);
|
||||
$this->context->set('tracer.carrier', $carrier);
|
||||
CT::set('tracer.span.' . static::class, $span);
|
||||
return $path;
|
||||
}
|
||||
|
||||
if ($proceedingJoinPoint->methodName === 'send') {
|
||||
$result = $proceedingJoinPoint->process();
|
||||
/** @var Span $span */
|
||||
if ($span = CT::get('tracer.span.' . static::class)) {
|
||||
if ($this->spanTagManager->has('rpc', 'status')) {
|
||||
$span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed');
|
||||
}
|
||||
$span->finish();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
return $proceedingJoinPoint->process();
|
||||
}
|
||||
}
|
@ -11,6 +11,8 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace Hyperf\Tracer;
|
||||
|
||||
use Hyperf\Rpc;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Hyperf\Utils\Context;
|
||||
use OpenTracing\Span;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
@ -30,6 +32,7 @@ trait SpanStarter
|
||||
): Span {
|
||||
$root = Context::get('tracer.root');
|
||||
if (! $root instanceof Span) {
|
||||
$container = ApplicationContext::getContainer();
|
||||
/** @var ServerRequestInterface $request */
|
||||
$request = Context::get(ServerRequestInterface::class);
|
||||
if (! $request instanceof ServerRequestInterface) {
|
||||
@ -38,6 +41,12 @@ trait SpanStarter
|
||||
$carrier = array_map(function ($header) {
|
||||
return $header[0];
|
||||
}, $request->getHeaders());
|
||||
if ($container->has(Rpc\Context::class) && $rpcContext = $container->get(Rpc\Context::class)) {
|
||||
$rpcCarrier = $rpcContext->get('tracer.carrier');
|
||||
if (! empty($rpcCarrier)) {
|
||||
$carrier = $rpcCarrier;
|
||||
}
|
||||
}
|
||||
// Extracts the context from the HTTP headers.
|
||||
$spanContext = $this->tracer->extract(TEXT_MAP, $carrier);
|
||||
if ($spanContext) {
|
||||
|
@ -27,6 +27,10 @@ class SpanTagManager
|
||||
'db.statement' => 'db.sql',
|
||||
'db.query_time' => 'db.query_time',
|
||||
],
|
||||
'rpc' => [
|
||||
'path' => 'rpc.path',
|
||||
'status' => 'rpc.status',
|
||||
],
|
||||
];
|
||||
|
||||
public function apply(array $tags): void
|
||||
|
Loading…
Reference in New Issue
Block a user