Skip to content

[Messenger] Add X-Ray trace header support to the SQS transport #40718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Added new `debug` option to log HTTP requests and responses.
* Allowed for receiver & sender injection into AmazonSqsTransport
* Add X-Ray trace header support to the SQS transport

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsFifoStamp;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsSender;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsXrayTraceHeaderStamp;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand Down Expand Up @@ -53,4 +54,22 @@ public function testSendWithAmazonSqsFifoStamp()
$sender = new AmazonSqsSender($connection, $serializer);
$sender->send($envelope);
}

public function testSendWithAmazonSqsXrayTraceHeaderStamp()
{
$envelope = (new Envelope(new DummyMessage('Oy')))
->with($stamp = new AmazonSqsXrayTraceHeaderStamp('traceHeader'));

$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')
->with($encoded['body'], $encoded['headers'], 0, null, null, $stamp->getTraceId());

$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);

$sender = new AmazonSqsSender($connection, $serializer);
$sender->send($envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function send(Envelope $envelope): Envelope

$messageGroupId = null;
$messageDeduplicationId = null;
$xrayTraceId = null;

/** @var AmazonSqsFifoStamp|null $amazonSqsFifoStamp */
$amazonSqsFifoStamp = $envelope->last(AmazonSqsFifoStamp::class);
Expand All @@ -53,13 +54,20 @@ public function send(Envelope $envelope): Envelope
$messageDeduplicationId = $amazonSqsFifoStamp->getMessageDeduplicationId();
}

/** @var AmazonSqsXrayTraceHeaderStamp|null $amazonSqsXrayTraceHeaderStamp */
$amazonSqsXrayTraceHeaderStamp = $envelope->last(AmazonSqsXrayTraceHeaderStamp::class);
if (null !== $amazonSqsXrayTraceHeaderStamp) {
$xrayTraceId = $amazonSqsXrayTraceHeaderStamp->getTraceId();
}

try {
$this->connection->send(
$encodedMessage['body'],
$encodedMessage['headers'] ?? [],
$delay,
$messageGroupId,
$messageDeduplicationId
$messageDeduplicationId,
$xrayTraceId
);
} catch (HttpException $e) {
throw new TransportException($e->getMessage(), 0, $e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Transport;

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

final class AmazonSqsXrayTraceHeaderStamp implements NonSendableStampInterface
{
private $traceId;

public function __construct(string $traceId)
{
$this->traceId = $traceId;
}

public function getTraceId(): string
{
return $this->traceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Transport;

use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
use AsyncAws\Sqs\Enum\QueueAttributeName;
use AsyncAws\Sqs\Result\ReceiveMessageResult;
use AsyncAws\Sqs\SqsClient;
use AsyncAws\Sqs\ValueObject\MessageAttributeValue;
use AsyncAws\Sqs\ValueObject\MessageSystemAttributeValue;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\TransportException;
Expand Down Expand Up @@ -312,7 +314,7 @@ public function getMessageCount(): int
return (int) ($attributes[QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES] ?? 0);
}

public function send(string $body, array $headers, int $delay = 0, ?string $messageGroupId = null, ?string $messageDeduplicationId = null): void
public function send(string $body, array $headers, int $delay = 0, ?string $messageGroupId = null, ?string $messageDeduplicationId = null, ?string $xrayTraceId = null): void
{
if ($this->configuration['auto_setup']) {
$this->setup();
Expand All @@ -323,6 +325,7 @@ public function send(string $body, array $headers, int $delay = 0, ?string $mess
'MessageBody' => $body,
'DelaySeconds' => $delay,
'MessageAttributes' => [],
'MessageSystemAttributes' => [],
];

$specialHeaders = [];
Expand All @@ -346,6 +349,13 @@ public function send(string $body, array $headers, int $delay = 0, ?string $mess
]);
}

if (null !== $xrayTraceId) {
$parameters['MessageSystemAttributes'][MessageSystemAttributeName::AWSTRACE_HEADER] = new MessageSystemAttributeValue([
'DataType' => 'String',
'StringValue' => $xrayTraceId,
]);
}

if (self::isFifoQueue($this->configuration['queue_name'])) {
$parameters['MessageGroupId'] = null !== $messageGroupId ? $messageGroupId : __METHOD__;
$parameters['MessageDeduplicationId'] = null !== $messageDeduplicationId ? $messageDeduplicationId : sha1(json_encode(['body' => $body, 'headers' => $headers]));
Expand Down