Skip to content

Commit

Permalink
updated attribute names
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Jan 4, 2024
1 parent 844693a commit f433886
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/Instrumentation/QueryInstrumentation.php
Expand Up @@ -40,8 +40,8 @@ public function recordQuery(QueryExecuted $event): void
TraceAttributes::DB_OPERATION => $operationName,
TraceAttributes::DB_STATEMENT => $event->sql,
TraceAttributes::DB_USER => $event->connection->getConfig('username'),
TraceAttributes::NET_PEER_NAME => $event->connection->getConfig('host'),
TraceAttributes::NET_PEER_PORT => $event->connection->getConfig('port'),
TraceAttributes::SERVER_ADDRESS => $event->connection->getConfig('host'),
TraceAttributes::SERVER_PORT => $event->connection->getConfig('port'),
]);

$span->end();
Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/RedisInstrumentation.php
Expand Up @@ -35,7 +35,7 @@ public function recordCommand(CommandExecuted $event): void
if ($span->isRecording()) {
$span->setAttribute(TraceAttributes::DB_SYSTEM, 'redis')
->setAttribute(TraceAttributes::DB_STATEMENT, $this->formatCommand($event->command, $event->parameters))
->setAttribute(TraceAttributes::NET_PEER_NAME, $event->connection->client()->getHost());
->setAttribute(TraceAttributes::SERVER_ADDRESS, $event->connection->client()->getHost());
}

$span->end();
Expand Down
19 changes: 10 additions & 9 deletions src/Support/HttpClient/GuzzleTraceMiddleware.php
Expand Up @@ -20,13 +20,14 @@ public static function make(): Closure
$span = Tracer::build(sprintf('HTTP %s', $request->getMethod()))
->setSpanKind(SpanKind::KIND_CLIENT)
->setAttributes([
TraceAttributes::HTTP_METHOD => $request->getMethod(),
TraceAttributes::HTTP_FLAVOR => $request->getProtocolVersion(),
TraceAttributes::HTTP_URL => sprintf('%s://%s%s', $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getUri()->getPath()),
TraceAttributes::HTTP_TARGET => $request->getUri()->getPath(),
TraceAttributes::HTTP_HOST => $request->getUri()->getHost(),
TraceAttributes::HTTP_SCHEME => $request->getUri()->getScheme(),
TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH => $request->getBody()->getSize(),
TraceAttributes::URL_FULL => sprintf('%s://%s%s', $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getUri()->getPath()),
TraceAttributes::URL_PATH => $request->getUri()->getPath(),
TraceAttributes::URL_QUERY => $request->getUri()->getQuery(),
TraceAttributes::HTTP_REQUEST_METHOD => $request->getMethod(),
TraceAttributes::HTTP_REQUEST_BODY_SIZE => $request->getBody()->getSize(),
TraceAttributes::URL_SCHEME => $request->getUri()->getScheme(),
TraceAttributes::SERVER_ADDRESS => $request->getUri()->getHost(),
TraceAttributes::SERVER_PORT => $request->getUri()->getPort(),
])
->startSpan();

Expand All @@ -41,8 +42,8 @@ public static function make(): Closure

return $promise->then(function (Response $response) use ($span) {
$span->setAttributes([
TraceAttributes::HTTP_STATUS_CODE => $response->getStatusCode(),
TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH => $response->getHeader('Content-Length')[0] ?? null,
TraceAttributes::HTTP_RESPONSE_STATUS_CODE => $response->getStatusCode(),
TraceAttributes::HTTP_REQUEST_BODY_SIZE => $response->getHeader('Content-Length')[0] ?? null,
]);

if ($response->getStatusCode() >= 400) {
Expand Down
24 changes: 14 additions & 10 deletions src/Support/HttpServer/TraceRequestMiddleware.php
Expand Up @@ -57,23 +57,27 @@ protected function startTracing(Request $request): SpanInterface

Tracer::setRootSpan($span);

$span->setAttribute(TraceAttributes::HTTP_METHOD, $request->method())
->setAttribute(TraceAttributes::HTTP_URL, $request->getUri())
->setAttribute(TraceAttributes::HTTP_TARGET, $request->getRequestUri())
$span
->setAttribute(TraceAttributes::URL_FULL, $request->fullUrl())
->setAttribute(TraceAttributes::URL_PATH, $request->path() === '/' ? $request->path() : '/'.$request->path())
->setAttribute(TraceAttributes::URL_QUERY, $request->getQueryString())
->setAttribute(TraceAttributes::URL_SCHEME, $request->getScheme())
->setAttribute(TraceAttributes::HTTP_ROUTE, $route)
->setAttribute(TraceAttributes::HTTP_HOST, $request->getHttpHost())
->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getScheme())
->setAttribute(TraceAttributes::HTTP_USER_AGENT, $request->userAgent())
->setAttribute(TraceAttributes::HTTP_CLIENT_IP, $request->ip())
->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->header('Content-Length'));
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->method())
->setAttribute(TraceAttributes::HTTP_REQUEST_BODY_SIZE, $request->header('Content-Length'))
->setAttribute(TraceAttributes::SERVER_ADDRESS, $request->getHttpHost())
->setAttribute(TraceAttributes::SERVER_PORT, $request->getPort())
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->userAgent())
->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion())
->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $request->ip());

