Skip to content

Commit

Permalink
Correct spec update
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChukwu committed Oct 17, 2024
1 parent da0cf7b commit 8204d40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 51 deletions.
20 changes: 3 additions & 17 deletions src/Instrumentation/Symfony/src/MessengerInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 8204d40

Please sign in to comment.