-
-
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.
# Conflicts: # src/coroutine/src/Traits/Container.php
- Loading branch information
Showing
4 changed files
with
122 additions
and
8 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,65 @@ | ||
<?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\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(); | ||
} | ||
} |
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
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
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,43 @@ | ||
<?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; | ||
|
||
use Hyperf\Context\Context; | ||
use OpenTracing\Span; | ||
use OpenTracing\Tracer; | ||
|
||
class TracerContext | ||
{ | ||
public const TRACER = 'tracer.tracer'; | ||
|
||
public const ROOT = 'tracer.root'; | ||
|
||
public static function setTracer(Tracer $tracer): Tracer | ||
{ | ||
return Context::set(self::TRACER, $tracer); | ||
} | ||
|
||
public static function getTracer(): ?Tracer | ||
{ | ||
return Context::get(self::TRACER) ?: null; | ||
} | ||
|
||
public static function setRoot(Span $root): Span | ||
{ | ||
return Context::set(self::ROOT, $root); | ||
} | ||
|
||
public static function getRoot(): ?Span | ||
{ | ||
return Context::get(self::ROOT) ?: null; | ||
} | ||
} |