fix: safely finish spans in case of exception

also make tracing aspects usable in CLI mode.
This commit is contained in:
Reasno 2020-11-05 12:15:17 +08:00 committed by 李铭昕
parent 09bbe7a51f
commit a367ae999a
6 changed files with 39 additions and 18 deletions

View File

@ -91,11 +91,15 @@ class HttpClientAspect implements AroundInterface
);
$options['headers'] = array_replace($options['headers'] ?? [], $appendHeaders);
$proceedingJoinPoint->arguments['keys']['options'] = $options;
$result = $proceedingJoinPoint->process();
if ($result instanceof ResponseInterface) {
$span->setTag($this->spanTagManager->get('http_client', 'http.status_code'), $result->getStatusCode());
try {
$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;
}
}

View File

@ -90,13 +90,16 @@ class JsonRpcAspect implements AroundInterface
}
if ($proceedingJoinPoint->methodName === 'send') {
$result = $proceedingJoinPoint->process();
/** @var Span $span */
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');
try {
$result = $proceedingJoinPoint->process();
} finally {
/** @var Span $span */
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;

View File

@ -58,8 +58,11 @@ class MethodAspect extends AbstractAspect
$key = $proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName;
$span = $this->startSpan($key);
$result = $proceedingJoinPoint->process();
$span->finish();
try {
$result = $proceedingJoinPoint->process();
} finally {
$span->finish();
}
return $result;
}
}

View File

@ -74,9 +74,12 @@ class RedisAspect implements AroundInterface
$arguments = $proceedingJoinPoint->arguments['keys'];
$span = $this->startSpan('Redis' . '::' . $arguments['name']);
$span->setTag($this->spanTagManager->get('redis', 'arguments'), json_encode($arguments['arguments']));
$result = $proceedingJoinPoint->process();
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
$span->finish();
try {
$result = $proceedingJoinPoint->process();
$span->setTag($this->spanTagManager->get('redis', 'result'), json_encode($result));
} finally {
$span->finish();
}
return $result;
}
}

View File

@ -58,8 +58,11 @@ class TraceAnnotationAspect implements AroundInterface
}
$span = $this->startSpan($name);
$span->setTag($tag, $source);
$result = $proceedingJoinPoint->process();
$span->finish();
try {
$result = $proceedingJoinPoint->process();
} finally {
$span->finish();
}
return $result;
}
}

View File

@ -36,7 +36,12 @@ trait SpanStarter
/** @var ServerRequestInterface $request */
$request = Context::get(ServerRequestInterface::class);
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) {
return $header[0];