-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimized the tracing in coroutine (#6027)
Co-authored-by: 李铭昕 <[email protected]>
- Loading branch information
1 parent
fc29a30
commit 57035ca
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
use Hyperf\Engine\Coroutine as Co; | ||
use Hyperf\Tracer\TracerContext; | ||
use OpenTracing\Span; | ||
use OpenTracing\Tracer; | ||
use Throwable; | ||
|
||
use function Hyperf\Support\call; | ||
use function Hyperf\Support\make; | ||
|
||
class CoroutineAspect extends AbstractAspect | ||
{ | ||
public array $classes = [ | ||
'Hyperf\Coroutine\Coroutine::create', | ||
]; | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
$callable = $proceedingJoinPoint->arguments['keys']['callable']; | ||
$root = TracerContext::getRoot(); | ||
|
||
$proceedingJoinPoint->arguments['keys']['callable'] = function () use ($callable, $root) { | ||
try { | ||
if ($root instanceof Span) { | ||
/** @var Tracer $tracer */ | ||
$tracer = make(Tracer::class); | ||
TracerContext::setTracer($tracer); | ||
$child = $tracer->startSpan('coroutine', [ | ||
'child_of' => $root->getContext(), | ||
]); | ||
$child->setTag('coroutine.id', Co::id()); | ||
TracerContext::setRoot($child); | ||
Co::defer(function () use ($child, $tracer) { | ||
$child->finish(); | ||
$tracer->flush(); | ||
}); | ||
} | ||
|
||
$callable(); | ||
} catch (Throwable $e) { | ||
if (isset($child)) { | ||
$child->setTag('error', true); | ||
$child->log(['message', $e->getMessage(), 'code' => $e->getCode(), 'stacktrace' => $e->getTraceAsString()]); | ||
} | ||
|
||
throw $e; | ||
} | ||
}; | ||
|
||
return $proceedingJoinPoint->process(); | ||
} | ||
} |