Skip to content

Commit

Permalink
Added switch for ElasticserachAspect and CoroutineAspect. (#6200)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Oct 10, 2023
1 parent a4a8192 commit 11029e3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions publish/opentracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
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),
'guzzle' => env('TRACER_ENABLE_GUZZLE', false),
'method' => env('TRACER_ENABLE_METHOD', false),
Expand Down
11 changes: 9 additions & 2 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 Down
4 changes: 4 additions & 0 deletions src/Aspect/ElasticserachAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ 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 {
Expand Down
4 changes: 4 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
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\HttpClientAspect;
use Hyperf\Tracer\Aspect\RedisAspect;
use Hyperf\Tracer\Aspect\RpcAspect;
Expand Down Expand Up @@ -47,7 +49,9 @@ public function __invoke(): array
],
],
'aspects' => [
CoroutineAspect::class,
CreateTraceContextAspect::class,
ElasticserachAspect::class,
HttpClientAspect::class,
RedisAspect::class,
RpcAspect::class,
Expand Down

0 comments on commit 11029e3

Please sign in to comment.