From 8204d403e826cb2eeec66a853fed39d6d3e3e3db Mon Sep 17 00:00:00 2001 From: Richard Chukwu Date: Thu, 17 Oct 2024 22:28:28 +0100 Subject: [PATCH] Correct spec update --- .../Symfony/src/MessengerInstrumentation.php | 20 ++----- .../MessengerInstrumentationTest.php | 53 +++++++------------ 2 files changed, 22 insertions(+), 51 deletions(-) diff --git a/src/Instrumentation/Symfony/src/MessengerInstrumentation.php b/src/Instrumentation/Symfony/src/MessengerInstrumentation.php index 2798e49f..a3d37444 100644 --- a/src/Instrumentation/Symfony/src/MessengerInstrumentation.php +++ b/src/Instrumentation/Symfony/src/MessengerInstrumentation.php @@ -118,7 +118,6 @@ public static function register(): void // Instrument sending as a "send" operation with SpanKind::KIND_PRODUCER $builder = $instrumentation -<<<<<<< HEAD ->tracer() ->spanBuilder(\sprintf('send %s', $messageClass)) ->setSpanKind(SpanKind::KIND_PRODUCER) // Set KIND_PRODUCER for sending @@ -128,19 +127,6 @@ public static function register(): void ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) ->setAttribute(self::ATTRIBUTE_MESSENGER_TRANSPORT, $class) ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass); -======= - ->tracer() - ->spanBuilder(\sprintf('SEND %s', $messageClass)) - ->setSpanKind(SpanKind::KIND_PRODUCER) - ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) - ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) - ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) - - ->setAttribute(self::ATTRIBUTE_MESSENGER_TRANSPORT, $class) - ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass) - ; ->>>>>>> 78a04cebaeba48d60a00dc1c48653695b926299d $parent = Context::getCurrent(); $span = $builder @@ -180,9 +166,9 @@ public static function register(): void // Instrument the receiving of messages (consumer-side) hook( - SenderInterface::class, - 'receive', - pre: static function ( + ReceiverInterface::class, + 'get', + static function ( SenderInterface $bus, array $params, string $class, diff --git a/src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php b/src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php index 347a88de..5c7cbfbc 100644 --- a/src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php +++ b/src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php @@ -12,7 +12,7 @@ use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport; use Symfony\Component\Messenger\Transport\InMemoryTransport as LegacyInMemoryTransport; -use Symfony\Component\Messenger\Transport\TransportInterface; +use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; final class SendEmailMessage { @@ -38,52 +38,37 @@ protected function getMessenger(): MessageBusInterface protected function getTransport() { - // Symfony 6+ + // Symfony 6+ version of the transport if (class_exists(InMemoryTransport::class)) { return new InMemoryTransport(); } - // Symfony 5+ + // Symfony 5+ fallback return new LegacyInMemoryTransport(); } - /** - * @dataProvider dispatchDataProvider - * @param mixed $message - * @param string $spanName - * @param int $kind - * @param array $attributes - */ - public function test_dispatch_message($message, string $spanName, int $kind, array $attributes) + protected function getReceiver() { - $bus = $this->getMessenger(); - $bus->dispatch($message); - - $this->assertCount(1, $this->storage); - - /** @var ImmutableSpan $span */ - $span = $this->storage[0]; - - $this->assertEquals($spanName, $span->getName()); - $this->assertEquals($kind, $span->getKind()); - - foreach ($attributes as $key => $value) { - $this->assertTrue($span->getAttributes()->has($key), sprintf('Attribute %s not found', $key)); - $this->assertEquals($value, $span->getAttributes()->get($key)); + // Symfony 6+ version of the receiver + if (class_exists(ReceiverInterface::class)) { + return new InMemoryTransport(); // Example transport acting as a receiver } + + // Symfony 5+ fallback + return new LegacyInMemoryTransport(); } /** - * @dataProvider sendDataProvider + * @dataProvider dispatchDataProvider * @param mixed $message * @param string $spanName * @param int $kind * @param array $attributes */ - public function test_send_message($message, string $spanName, int $kind, array $attributes) + public function test_dispatch_message($message, string $spanName, int $kind, array $attributes) { - $transport = $this->getTransport(); - $transport->send(new Envelope($message)); + $bus = $this->getMessenger(); + $bus->dispatch($message); // Target the correct interface (MessageBusInterface) $this->assertCount(1, $this->storage); @@ -104,12 +89,12 @@ public function test_send_message($message, string $spanName, int $kind, array $ */ public function test_consume_message() { - $transport = $this->getTransport(); + $transport = $this->getReceiver(); // Use the correct receiver interface $message = new SendEmailMessage('Hello Consumer'); $envelope = new Envelope($message); - - // Simulate receiving the message via the transport - $transport->send($envelope); + + // Simulate receiving the message via ReceiverInterface::get + $transport->get(); // Simulate message consumption (processing) $bus = $this->getMessenger(); @@ -228,7 +213,7 @@ public function dispatchDataProvider(): array [ new SendEmailMessage('Hello Again'), 'DISPATCH OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage', - SpanKind::KIND_PRODUCER, + SpanKind::KIND_PROCESS, // Correct SpanKind for dispatching [ MessengerInstrumentation::ATTRIBUTE_MESSENGER_BUS => 'Symfony\Component\Messenger\MessageBus', MessengerInstrumentation::ATTRIBUTE_MESSENGER_MESSAGE => 'OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage',