8000 feature #38998 [Messenger][SQS] Make sure one can enable debug logs (… · symfony/symfony@1d945b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d945b9

Browse files
committed
feature #38998 [Messenger][SQS] Make sure one can en 10000 able debug logs (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Messenger][SQS] Make sure one can enable debug logs | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | Needed If you add `&debug=true` on your DSN, then we will use `LoggerInterface::debug()` to print HTTP requests and responses. This has a negative impact on performance, but it will be helpful when debugging. Commits ------- 66edc59 [Messenger][SQS] Make sure one can enable debug logs
2 parents f0ff411 + 66edc59 commit 1d945b9

File tree

7 files changed

+114
-7
lines changed

7 files changed

+114
-7
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"twig/markdown-extra": "^2.12"
139139
},
140140
"conflict": {
141+
"async-aws/core": "<1.5",
141142
"doctrine/dbal": "<2.10",
142143
"masterminds/html5": "<2.6",
143144
"phpdocumentor/reflection-docblock": "<3.2.2",

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
->tag('kernel.reset', ['method' => 'reset'])
129129

130130
->set('messenger.transport.sqs.factory', AmazonSqsTransportFactory::class)
131+
->args([
132+
service('logger')->ignoreOnInvalid(),
133+
])
131134

132135
->set('messenger.transport.beanstalkd.factory', BeanstalkdTransportFactory::class)
133136

src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added new `debug` option to log HTTP requests and responses.
8+
49
5.2.0
510
-----
611

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use AsyncAws\Sqs\SqsClient;
1919
use AsyncAws\Sqs\ValueObject\Message;
2020
use PHPUnit\Framework\TestCase;
21+
use Psr\Log\NullLogger;
22+
use Symfony\Component\HttpClient\MockHttpClient;
23+
use Symfony\Component\HttpClient\Response\MockResponse;
2124
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
2225
use Symfony\Contracts\HttpClient\HttpClientInterface;
2326

@@ -179,23 +182,23 @@ public function testFromDsnWithAccountAndEndpointOption()
179182
public function testFromDsnWithInvalidQueryString()
180183
{
181184
$this->expectException(\InvalidArgumentException::class);
182-
$this->expectExceptionMessage('Unknown option found in DSN: [foo]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
185+
$this->expectExceptionMessageMatches('|Unknown option found in DSN: \[foo\]\. Allowed options are \[buffer_size, |');
183186

184187
Connection::fromDsn('sqs://default?foo=foo');
185188
}
186189

187190
public function testFromDsnWithInvalidOption()
188191
{
189192
$this->expectException(\InvalidArgumentException::class);
190-
$this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
193+
$this->expectExceptionMessageMatches('|Unknown option found: \[bar\]\. Allowed options are \[buffer_size, |');
191194

192195
Connection::fromDsn('sqs://default', ['bar' => 'bar']);
193196
}
194197

195198
public function testFromDsnWithInvalidQueryStringAndOption()
196199
{
197200
$this->expectException(\InvalidArgumentException::class);
198-
$this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
201+
$this->expectExceptionMessageMatches('|Unknown option found: \[bar\]\. Allowed options are \[buffer_size, |');
199202

200203
Connection::fromDsn('sqs://default?foo=foo', ['bar' => 'bar']);
201204
}
@@ -312,4 +315,83 @@ public function testGetQueueUrlNotCalled()
312315

313316
$connection->delete('id');
314317
}
318+
319+
public function testLoggerWithoutDebugOption()
320+
{
321+
$client = new MockHttpClient([$this->getMockedQueueUrlResponse(), $this->getMockedReceiveMessageResponse()]);
322+
$logger = $this->getMockBuilder(NullLogger::class)
323+
->disableOriginalConstructor()
324+
->onlyMethods(['debug'])
325+
->getMock();
326+
$logger->expects($this->never())->method('debug');
327+
$connection = Connection::fromDsn('sqs://default', ['access_key' => 'foo', 'secret_key' => 'bar', 'auto_setup' => false], $client, $logger);
328+
$connection->get();
329+
}
330+
331+
public function testLoggerWithDebugOption()
332+
{
333+
$client = new MockHttpClient([$this->getMockedQueueUrlResponse(), $this->getMockedReceiveMessageResponse()]);
334+
$logger = $this->getMockBuilder(NullLogger::class)
335+
->disableOriginalConstructor()
336+
->onlyMethods(['debug'])
337+
->getMock();
338+
$logger->expects($this->exactly(4))->method('debug');
339+
$connection = Connection::fromDsn('sqs://default?debug=true', ['access_key' => 'foo', 'secret_key' => 'bar', 'auto_setup' => false], $client, $logger);
340+
$connection->get();
341+
}
342+
343+
private function getMockedQueueUrlResponse(): MockResponse
344+
{
345+
return new MockResponse(<<<XML
346+
<GetQueueUrlResponse>
347+
<GetQueueUrlResult>
348+
<QueueUrl>https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue</QueueUrl>
349+
</GetQueueUrlResult>
350+
<ResponseMetadata>
351+
<RequestId>470a6f13-2ed9-4181-ad8a-2fdea142988e</RequestId>
352+
</ResponseMetadata>
353+
</GetQueueUrlResponse>
354+
XML
355+
);
356+
}
357+
358+
private function getMockedReceiveMessageResponse(): MockResponse
359+
{
360+
return new MockResponse(<<<XML
361+
<ReceiveMessageResponse>
362+
<ReceiveMessageResult>
363+
<Message>
364+
<MessageId>5fea7756-0ea4-451a-a703-a558b933e274</MessageId>
365+
<ReceiptHandle>
366+
MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+Cw
367+
Lj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QE
368+
auMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=
369+
</ReceiptHandle>
370+
<MD5OfBody>fafb00f5732ab283681e124bf8747ed1</MD5OfBody>
371+
<Body>This is a test message</Body>
372+
<Attribute>
373+
<Name>SenderId</Name>
374+
<Value>195004372649</Value>
375+
</Attribute>
376+
<Attribute>
377+
<Name>SentTimestamp</Name>
378+
<Value>1238099229000</Value>
379+
</Attribute>
380+
<Attribute>
381+
<Name>ApproximateReceiveCount</Name>
382+
<Value>5</Value>
383+
</Attribute>
384+
<Attribute>
385+
<Name>ApproximateFirstReceiveTimestamp</Name>
386+
<Value>1250700979248</Value>
387+
</Attribute>
388+
</Message>
389+
</ReceiveMessageResult>
390+
<ResponseMetadata>
391+
<RequestId>b6633655-283d-45b4-aee4-4e84e0ae6afa</RequestId>
392+
</ResponseMetadata>
393+
</ReceiveMessageResponse>
394+
XML
395+
);
396+
}
315397
}

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransportFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
1516
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
1617
use Symfony\Component\Messenger\Transport\TransportInterface;
@@ -20,11 +21,18 @@
2021
*/
2122
class AmazonSqsTransportFactory implements TransportFactoryInterface
2223
{
24+
private $logger;
25+
26+
public function __construct(LoggerInterface $logger = null)
27+
{
28+
$this->logger = $logger;
29+
}
30+
2331
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
2432
{
2533
unset($options['transport_name']);
2634

27-
return new AmazonSqsTransport(Connection::fromDsn($dsn, $options), $serializer);
35+
return new AmazonSqsTransport(Connection::fromDsn($dsn, $options, null, $this->logger), $serializer);
2836
}
2937

3038
public function supports(string $dsn, array $options): bool

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use AsyncAws\Sqs\Result\ReceiveMessageResult;
1616
use AsyncAws\Sqs\SqsClient;
1717
use AsyncAws\Sqs\ValueObject\MessageAttributeValue;
18+
use Psr\Log\LoggerInterface;
1819
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1920
use Symfony\Component\Messenger\Exception\TransportException;
2021
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -45,6 +46,7 @@ class Connection
4546
'queue_name' => 'messages',
4647
'account' => null,
4748
'sslmode' => null,
49+
'debug' => null,
4850
];
4951

