Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b9c271

Browse files
committed
Merge branch 'develop'
2 parents c5caac2 + 1bffee8 commit 3b9c271

37 files changed

+530
-119
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Unreleased
44

5+
- Log the bus name, receiver name and message class name as event tags when using Symfony Messenger (#492)
6+
- Make the transport factory configurable in the bundle's config (#504)
7+
- Add the `sentry_trace_meta()` Twig function to print the `sentry-trace` HTML meta tag (#510)
8+
- Make the list of commands for which distributed tracing is active configurable (#515)
9+
- Introduce `TracingDriverConnection::getWrappedConnection()` (#536)
10+
- Add the `logger` config option to ease setting a PSR-3 logger to debug the SDK (#538)
11+
- Bump requirement for DBAL tracing to `^2.13|^3`; simplify the DBAL tracing feature (#527)
12+
513
## 4.1.4 (2021-06-18)
614

715
- Fix decoration of cache adapters inheriting parent service (#525)

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"symfony/security-core": "^3.4.44||^4.4.20||^5.0.11"
3535
},
3636
"require-dev": {
37-
"doctrine/dbal": "^2.10||^3.0",
37+
"doctrine/dbal": "^2.13||^3.0",
3838
"doctrine/doctrine-bundle": "^1.12||^2.0",
3939
"friendsofphp/php-cs-fixer": "^2.18",
4040
"jangregor/phpstan-prophecy": "^0.8",
@@ -91,7 +91,7 @@
9191
},
9292
"extra": {
9393
"branch-alias": {
94-
"dev-master": "4.x-dev",
94+
"dev-master": "4.2.x-dev",
9595
"releases/3.2.x": "3.2.x-dev",
9696
"releases/2.x": "2.x-dev",
9797
"releases/1.x": "1.x-dev"

phpstan-baseline.neon

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ parameters:
5656
path: src/Tracing/Doctrine/DBAL/TracingDriver.php
5757

5858
-
59-
message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\Exception given\\.$#"
59+
message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\DriverException given\\.$#"
60+
count: 1
61+
path: src/Tracing/Doctrine/DBAL/TracingDriver.php
62+
63+
-
64+
message: "#^Parameter \\$exception of method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriver\\:\\:convertException\\(\\) has invalid typehint type Doctrine\\\\DBAL\\\\Driver\\\\DriverException\\.$#"
6065
count: 1
6166
path: src/Tracing/Doctrine/DBAL/TracingDriver.php
6267

@@ -115,6 +120,11 @@ parameters:
115120
count: 1
116121
path: src/aliases.php
117122

123+
-
124+
message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#"
125+
count: 1
126+
path: tests/End2End/TracingEnd2EndTest.php
127+
118128
-
119129
message: "#^Call to method getException\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
120130
count: 1
@@ -235,13 +245,23 @@ parameters:
235245
count: 1
236246
path: tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php
237247

248+
-
249+
message: "#^Class Doctrine\\\\DBAL\\\\Driver\\\\DriverException not found\\.$#"
250+
count: 3
251+
path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php
252+
238253
-
239254
message: "#^Parameter \\#1 \\$driverException of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Driver\\\\Exception, string given\\.$#"
240255
count: 1
241256
path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php
242257

243258
-
244-
message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\Exception&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
259+
message: "#^Parameter \\#1 \\$originalClassName of method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) expects class\\-string\\<Doctrine\\\\DBAL\\\\Driver\\\\DriverException\\>, string given\\.$#"
260+
count: 2
261+
path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php
262+
263+
-
264+
message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\DriverException&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#"
245265
count: 1
246266
path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php
247267

psalm-baseline.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.7.0@d4377c0baf3ffbf0b1ec6998e8d1be2a40971005">
2+
<files psalm-version="4.8.1@f73f2299dbc59a3e6c4d66cff4605176e728ee69">
33
<file src="src/EventListener/ConsoleCommandListener.php">
44
<InvalidExtendClass occurrences="1">
55
<code>ConsoleListener</code>
@@ -8,9 +8,4 @@
88
<code>public function __construct(HubInterface $hub, bool $captureErrors = true)</code>
99
</MethodSignatureMismatch>
1010
</file>
11-
<file src="src/Tracing/Cache/TraceableCacheAdapterTrait.php">
12-
<InvalidReturnType occurrences="1">
13-
<code>getItems</code>
14-
</InvalidReturnType>
15-
</file>
1611
</files>

src/DependencyInjection/Compiler/DbalTracingPass.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sentry\SentryBundle\DependencyInjection\Compiler;
66

77
use Doctrine\DBAL\Driver\ResultStatement;
8+
use Doctrine\DBAL\Result;
89
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\ConnectionConfigurator;
910
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware;
1011
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -39,6 +40,8 @@ public function process(ContainerBuilder $container): void
3940
return;
4041
}
4142

43+
$this->assertRequiredDbalVersion();
44+
4245
/** @var string[] $connectionsToTrace */
4346
$connectionsToTrace = $container->getParameter('sentry.tracing.dbal.connections');
4447

@@ -92,4 +95,19 @@ private function getSetMiddlewaresMethodCallArguments(Definition $definition): a
9295

9396
return [];
9497
}
98+
99+
private function assertRequiredDbalVersion(): void
100+
{
101+
if (interface_exists(Result::class)) {
102+
// DBAL ^2.13
103+
return;
104+
}
105+
106+
if (class_exists(Result::class)) {
107+
// DBAL ^3
108+
return;
109+
}
110+
111+
throw new \LogicException('Tracing support cannot be enabled as the Doctrine DBAL 2.13+ package is not installed. Try running "composer require doctrine/dbal:^2.13".');
112+
}
95113
}

src/DependencyInjection/Configuration.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Jean85\PrettyVersions;
99
use Sentry\Options;
1010
use Sentry\SentryBundle\ErrorTypesParser;
11+
use Sentry\Transport\TransportFactoryInterface;
1112
use Symfony\Bundle\TwigBundle\TwigBundle;
1213
use Symfony\Component\Cache\CacheItem;
1314
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
@@ -35,6 +36,14 @@ public function getConfigTreeBuilder(): TreeBuilder
3536
->info('If this value is not provided, the SDK will try to read it from the SENTRY_DSN environment variable. If that variable also does not exist, the SDK will just not send any events.')
3637
->end()
3738
->booleanNode('register_error_listener')->defaultTrue()->end()
39+
->scalarNode('logger')
40+
->info('The service ID of the PSR-3 logger used to log messages coming from the SDK client. Be aware that setting the same logger of the application may create a circular loop when an event fails to be sent.')
41+
->defaultNull()
42+
->end()
43+
->scalarNode('transport_factory')
44+
->info('The service ID of the transport factory used by the default SDK client.')
45+
->defaultValue(TransportFactoryInterface::class)
46+
->end()
3847
->arrayNode('options')
3948
->addDefaultsIfNotSet()
4049
->fixXmlConfig('integration')
@@ -167,6 +176,16 @@ private function addDistributedTracingSection(ArrayNodeDefinition $rootNode): vo
167176
->arrayNode('cache')
168177
->{class_exists(CacheItem::class) ? 'canBeDisabled' : 'canBeEnabled'}()
169178
->end()
179+
->arrayNode('console')
180+
->addDefaultsIfNotSet()
181+
->fixXmlConfig('excluded_command')
182+
->children()
183+
->arrayNode('excluded_commands')
184+
->scalarPrototype()->end()
185+
->defaultValue(['messenger:consume'])
186+
->end()
187+
->end()
188+
->end()
170189
->end()
171190
->end()
172191
->end();

src/DependencyInjection/SentryExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
88
use Jean85\PrettyVersions;
99
use LogicException;
10+
use Psr\Log\NullLogger;
1011
use Sentry\Client;
1112
use Sentry\ClientBuilder;
1213
use Sentry\Integration\IgnoreErrorsIntegration;
@@ -26,7 +27,6 @@
2627
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension;
2728
use Sentry\Serializer\RepresentationSerializer;
2829
use Sentry\Serializer\Serializer;
29-
use Sentry\Transport\TransportFactoryInterface;
3030
use Symfony\Bundle\TwigBundle\TwigBundle;
3131
use Symfony\Component\Cache\CacheItem;
3232
use Symfony\Component\Config\FileLocator;
@@ -128,9 +128,10 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
128128
->setArgument(0, new Reference('sentry.client.options'))
129129
->addMethodCall('setSdkIdentifier', [SentryBundle::SDK_IDENTIFIER])
130130
->addMethodCall('setSdkVersion', [PrettyVersions::getVersion('sentry/sentry-symfony')->getPrettyVersion()])
131-
->addMethodCall('setTransportFactory', [new Reference(TransportFactoryInterface::class)])
131+
->addMethodCall('setTransportFactory', [new Reference($config['transport_factory'])])
132132
->addMethodCall('setSerializer', [$serializer])
133-
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition]);
133+
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition])
134+
->addMethodCall('setLogger', [null !== $config['logger'] ? new Reference($config['logger']) : new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
134135

135136
$container
136137
->setDefinition('sentry.client', new Definition(Client::class))
@@ -175,7 +176,11 @@ private function registerTracingConfiguration(ContainerBuilder $container, array
175176
$container->removeDefinition(TracingRequestListener::class);
176177
$container->removeDefinition(TracingSubRequestListener::class);
177178
$container->removeDefinition(TracingConsoleListener::class);
179+
180+
return;
178181
}
182+
183+
$container->getDefinition(TracingConsoleListener::class)->replaceArgument(1, $config['console']['excluded_commands']);
179184
}
180185

181186
/**

src/EventListener/MessengerListener.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Sentry\SentryBundle\EventListener;
66

7+
use Sentry\Event;
78
use Sentry\State\HubInterface;
9+
use Sentry\State\Scope;
810
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
911
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
1012
use Symfony\Component\Messenger\Exception\HandlerFailedException;
13+
use Symfony\Component\Messenger\Stamp\BusNameStamp;
1114

1215
final class MessengerListener
1316
{
@@ -45,15 +48,28 @@ public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event):
4548
return;
4649
}
4750

48-
$error = $event->getThrowable();
51+
$this->hub->withScope(function (Scope $scope) use ($event): void {
52+
$envelope = $event->getEnvelope();
53+
$exception = $event->getThrowable();
4954

50-
if ($error instanceof HandlerFailedException) {
51-
foreach ($error->getNestedExceptions() as $nestedException) {
52-
$this->hub->captureException($nestedException);
55+
$scope->setTag('messenger.receiver_name', $event->getReceiverName());
56+
$scope->setTag('messenger.message_class', \get_class($envelope->getMessage()));
57+
58+
/** @var BusNameStamp|null $messageBusStamp */
59+
$messageBusStamp = $envelope->last(BusNameStamp::class);
60+
61+
if (null !== $messageBusStamp) {
62+
$scope->setTag('messenger.message_bus', $messageBusStamp->getBusName());
5363
}
54-
} else {
55-
$this->hub->captureException($error);
56-
}
64+
65+
if ($exception instanceof HandlerFailedException) {
66+
foreach ($exception->getNestedExceptions() as $nestedException) {
67+
$this->hub->captureException($nestedException);
68+
}
69+
} else {
70+
$this->hub->captureException($exception);
71+
}
72+
});
5773

