Skip to content

Commit

Permalink
Merge branch 'master' into 3.1-merge
Browse files Browse the repository at this point in the history
# Conflicts:
#	docs/zh-cn/changelog.md
#	docs/zh-hk/changelog.md
#	docs/zh-tw/changelog.md
#	src/redis/composer.json
  • Loading branch information
limingxinleo committed Oct 13, 2023
2 parents 84a3202 + ae9abcc commit e8bc063
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 14 deletions.
3 changes: 3 additions & 0 deletions publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
return [
'default' => env('TRACER_DRIVER', 'zipkin'),
'enable' => [
'coroutine' => env('TRACER_ENABLE_COROUTINE', false),
'db' => env('TRACER_ENABLE_DB', false),
'elasticserach' => env('TRACER_ENABLE_ELASTICSERACH', false),
'exception' => env('TRACER_ENABLE_EXCEPTION', false),
'grpc' => env('TRACER_ENABLE_GRPC', false),
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
'method' => env('TRACER_ENABLE_METHOD', false),
'redis' => env('TRACER_ENABLE_REDIS', false),
Expand Down
13 changes: 10 additions & 3 deletions src/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Engine\Coroutine as Co;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager;
use Hyperf\Tracer\TracerContext;
use OpenTracing\Span;
Expand All @@ -25,12 +26,16 @@ class CoroutineAspect extends AbstractAspect
'Hyperf\Coroutine\Coroutine::create',
];

public function __construct(private SwitchManager $switchManager)
public function __construct(protected SwitchManager $switchManager, protected SpanTagManager $spanTagManager)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (! $this->switchManager->isEnable('coroutine')) {
return $proceedingJoinPoint->process();
}

$callable = $proceedingJoinPoint->arguments['keys']['callable'];
$root = TracerContext::getRoot();

Expand All @@ -41,7 +46,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$child = $tracer->startSpan('coroutine', [
'child_of' => $root->getContext(),
]);
$child->setTag('coroutine.id', Co::id());
if ($this->spanTagManager->has('coroutine', 'id')) {
$child->setTag($this->spanTagManager->get('coroutine', 'id'), Co::id());
}
TracerContext::setRoot($child);
Co::defer(function () use ($child, $tracer) {
$child->finish();
Expand All @@ -51,7 +58,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)

$callable();
} catch (Throwable $e) {
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if (isset($child) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$child->setTag('error', true);
$child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/DbAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Aspect/ElasticserachAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ public function __construct(private SwitchManager $switchManager, private SpanTa
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if ($this->switchManager->isEnable('elasticserach') === false) {
return $proceedingJoinPoint->process();
}

$key = $proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName;
$span = $this->startSpan($key);
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
130 changes: 130 additions & 0 deletions src/Aspect/GrpcAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @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\GrpcClient\GrpcClient;
use Hyperf\GrpcClient\Request;
use Hyperf\Rpc\Context;
use Hyperf\Tracer\SpanStarter;
use Hyperf\Tracer\SpanTagManager;
use Hyperf\Tracer\SwitchManager;
use Hyperf\Tracer\TracerContext;
use OpenTracing\Span;
use Psr\Container\ContainerInterface;
use Swoole\Http2\Response;
use Throwable;

use const OpenTracing\Formats\TEXT_MAP;

class GrpcAspect extends AbstractAspect
{
use SpanStarter;

public array $classes = [
GrpcClient::class . '::send',
GrpcClient::class . '::recv',
];

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 (! $this->switchManager->isEnable('grpc')) {
return $proceedingJoinPoint->process();
}

return match ($proceedingJoinPoint->methodName) {
'send' => $this->processSend($proceedingJoinPoint),
'recv' => $this->processRecv($proceedingJoinPoint),
default => $proceedingJoinPoint->process(),
};
}

private function processSend(ProceedingJoinPoint $proceedingJoinPoint)
{
$arguments = $proceedingJoinPoint->getArguments();
/** @var Request $request */
$request = $arguments[0];
$key = "GRPC send [{$request->path}]";
$span = $this->startSpan($key);
$carrier = [];
// Injects the context into the wire
TracerContext::getTracer()->inject(
$span->getContext(),
TEXT_MAP,
$carrier
);

// Merge tracer info
$request->headers = array_merge($request->headers, $carrier);
if ($this->spanTagManager->has('grpc', 'request.header')) {
foreach ($request->headers as $key => $value) {
$span->setTag($this->spanTagManager->get('grpc', 'request.header') . '.' . $key, $value);
}
}

$this->context->set('tracer.carrier', $carrier);
CT::set('tracer.span.' . static::class, $span);

try {
return $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
throw $e;
}
}

private function processRecv(ProceedingJoinPoint $proceedingJoinPoint)
{
/** @var null|Span $span */
$span = CT::get('tracer.span.' . static::class);

try {
/** @var bool|Response $result */
$result = $proceedingJoinPoint->process();
if ($result instanceof Response) {
if ($this->spanTagManager->has('grpc', 'response.header')) {
foreach ($result->headers as $key => $value) {
$span?->setTag($this->spanTagManager->get('grpc', 'response.header') . '.' . $key, $value);
}
}
}
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
$span?->setTag('error', true);
$span?->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
throw $e;
} finally {
$span?->finish();
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Aspect/HttpClientAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
}
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/MethodAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/RedisAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$result = $proceedingJoinPoint->process();
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/RpcAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if (($span = CT::get('tracer.span.' . static::class)) && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
CT::set('tracer.span.' . static::class, $span);
Expand Down
2 changes: 1 addition & 1 deletion src/Aspect/TraceAnnotationAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
try {
$result = $proceedingJoinPoint->process();
} catch (Throwable $e) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($e)) {
$span->setTag('error', true);
$span->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]);
}
Expand Down
6 changes: 6 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Hyperf\Tracer;

use GuzzleHttp\Client;
use Hyperf\Tracer\Aspect\CoroutineAspect;
use Hyperf\Tracer\Aspect\CreateTraceContextAspect;
use Hyperf\Tracer\Aspect\ElasticserachAspect;
use Hyperf\Tracer\Aspect\GrpcAspect;
use Hyperf\Tracer\Aspect\HttpClientAspect;
use Hyperf\Tracer\Aspect\RedisAspect;
use Hyperf\Tracer\Aspect\RpcAspect;
Expand Down Expand Up @@ -47,7 +50,10 @@ public function __invoke(): array
],
],
'aspects' => [
CoroutineAspect::class,
CreateTraceContextAspect::class,
ElasticserachAspect::class,
GrpcAspect::class,
HttpClientAspect::class,
RedisAspect::class,
RpcAspect::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/RequestTraceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function handleRequestTerminated(RequestTerminated $event): void
$span = TracerContext::getRoot();
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());

if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception::class)) {
if ($event->exception && $this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($event->exception)) {
$this->appendExceptionToSpan($span, $exception = $event->exception);

if ($exception instanceof HttpException) {
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
} catch (Throwable $exception) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception::class)) {
if ($this->switchManager->isEnable('exception') && ! $this->switchManager->isIgnoreException($exception)) {
$this->appendExceptionToSpan($span, $exception);
}
if ($exception instanceof HttpException) {
Expand Down
4 changes: 4 additions & 0 deletions src/SpanTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class SpanTagManager
'http.method' => 'http.method',
'http.status_code' => 'http.status_code',
],
'grpc' => [
'request.header' => 'grpc.request.header',
'response.header' => 'grpc.response.header',
],
'redis' => [
'arguments' => 'redis.arguments',
'result' => 'redis.result',
Expand Down
5 changes: 3 additions & 2 deletions src/SwitchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Hyperf\Context\Context;
use OpenTracing\Span;
use Throwable;

class SwitchManager
{
Expand Down Expand Up @@ -50,11 +51,11 @@ public function isEnable(string $identifier): bool
return $this->config[$identifier] && Context::get('tracer.root') instanceof Span;
}

public function isIgnoreException(string $className): bool
public function isIgnoreException(Throwable|string $exception): bool
{
$ignoreExceptions = $this->config['ignore_exceptions'] ?? [];
foreach ($ignoreExceptions as $ignoreException) {
if (is_a($className, $ignoreException, true)) {
if (is_a($exception, $ignoreException, true)) {
return true;
}
}
Expand Down

0 comments on commit e8bc063

Please sign in to comment.