return $span;
}

protected function recordHttpResponseToSpan(SpanInterface $span, Response $response): void
{
$span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $response->getStatusCode())
->setAttribute(TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH, strlen($response->getContent()));
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode())
->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, strlen($response->getContent()));

if ($response->isSuccessful()) {
$span->setStatus(StatusCode::STATUS_OK);
Expand Down
17 changes: 10 additions & 7 deletions tests/Instrumentation/HttpClientInstrumentationTest.php
Expand Up @@ -58,12 +58,15 @@
->getName()->toBe('HTTP GET')
->getStatus()->getCode()->toBe(StatusCode::STATUS_UNSET)
->getAttributes()->toMatchArray([
'http.method' => 'GET',
'http.url' => 'http://127.0.0.1/',
'http.target' => '/',
'http.request_content_length' => 0,
'http.response_content_length' => 0,
'http.status_code' => 200,
'url.full' => 'http://127.0.0.1/',
'url.path' => '/',
'url.query' => '',
'http.request.method' => 'GET',
'http.request.body.size' => '0',
'url.scheme' => 'http',
'server.address' => '127.0.0.1',
'server.port' => 8126,
'http.response.status_code' => 200,
]);
});

Expand All @@ -83,6 +86,6 @@
->getName()->toBe('HTTP GET')
->getStatus()->getCode()->toBe(StatusCode::STATUS_ERROR)
->getAttributes()->toMatchArray([
'http.status_code' => 500,
'http.response.status_code' => 500,
]);
});
42 changes: 24 additions & 18 deletions tests/Instrumentation/HttpServerInstrumentationTest.php
Expand Up @@ -33,15 +33,18 @@
->getStatus()->getCode()->toBe(StatusCode::STATUS_OK)
->getTraceId()->toBe($traceId)
->getAttributes()->toMatchArray([
'http.method' => 'GET',
'http.url' => 'http://localhost/test-ok',
'http.target' => '/test-ok',
'url.full' => 'http://localhost/test-ok',
'url.path' => '/test-ok',
'url.scheme' => 'http',
'http.route' => '/test-ok',
'http.host' => 'localhost',
'http.scheme' => 'http',
'http.user_agent' => 'Symfony',
'http.status_code' => 200,
'http.response_content_length' => 32,
'http.request.method' => 'GET',
'server.address' => 'localhost',
'server.port' => 80,
'user_agent.original' => 'Symfony',
'network.protocol.version' => 'HTTP/1.1',
'network.peer.address' => '127.0.0.1',
'http.response.status_code' => 200,
'http.response.body.size' => 32,
]);
});

Expand All @@ -60,14 +63,17 @@
->getKind()->toBe(SpanKind::KIND_SERVER)
->getStatus()->getCode()->toBe(StatusCode::STATUS_ERROR)
->getAttributes()->toMatchArray([
'http.method' => 'GET',
'http.url' => 'http://localhost/test-exception',
'http.target' => '/test-exception',
'url.full' => 'http://localhost/test-exception',
'url.path' => '/test-exception',
'url.scheme' => 'http',
'http.route' => '/test-exception',
'http.host' => 'localhost',
'http.scheme' => 'http',
'http.user_agent' => 'Symfony',
'http.status_code' => 500,
'http.request.method' => 'GET',
'server.address' => 'localhost',
'server.port' => 80,
'user_agent.original' => 'Symfony',
'network.protocol.version' => 'HTTP/1.1',
'network.peer.address' => '127.0.0.1',
'http.response.status_code' => 500,
]);
});

Expand All @@ -86,10 +92,10 @@
->getKind()->toBe(SpanKind::KIND_SERVER)
->getStatus()->getCode()->toBe(StatusCode::STATUS_OK)
->getAttributes()->toMatchArray([
'http.method' => 'GET',
'http.url' => 'http://localhost/test/user1',
'http.target' => '/test/user1',
'url.full' => 'http://localhost/test/user1',
'url.path' => '/test/user1',
'http.route' => '/test/{parameter}',
'http.request.method' => 'GET',
]);
});

Expand Down
4 changes: 2 additions & 2 deletions tests/Instrumentation/RedisInstrumentationTest.php
Expand Up @@ -16,10 +16,10 @@
expect($span)
->getName()->toBe('redis default get')
->getKind()->toBe(SpanKind::KIND_CLIENT)
->getAttributes()->toArray()->toMatchArray([
->getAttributes()->toArray()->toBe([
'db.system' => 'redis',
'db.statement' => 'get test',
'net.peer.name' => '127.0.0.1',
'server.address' => '127.0.0.1',
])
->hasEnded()->toBeTrue()
->getEndEpochNanos()->toBeLessThan(ClockFactory::getDefault()->now());
Expand Down

0 comments on commit f433886

Please sign in to comment.