diff --git a/phpstan.neon.dist b/phpstan.neon.dist index f0bc5e436..95b8279f3 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -40,4 +40,4 @@ parameters: - message: '#.* has invalid return type Elasticsearch\\Client.#' paths: - - src/telescope-elasticsearch/*/*.php \ No newline at end of file + - src/telescope-elasticsearch/*/*.php diff --git a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php index 08177576a..0910bba03 100644 --- a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php +++ b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php @@ -16,8 +16,10 @@ use Hyperf\Di\Aop\ProceedingJoinPoint; use Sentry\State\Scope; use Sentry\Tracing\SpanContext; +use Throwable; use function FriendsOfHyperf\Sentry\trace; +use function Hyperf\Tappable\tap; class ElasticsearchAspect extends AbstractAspect { @@ -62,13 +64,25 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return trace( function (Scope $scope) use ($proceedingJoinPoint) { - $result = $proceedingJoinPoint->process(); - if ($this->feature->isTracingTagEnabled('elasticsearch.result')) { - $scope->getSpan()?->setData([ - 'elasticsearch.result' => (string) json_encode($result, JSON_UNESCAPED_UNICODE), - ]); - } - return $result; + return tap($proceedingJoinPoint->process(), function ($result) use ($scope, $proceedingJoinPoint) { + if ($this->feature->isTracingTagEnabled('elasticsearch.result')) { + $scope->getSpan()?->setData([ + 'elasticsearch.result' => (string) json_encode($result, JSON_UNESCAPED_UNICODE), + ]); + } + + try { + $client = $proceedingJoinPoint->getInstance(); + $data = match ($client::class) { + 'Elasticsearch\Client' => $this->getV7Data($client), // @phpstan-ignore-line + 'Elastic\Elasticsearch\Client' => $this->getV8Data($client), // @phpstan-ignore-line + default => [], + }; + $scope->getSpan()?->setData($data); + } catch (Throwable) { + // Ignore errors + } + }); }, SpanContext::make() ->setOp('db.query') @@ -78,12 +92,33 @@ function (Scope $scope) use ($proceedingJoinPoint) { 'db.system' => 'elasticsearch', 'db.operation.name' => $proceedingJoinPoint->methodName, 'arguments' => (string) json_encode($proceedingJoinPoint->arguments['keys'], JSON_UNESCAPED_UNICODE), - // TODO - // 'http.request.method' => '', - // 'url.full' => '', - // 'server.host' => '', - // 'server.port' => '', ]) ); } + + protected function getV7Data($client): array + { + $lastConnection = $client->transport->getLastConnection(); + $lastRequestInfo = $lastConnection->getLastRequestInfo(); + + return [ + 'server.address' => $lastConnection->getHost(), + 'server.port' => $lastConnection->getPort(), + 'http.request.method' => $lastRequestInfo['request']['http_method'] ?? null, + 'url.full' => $lastRequestInfo['response']['effective_url'] ?? null, + ]; + } + + protected function getV8Data($client): array + { + $transport = $client->getTransport(); + $lastRequest = $transport->getLastRequest(); + + return [ + 'server.address' => $lastRequest->getUri()->getHost(), + 'server.port' => $lastRequest->getUri()->getPort(), + 'http.request.method' => $lastRequest->getMethod(), + 'url.full' => (fn ($request) => $this->getFullUrl($request))->call($transport, $lastRequest), + ]; + } }