5874
$this->flushClient();
5975
}

src/EventListener/TracingConsoleListener.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ final class TracingConsoleListener
2424
*/
2525
private $hub;
2626

27+
/**
28+
* @var string[] The list of commands for which distributed tracing must be skipped
29+
*/
30+
private $excludedCommands;
31+
2732
/**
2833
* Constructor.
2934
*
30-
* @param HubInterface $hub The current hub
35+
* @param HubInterface $hub The current hub
36+
* @param string[] $excludedCommands The list of commands for which distributed tracing must be skipped
3137
*/
32-
public function __construct(HubInterface $hub)
38+
public function __construct(HubInterface $hub, array $excludedCommands = [])
3339
{
3440
$this->hub = $hub;
41+
$this->excludedCommands = $excludedCommands;
3542
}
3643

3744
/**
@@ -42,18 +49,24 @@ public function __construct(HubInterface $hub)
4249
*/
4350
public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
4451
{
52+
$command = $event->getCommand();
53+
54+
if ($this->isCommandExcluded($command)) {
55+
return;
56+
}
57+
4558
$currentSpan = $this->hub->getSpan();
4659

4760
if (null === $currentSpan) {
4861
$transactionContext = new TransactionContext();
4962
$transactionContext->setOp('console.command');
50-
$transactionContext->setName($this->getSpanName($event->getCommand()));
63+
$transactionContext->setName($this->getSpanName($command));
5164

5265
$span = $this->hub->startTransaction($transactionContext);
5366
} else {
5467
$spanContext = new SpanContext();
5568
$spanContext->setOp('console.command');
56-
$spanContext->setDescription($this->getSpanName($event->getCommand()));
69+
$spanContext->setDescription($this->getSpanName($command));
5770

5871
$span = $currentSpan->startChild($spanContext);
5972
}
@@ -69,6 +82,10 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
6982
*/
7083
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
7184
{
85+
if ($this->isCommandExcluded($event->getCommand())) {
86+
return;
87+
}
88+
7289
$span = $this->hub->getSpan();
7390

7491
if (null !== $span) {
@@ -84,4 +101,13 @@ private function getSpanName(?Command $command): string
84101

85102
return $command->getName();
86103
}
104+
105+
private function isCommandExcluded(?Command $command): bool
106+
{
107+
if (null === $command) {
108+
return true;
109+
}
110+
111+
return \in_array($command->getName(), $this->excludedCommands, true);
112+
}
87113
}

src/Resources/config/schema/sentry-1.0.xsd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
</xsd:choice>
1616

1717
<xsd:attribute name="register-error-listener" type="xsd:boolean" />
18+
<xsd:attribute name="transport-factory" type="xsd:string" />
1819
<xsd:attribute name="dsn" type="xsd:string" />
20+
<xsd:attribute name="logger" type="xsd:string" />
1921
</xsd:complexType>
2022

2123
<xsd:complexType name="options">
@@ -86,6 +88,7 @@
8688
<xsd:element name="dbal" type="tracing-dbal" minOccurs="0" maxOccurs="1" />
8789
<xsd:element name="twig" type="tracing-twig" minOccurs="0" maxOccurs="1" />
8890
<xsd:element name="cache" type="tracing-cache" minOccurs="0" maxOccurs="1" />
91+
<xsd:element name="console" type="tracing-console" minOccurs="0" maxOccurs="1" />
8992
</xsd:choice>
9093

9194
<xsd:attribute name="enabled" type="xsd:boolean" default="true"/>
@@ -106,4 +109,10 @@
106109
<xsd:complexType name="tracing-cache">
107110
<xsd:attribute name="enabled" type="xsd:boolean" />
108111
</xsd:complexType>
112+
113+
<xsd:complexType name="tracing-console">
114+
<xsd:sequence maxOccurs="unbounded">
115+
<xsd:element name="excluded-command" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
116+
</xsd:sequence>
117+
</xsd:complexType>
109118
</xsd:schema>

src/Resources/config/services.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
<service id="Sentry\SentryBundle\EventListener\TracingConsoleListener" class="Sentry\SentryBundle\EventListener\TracingConsoleListener">
7474
<argument type="service" id="Sentry\State\HubInterface" />
75+
<argument /> <!-- $excludedCommands -->
7576

7677
<tag name="kernel.event_listener" event="console.command" method="handleConsoleCommandEvent" priority="118" />
7778
<tag name="kernel.event_listener" event="console.terminate" method="handleConsoleTerminateEvent" priority="-54" />
@@ -116,5 +117,11 @@
116117
<argument type="service" id="Symfony\Component\HttpFoundation\RequestStack" />
117118
<argument type="service" id="Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface" on-invalid="null" />
118119
</service>
120+
121+
<service id="Sentry\SentryBundle\Twig\SentryExtension" class="Sentry\SentryBundle\Twig\SentryExtension">
122+
<argument type="service" id="Sentry\State\HubInterface" />
123+
124+
<tag name="twig.extension" />
125+
</service>
119126
</services>
120127
</container>

0 commit comments

Comments
 (0)