Skip to content

Commit

Permalink
Update ExtAmqpInstrumentation.php
Browse files Browse the repository at this point in the history
  • Loading branch information
new999day authored May 3, 2024
1 parent 224273d commit faa30b6
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/ExtAmqpInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AMQPExchange;
use AMQPQueue;
use PhpAmqpLib\Channel\AMQPChannel;
use Composer\InstalledVersions;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
Expand All @@ -26,27 +27,29 @@ public static function register(): void
{
$instrumentation = new CachedInstrumentation(
'io.opentelemetry.contrib.php.ext_amqp',
InstalledVersions::getVersion('open-telemetry/opentelemetry-auto-ext-amqp'),
InstalledVersions::getVersion('new999day/opentelemetry-auto-ext-amqp'),
TraceAttributes::SCHEMA_URL,
);

hook(
AMQPExchange::class,
'publish',
AMQPChannel::class,
'basic_publish',
pre: static function (
AMQPExchange $exchange,
AMQPChannel $channel,
array $params,
string $class,
string $function,
?string $filename,
?int $lineno,
) use ($instrumentation): array {
$routingKey = $params[1];
$payload = isset($params[0]) ? (string)$params[0]->body : '';
$exchangeName = $params[1];
$routingKey = $params[2];

/** @psalm-suppress ArgumentTypeCoercion */
$builder = $instrumentation
->tracer()
->spanBuilder(sprintf('%s%s', $exchange->getName() != '' ? $exchange->getName() . ' ': '', $routingKey) . ' publish')
->spanBuilder(sprintf('%s%s', $exchangeName != '' ? $exchangeName . ' ': '', $routingKey) . ' publish')
->setSpanKind(SpanKind::KIND_PRODUCER)
// code
->setAttribute(TraceAttributes::CODE_FUNCTION, $function)
Expand All @@ -59,23 +62,18 @@ public static function register(): void

->setAttribute(TraceAttributes::MESSAGING_DESTINATION, $routingKey)
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_NAME, $routingKey)
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_PUBLISH_NAME, sprintf('%s%s', $exchange->getName() != '' ? $exchange->getName() . ' ': '', $routingKey))

->setAttribute(TraceAttributes::MESSAGING_DESTINATION_KIND, $exchange->getType() !== '' ? $exchange->getType() : 'unknown')
->setAttribute(TraceAttributes::MESSAGING_DESTINATION_PUBLISH_NAME, sprintf('%s%s', $exchangeName != '' ? $exchangeName . ' ': '', $routingKey))

->setAttribute(TraceAttributes::MESSAGING_RABBITMQ_ROUTING_KEY, $routingKey)
->setAttribute(TraceAttributes::MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, $routingKey)
->setAttribute(TraceAttributes::MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, $routingKey)
->setAttribute('messaging.message.body', $payload)

// network
->setAttribute(TraceAttributes::NET_PROTOCOL_NAME, 'amqp')
->setAttribute(TraceAttributes::NETWORK_PROTOCOL_NAME, 'amqp')
->setAttribute(TraceAttributes::NET_TRANSPORT, 'tcp')
->setAttribute(TraceAttributes::NETWORK_TRANSPORT, 'tcp')

->setAttribute(TraceAttributes::NET_PEER_NAME, $exchange->getConnection()->getHost())
->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $exchange->getConnection()->getHost())
->setAttribute(TraceAttributes::NET_PEER_PORT, $exchange->getConnection()->getPort())
->setAttribute(TraceAttributes::NETWORK_PEER_PORT, $exchange->getConnection()->getPort())
;

$parent = Context::getCurrent();
Expand Down Expand Up @@ -111,7 +109,7 @@ public static function register(): void
return $params;
},
post: static function (
AMQPExchange $exchange,
AMQPChannel $channel,
array $params,
?bool $success,
?Throwable $exception,
Expand Down

0 comments on commit faa30b6

Please sign in to comment.