Skip to content

Commit

Permalink
feat: allow passing request and response callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Dec 20, 2024
1 parent a65e44d commit e543ffb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Http/TracingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
use Instrumentation\Semantics\OperationName\ClientRequestOperationNameResolver;
use Instrumentation\Semantics\OperationName\ClientRequestOperationNameResolverInterface;
use Instrumentation\Tracing\Tracing;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SemConv\TraceAttributes;
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Component\HttpClient\HttpClient;
Expand Down
17 changes: 6 additions & 11 deletions src/Semantics/Attribute/ClientRequestAttributeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function __construct(private array $capturedHeaders = [])
public function getAttributes(string $method, string $url, array $headers = []): array
{
$attributes = [
TraceAttributes::HTTP_METHOD => strtoupper($method),
TraceAttributes::HTTP_URL => HttpSensitiveDataHelper::filterUrl($url),
TraceAttributes::HTTP_REQUEST_METHOD => strtoupper($method),
TraceAttributes::URL_FULL => HttpSensitiveDataHelper::filterUrl($url),
];

foreach ($this->capturedHeaders as $header) {
Expand All @@ -46,15 +46,10 @@ public function getAttributes(string $method, string $url, array $headers = []):
}

$attributes += [
TraceAttributes::HTTP_TARGET => $components['path'] ?? null,
TraceAttributes::HTTP_HOST => $components['host'] ?? null,
TraceAttributes::HTTP_SCHEME => $components['scheme'] ?? null,
TraceAttributes::NET_HOST_PORT => isset($components['port']) ? (string) $components['port'] : null,

TraceAttributes::HTTP_FLAVOR => null,
TraceAttributes::HTTP_USER_AGENT => null,
TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH => null,
TraceAttributes::HTTP_CLIENT_IP => null,
TraceAttributes::URL_PATH => $components['path'] ?? null,
TraceAttributes::URL_PORT => $components['host'] ?? null,
TraceAttributes::URL_SCHEME => $components['scheme'] ?? null,
TraceAttributes::SERVER_PORT => isset($components['port']) ? (string) $components['port'] : null,
];

return array_filter($attributes);
Expand Down
14 changes: 7 additions & 7 deletions src/Semantics/Attribute/DoctrineConnectionAttributeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ public function getAttributes(Platforms\AbstractPlatform $platform, array $param
$attributes = [TraceAttributes::DB_SYSTEM => $this->getSystemAttribute($platform)];

if (isset($params['user'])) {
$attributes[TraceAttributes::DB_USER] = $params['user'];
$attributes['db.user'] = $params['user'];
}

if (isset($params['dbname'])) {
$attributes[TraceAttributes::DB_NAME] = $params['dbname'];
$attributes[TraceAttributes::DB_NAMESPACE] = $params['dbname'];
}

if (isset($params['host']) && !empty($params['host']) && !isset($params['memory'])) {
if (false === filter_var($params['host'], \FILTER_VALIDATE_IP)) {
$attributes[TraceAttributes::NET_PEER_NAME] = $params['host'];
$attributes[TraceAttributes::SERVER_ADDRESS] = $params['host'];
} else {
$attributes[TraceAttributes::NET_PEER_IP] = $params['host'];
$attributes[TraceAttributes::NETWORK_PEER_ADDRESS] = $params['host'];
}
}

if (isset($params['port'])) {
$attributes[TraceAttributes::NET_PEER_PORT] = (string) $params['port'];
$attributes[TraceAttributes::SERVER_PORT] = (string) $params['port'];
}

if (isset($params['unix_socket'])) {
$attributes[TraceAttributes::NET_TRANSPORT] = 'unix';
$attributes[TraceAttributes::NETWORK_TRANSPORT] = 'unix';
} elseif (isset($params['memory'])) {
$attributes[TraceAttributes::NET_TRANSPORT] = 'inproc';
$attributes[TraceAttributes::NETWORK_TRANSPORT] = 'inproc';
}

return $attributes;
Expand Down
2 changes: 0 additions & 2 deletions src/Semantics/Attribute/MessageAttributeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ class MessageAttributeProvider implements MessageAttributeProviderInterface
public function getAttributes(Envelope $envelope): array
{
$attributes = [
TraceAttributes::MESSAGING_DESTINATION_KIND => 'queue',
'messenger.message' => \get_class($envelope->getMessage()),
];

if ($envelope->last(RedisReceivedStamp::class)) { // @phpstan-ignore-line
$attributes[TraceAttributes::MESSAGING_SYSTEM] = 'redis';
} elseif ($envelope->last(AmqpReceivedStamp::class)) { // @phpstan-ignore-line
$attributes[TraceAttributes::MESSAGING_SYSTEM] = 'rabbitmq';
$attributes[TraceAttributes::MESSAGING_PROTOCOL] = 'AMQP';
}

/** @var TransportMessageIdStamp|null $stamp */
Expand Down
2 changes: 0 additions & 2 deletions src/Semantics/Attribute/MessageAttributeProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ interface MessageAttributeProviderInterface
* @return array{
* 'messenger.message':class-string,
* 'messenger.bus'?:string,
* 'messaging.destination.kind':'queue'|'topic',
* 'messaging.system'?:'rabbitmq'|'redis',
* 'messaging.protocol'?:'AMQP',
* 'messaging.message_id'?:string
* }
*/
Expand Down
30 changes: 13 additions & 17 deletions src/Semantics/Attribute/ServerRequestAttributeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@ public function __construct(private string|null $serverName = null, private arra
public function getAttributes(Request $request): array
{
$attributes = [
TraceAttributes::HTTP_SERVER_NAME => $this->serverName,
TraceAttributes::HTTP_METHOD => $request->getMethod(),
TraceAttributes::HTTP_TARGET => $request->getRequestUri(),
TraceAttributes::HTTP_HOST => $request->headers->get('host'), // Per spec, only if host header is present
TraceAttributes::CLIENT_ADDRESS => $request->getClientIp(),
TraceAttributes::SERVER_ADDRESS => $this->serverName ?: $request->getHost(),
TraceAttributes::SERVER_PORT => (string) $request->getPort(),
TraceAttributes::NETWORK_PROTOCOL_NAME => substr((string) $request->getProtocolVersion(), 5),
TraceAttributes::URL_SCHEME => $request->getScheme(),
TraceAttributes::URL_DOMAIN => $request->getHost(),
TraceAttributes::URL_PATH => $request->getPathInfo(),
TraceAttributes::URL_QUERY => $request->getQueryString(),
TraceAttributes::HTTP_REQUEST_METHOD => $request->getMethod(),
TraceAttributes::HTTP_ROUTE => $request->attributes->get('_route'),
TraceAttributes::HTTP_SCHEME => $request->getScheme(),
TraceAttributes::HTTP_FLAVOR => substr((string) $request->getProtocolVersion(), 5),
TraceAttributes::HTTP_USER_AGENT => $request->headers->get('user-agent', null),
TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH => $request->headers->get('content-length', null),
TraceAttributes::HTTP_CLIENT_IP => $request->getClientIp(),
TraceAttributes::NET_HOST_PORT => (string) $request->getPort(),
TraceAttributes::USER_AGENT_ORIGINAL => $request->headers->get('user-agent', null),
'http.request.header.host' => $request->headers->get('host'), // Per spec, only if host header is present
'http.request.header.content-length' => $request->headers->get('content-length', null),
];

if ($this->serverName) {
$attributes[TraceAttributes::HTTP_SERVER_NAME] = $this->serverName;
} else {
$attributes[TraceAttributes::NET_HOST_NAME] = $request->getHost();
}

foreach ($this->capturedHeaders as $header) {
$attributes[\sprintf('http.response.header.%s', str_replace('-', '_', $header))] = [(string) $request->headers->get($header, '')];
$attributes[\sprintf('http.request.header.%s', str_replace('-', '_', $header))] = [(string) $request->headers->get($header, '')];
}

return array_filter($attributes);
Expand Down
4 changes: 2 additions & 2 deletions src/Semantics/Attribute/ServerResponseAttributeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ServerResponseAttributeProvider implements ServerResponseAttributeProvider
public function getAttributes(Response $response): array
{
return array_filter([
TraceAttributes::HTTP_STATUS_CODE => (string) $response->getStatusCode(),
TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED => $response->headers->get('content-length'),
TraceAttributes::HTTP_RESPONSE_STATUS_CODE => (string) $response->getStatusCode(),
TraceAttributes::HTTP_RESPONSE_BODY_SIZE => $response->headers->get('content-length'),
]);
}
}

0 comments on commit e543ffb

Please sign in to comment.