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); $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;
} }
} }

View File

@ -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;

View File

@ -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;
} }
} }

View File

@ -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;
} }
} }

View File

@ -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;
} }
} }

View File

@ -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];