Skip to content

Commit

Permalink
Merge branch 'master' into 3.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/coroutine/src/Traits/Container.php
  • Loading branch information
limingxinleo committed Aug 18, 2023
2 parents 449dba5 + 8bdd784 commit 4413bb4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
65 changes: 65 additions & 0 deletions src/Aspect/CoroutineAspect.php
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();
}
}
5 changes: 5 additions & 0 deletions src/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
});
try {
$response = $handler->handle($request);
/** @var \ZipkinOpenTracing\SpanContext $spanContent */
$spanContent = $span->getContext();
/** @var \Zipkin\Propagation\TraceContext $traceContext */
$traceContext = $spanContent->getContext();
$response = $response->withHeader('Trace-Id', $traceContext->getTraceId());
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
} catch (Throwable $exception) {
$this->switchManager->isEnable('exception') && $this->appendExceptionToSpan($span, $exception);
Expand Down
17 changes: 9 additions & 8 deletions src/SpanStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ protected function startSpan(
array $option = [],
string $kind = SPAN_KIND_RPC_SERVER
): Span {
$root = Context::get('tracer.root');
$root = TracerContext::getRoot();
$tracer = TracerContext::getTracer() ?: $this->tracer;
if (! $root instanceof Span) {
$container = ApplicationContext::getContainer();
$request = RequestContext::getOrNull();
if (! $request instanceof ServerRequestInterface) {
// If the request object is absent, we are probably in a commandline context.
// If the request object is absent, we are probably in a commandLine context.
// Throwing an exception is unnecessary.
$root = $this->tracer->startSpan($name, $option);
$root = $tracer->startSpan($name, $option);
$root->setTag(SPAN_KIND, $kind);
Context::set('tracer.root', $root);
TracerContext::setRoot($root);
return $root;
}
$carrier = array_map(function ($header) {
Expand All @@ -54,17 +55,17 @@ protected function startSpan(
}
}
// Extracts the context from the HTTP headers.
$spanContext = $this->tracer->extract(TEXT_MAP, $carrier);
$spanContext = $tracer->extract(TEXT_MAP, $carrier);
if ($spanContext) {
$option['child_of'] = $spanContext;
}
$root = $this->tracer->startSpan($name, $option);
$root = $tracer->startSpan($name, $option);
$root->setTag(SPAN_KIND, $kind);
Context::set('tracer.root', $root);
TracerContext::setRoot($root);
return $root;
}
$option['child_of'] = $root->getContext();
$child = $this->tracer->startSpan($name, $option);
$child = $tracer->startSpan($name, $option);
$child->setTag(SPAN_KIND, $kind);
return $child;
}
Expand Down
43 changes: 43 additions & 0 deletions src/TracerContext.php
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;
}
}

0 comments on commit 4413bb4

Please sign in to comment.