Skip to content

Commit

Permalink
Merge branch 'master' into 3.1-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Oct 10, 2023
2 parents 48f3b79 + a4a8192 commit 84a3202
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 85 deletions.
11 changes: 8 additions & 3 deletions publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
return [
'default' => env('TRACER_DRIVER', 'zipkin'),
'enable' => [
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
'redis' => env('TRACER_ENABLE_REDIS', false),
'db' => env('TRACER_ENABLE_DB', false),
'method' => env('TRACER_ENABLE_METHOD', false),
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
'method' => env('TRACER_ENABLE_METHOD', false),
'redis' => env('TRACER_ENABLE_REDIS', false),
'rpc' => env('TRACER_ENABLE_RPC', false),
'ignore_exceptions' => [],
],
'tracer' => [
Expand Down Expand Up @@ -118,5 +119,9 @@
'response' => [
'status_code' => 'response.status_code',
],
'rpc' => [
'path' => 'rpc.path',
'status' => 'rpc.status',
],
],
];
86 changes: 4 additions & 82 deletions src/Aspect/JsonRpcAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,9 @@
*/
namespace Hyperf\Tracer\Aspect;

use Hyperf\Context\Context as CT;
use Hyperf\Di\Aop\AbstractAspect;
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\Tracer\TracerContext;
use OpenTracing\Span;
use Psr\Container\ContainerInterface;
use Throwable;

use const OpenTracing\Formats\TEXT_MAP;

class JsonRpcAspect extends AbstractAspect
/**
* @deprecated since v3.0, will be removed in v3.1.
*/
class JsonRpcAspect extends RpcAspect
{
use SpanStarter;

public array $classes = [
AbstractServiceClient::class . '::__generateRpcPath',
Client::class . '::send',
];

private SwitchManager $switchManager;

private SpanTagManager $spanTagManager;

private Context $context;

public function __construct(private ContainerInterface $container)
{
$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
TracerContext::getTracer()->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') {
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
CT::set('tracer.span.' . static::class, $span);
}
throw $e;
} finally {
/** @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();
}
}
101 changes: 101 additions & 0 deletions src/Aspect/RpcAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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\Context\Context as CT;
use Hyperf\Di\Aop\AbstractAspect;
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\Tracer\TracerContext;
use OpenTracing\Span;
use Psr\Container\ContainerInterface;
use Throwable;

use const OpenTracing\Formats\TEXT_MAP;

class RpcAspect extends AbstractAspect
{
use SpanStarter;

public array $classes = [
AbstractServiceClient::class . '::__generateRpcPath',
Client::class . '::send',
];

private SwitchManager $switchManager;

private SpanTagManager $spanTagManager;

private Context $context;

public function __construct(private ContainerInterface $container)
{
$this->switchManager = $container->get(SwitchManager::class);
$this->spanTagManager = $container->get(SpanTagManager::class);
$this->context = $container->get(Context::class);
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (static::class == self::class && $this->switchManager->isEnable('rpc') === false) {
return $proceedingJoinPoint->process();
}

if ($proceedingJoinPoint->methodName === '__generateRpcPath') {
$path = $proceedingJoinPoint->process();
$key = "RPC 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
TracerContext::getTracer()->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') {
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
CT::set('tracer.span.' . static::class, $span);
}
throw $e;
} finally {
/** @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();
}
}
2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Hyperf\Tracer\Aspect\CreateTraceContextAspect;
use Hyperf\Tracer\Aspect\HttpClientAspect;
use Hyperf\Tracer\Aspect\RedisAspect;
use Hyperf\Tracer\Aspect\RpcAspect;
use Hyperf\Tracer\Aspect\TraceAnnotationAspect;
use Hyperf\Tracer\Listener\DbQueryExecutedListener;
use Jaeger\ThriftUdpTransport;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function __invoke(): array
CreateTraceContextAspect::class,
HttpClientAspect::class,
RedisAspect::class,
RpcAspect::class,
TraceAnnotationAspect::class,
],
'publish' => [
Expand Down

0 comments on commit 84a3202

Please sign in to comment.