5052
private $configuration;
@@ -85,8 +87,9 @@ public function __destruct()
8587
* * poll_timeout: amount of seconds the transport should wait for new message
8688
* * visibility_timeout: amount of seconds the message won't be visible
8789
* * auto_setup: Whether the queue should be created automatically during send / get (Default: true)
90+
* * debug: Log all HTTP requests and responses as LoggerInterface::DEBUG (Default: false)
8891
*/
89-
public static function fromDsn(string $dsn, array $options = [], HttpClientInterface $client = null): self
92+
public static function fromDsn(string $dsn, array $options = [], HttpClientInterface $client = null, LoggerInterface $logger = null): self
9093
{
9194
if (false === $parsedUrl = parse_url($dsn)) {
9295
throw new InvalidArgumentException(sprintf('The given Amazon SQS DSN "%s" is invalid.', $dsn));
@@ -124,6 +127,9 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
124127
'accessKeyId' => urldecode($parsedUrl['user'] ?? '') ?: $options['access_key'] ?? self::DEFAULT_OPTIONS['access_key'],
125128
'accessKeySecret' => urldecode($parsedUrl['pass'] ?? '') ?: $options['secret_key'] ?? self::DEFAULT_OPTIONS['secret_key'],
126129
];
130+
if (isset($options['debug'])) {
131+
$clientConfiguration['debug'] = $options['debug'];
132+
}
127133
unset($query['region']);
128134

129135
if ('default' !== ($parsedUrl['host'] ?? 'default')) {
@@ -152,7 +158,7 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
152158
$queueUrl = 'https://'.$parsedUrl['host'].$parsedUrl['path'];
153159
}
154160

155-
return new self($configuration, new SqsClient($clientConfiguration, null, $client), $queueUrl);
161+
return new self($configuration, new SqsClient($clientConfiguration, null, $client, $logger), $queueUrl);
156162
}
157163

158164
public function get(): ?array

src/Symfony/Component/Messenger/Bridge/AmazonSqs/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20+
"async-aws/core": "^1.5",
2021
"async-aws/sqs": "^1.0",
2122
"symfony/messenger": "^4.3|^5.0",
22-
"symfony/service-contracts": "^1.1|^2"
23+
"symfony/service-contracts": "^1.1|^2",
24+
"psr/log": "^1.0"
2325
},
2426
"require-dev": {
2527
"symfony/http-client-contracts": "^1.0|^2.0",

0 commit comments

Comments
 (0)
0