mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-05 21:28:15 +08:00
fix: safely finish spans in case of exception
also make tracing aspects usable in CLI mode.
This commit is contained in:
parent
09bbe7a51f
commit
a367ae999a
@ -91,11 +91,15 @@ class HttpClientAspect implements AroundInterface
|
|||||||
);
|
);
|
||||||
$options['headers'] = array_replace($options['headers'] ?? [], $appendHeaders);
|
$options['headers'] = array_replace($options['headers'] ?? [], $appendHeaders);
|
||||||
$proceedingJoinPoint->arguments['keys']['options'] = $options;
|
$proceedingJoinPoint->arguments['keys']['options'] = $options;
|
||||||
$result = $proceedingJoinPoint->process();
|
|
||||||
if ($result instanceof ResponseInterface) {
|
try {
|
||||||
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
|
$result = $proceedingJoinPoint->process();
|
||||||
|
if ($result instanceof ResponseInterface) {
|
||||||
|
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
$span->finish();
|
||||||
}
|
}
|
||||||
$span->finish();
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,13 +90,16 @@ class JsonRpcAspect implements AroundInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($proceedingJoinPoint->methodName === 'send') {
|
if ($proceedingJoinPoint->methodName === 'send') {
|
||||||
$result = $proceedingJoinPoint->process();
|
try {
|
||||||
/** @var Span $span */
|
$result = $proceedingJoinPoint->process();
|
||||||
if ($span = CT::get('tracer.span.' . static::class)) {
|
} finally {
|
||||||
if ($this->spanTagManager->has('rpc', 'status')) {
|
/** @var Span $span */
|
||||||
$span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed');
|
if ($span = CT::get('tracer.span.' . static::class)) {
|
||||||
|
if ($this->spanTagManager->has('rpc', 'status')) {
|
||||||
|
$span->setTag($this->spanTagManager->get('rpc', 'status'), isset($result['result']) ? 'OK' : 'Failed');
|
||||||
|
}
|
||||||
|
$span->finish();
|
||||||
}
|
}
|
||||||
$span->finish();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -58,8 +58,11 @@ class MethodAspect extends AbstractAspect
|
|||||||
|
|
||||||
$key = $proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName;
|
$key = $proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName;
|
||||||
$span = $this->startSpan($key);
|
$span = $this->startSpan($key);
|
||||||
$result = $proceedingJoinPoint->process();
|
try {
|
||||||
$span->finish();
|
$result = $proceedingJoinPoint->process();
|
||||||
|
} finally {
|
||||||
|
$span->finish();
|
||||||
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,9 +74,12 @@ class RedisAspect implements AroundInterface
|
|||||||
$arguments = $proceedingJoinPoint->arguments['keys'];
|
$arguments = $proceedingJoinPoint->arguments['keys'];
|
||||||
$span = $this->startSpan('Redis' . '::' . $arguments['name']);
|
$span = $this->startSpan('Redis' . '::' . $arguments['name']);
|
||||||
$span->setTag($this->spanTagManager->get('redis', 'arguments'), json_encode($arguments['arguments']));
|
$span->setTag($this->spanTagManager->get('redis', 'arguments'), json_encode($arguments['arguments']));
|
||||||
$result = $proceedingJoinPoint->process();
|
try {
|
||||||
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
|
$result = $proceedingJoinPoint->process();
|
||||||
$span->finish();
|
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
|
||||||
|
} finally {
|
||||||
|
$span->finish();
|
||||||
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,11 @@ class TraceAnnotationAspect implements AroundInterface
|
|||||||
}
|
}
|
||||||
$span = $this->startSpan($name);
|
$span = $this->startSpan($name);
|
||||||
$span->setTag($tag, $source);
|
$span->setTag($tag, $source);
|
||||||
$result = $proceedingJoinPoint->process();
|
try {
|
||||||
$span->finish();
|
$result = $proceedingJoinPoint->process();
|
||||||
|
} finally {
|
||||||
|
$span->finish();
|
||||||
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,12 @@ trait SpanStarter
|
|||||||
/** @var ServerRequestInterface $request */
|
/** @var ServerRequestInterface $request */
|
||||||
$request = Context::get(ServerRequestInterface::class);
|
$request = Context::get(ServerRequestInterface::class);
|
||||||
if (! $request instanceof ServerRequestInterface) {
|
if (! $request instanceof ServerRequestInterface) {
|
||||||
throw new \RuntimeException('ServerRequest object missing.');
|
// 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->setTag(SPAN_KIND, $kind);
|
||||||
|
Context::set('tracer.root', $root);
|
||||||
|
return $root;
|
||||||
}
|
}
|
||||||
$carrier = array_map(function ($header) {
|
$carrier = array_map(function ($header) {
|
||||||
return $header[0];
|
return $header[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user