Skip to content

Commit 30a7e5d

Browse files
committed
feat: enhance Elasticsearch tracing with server and HTTP details
- Add server.address, server.port, http.request.method, and url.full tracing data - Implement tap() function for cleaner result processing - Remove TODO comments for completed HTTP tracing attributes - Add proper type hints and transport request extraction
1 parent 3095541 commit 30a7e5d

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ parameters:
4040
-
4141
message: '#.* has invalid return type Elasticsearch\\Client.#'
4242
paths:
43-
- src/telescope-elasticsearch/*/*.php
43+
- src/telescope-elasticsearch/*/*.php

src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sentry\Tracing\SpanContext;
1919

2020
use function FriendsOfHyperf\Sentry\trace;
21+
use function Hyperf\Tappable\tap;
2122

2223
class ElasticsearchAspect extends AbstractAspect
2324
{
@@ -49,13 +50,25 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
4950

5051
return trace(
5152
function (Scope $scope) use ($proceedingJoinPoint) {
52-
$result = $proceedingJoinPoint->process();
53-
if ($this->feature->isTracingTagEnabled('elasticsearch.result')) {
54-
$scope->getSpan()?->setData([
55-
'elasticsearch.result' => (string) json_encode($result, JSON_UNESCAPED_UNICODE),
56-
]);
57-
}
58-
return $result;
53+
return tap($proceedingJoinPoint->process(), function ($result) use ($scope, $proceedingJoinPoint) {
54+
if ($this->feature->isTracingTagEnabled('elasticsearch.result')) {
55+
$scope->getSpan()?->setData([
56+
'elasticsearch.result' => (string) json_encode($result, JSON_UNESCAPED_UNICODE),
57+
]);
58+
}
59+
60+
/** @var \Elastic\Elasticsearch\Client */
61+
$client = $proceedingJoinPoint->getInstance();
62+
$transport = $client->getTransport();
63+
$lastRequest = $transport->getLastRequest();
64+
$data = [
65+
'server.address' => $lastRequest->getUri()->getHost(),
66+
'server.port' => $lastRequest->getUri()->getPort(),
67+
'http.request.method' => $lastRequest->getMethod(),
68+
'url.full' => (fn ($request) => $this->getFullUrl($request))->call($transport, $lastRequest),
69+
];
70+
$scope->getSpan()?->setData($data);
71+
});
5972
},
6073
SpanContext::make()
6174
->setOp('db.query')
@@ -65,11 +78,6 @@ function (Scope $scope) use ($proceedingJoinPoint) {
6578
'db.system' => 'elasticsearch',
6679
'db.operation.name' => $proceedingJoinPoint->methodName,
6780
'arguments' => (string) json_encode($proceedingJoinPoint->arguments['keys'], JSON_UNESCAPED_UNICODE),
68-
// TODO
69-
// 'http.request.method' => '',
70-
// 'url.full' => '',
71-
// 'server.host' => '',
72-
// 'server.port' => '',
7381
])
7482
);
7583
}

0 commit comments

Comments
 (0)