Skip to content

Commit a8a17bb

Browse files
committed
Make the list of commands for which distributed tracing is active configurable
1 parent 6576e11 commit a8a17bb

File tree

15 files changed

+148
-23
lines changed

15 files changed

+148
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Make the transport factory configurable in the bundle's config (#504)
66
- Add the `sentry_trace_meta()` Twig function to print the `sentry-trace` HTML meta tag (#510)
7+
- Make the list of commands for which distributed tracing is active configurable (#515)
78

89
## 4.1.0 (2021-04-19)
910

src/DependencyInjection/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ private function addDistributedTracingSection(ArrayNodeDefinition $rootNode): vo
172172
->arrayNode('cache')
173173
->{interface_exists(CacheInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
174174
->end()
175+
->arrayNode('console')
176+
->addDefaultsIfNotSet()
177+
->fixXmlConfig('excluded_command')
178+
->children()
179+
->arrayNode('excluded_commands')
180+
->scalarPrototype()->end()
181+
->defaultValue(['messenger:consume'])
182+
->end()
183+
->end()
184+
->end()
175185
->end()
176186
->end()
177187
->end();

src/DependencyInjection/SentryExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ private function registerTracingConfiguration(ContainerBuilder $container, array
174174
$container->removeDefinition(TracingRequestListener::class);
175175
$container->removeDefinition(TracingSubRequestListener::class);
176176
$container->removeDefinition(TracingConsoleListener::class);
177+
178+
return;
177179
}
180+
181+
$container->getDefinition(TracingConsoleListener::class)->replaceArgument(1, $config['console']['excluded_commands']);
178182
}
179183

180184
/**

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<xsd:element name="dbal" type="tracing-dbal" minOccurs="0" maxOccurs="1" />
8888
<xsd:element name="twig" type="tracing-twig" minOccurs="0" maxOccurs="1" />
8989
<xsd:element name="cache" type="tracing-cache" minOccurs="0" maxOccurs="1" />
90+
<xsd:element name="console" type="tracing-console" minOccurs="0" maxOccurs="1" />
9091
</xsd:choice>
9192

9293
<xsd:attribute name="enabled" type="xsd:boolean" default="true"/>
@@ -107,4 +108,10 @@
107108
<xsd:complexType name="tracing-cache">
108109
<xsd:attribute name="enabled" type="xsd:boolean" />
109110
</xsd:complexType>
111+
112+
<xsd:complexType name="tracing-console">
113+
<xsd:sequence maxOccurs="unbounded">
114+
<xsd:element name="excluded-command" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
115+
</xsd:sequence>
116+
</xsd:complexType>
110117
</xsd:schema>

src/Resources/config/services.xml

Lines changed: 1 addition & 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" />

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
5151
'cache' => [
5252
'enabled' => interface_exists(CacheInterface::class),
5353
],
54+
'console' => [
55+
'excluded_commands' => ['messenger:consume'],
56+
],
5457
],
5558
];
5659

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
7+
/** @var ContainerBuilder $container */
8+
$container->loadFromExtension('sentry', [
9+
'tracing' => [
10+
'console' => [
11+
'excluded_commands' => [
12+
'foo:bar',
13+
'bar:foo',
14+
],
15+
],
16+
],
17+
]);

tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@
5454
'cache' => [
5555
'enabled' => false,
5656
],
57+
'console' => [
58+
'excluded_commands' => ['app:command'],
59+
],
5760
],
5861
]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">
8+
9+
<sentry:config>
10+
<sentry:tracing>
11+
<sentry:console>
12+
<sentry:excluded-command>foo:bar</sentry:excluded-command>
13+
<sentry:excluded-command>bar:foo</sentry:excluded-command>
14+
</sentry:console>
15+
</sentry:tracing>
16+
</sentry:config>
17+
</container>

tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
</sentry:dbal>
4747
<sentry:twig enabled="false" />
4848
<sentry:cache enabled="false" />
49+
<sentry:console>
50+
<sentry:excluded-command>app:command</sentry:excluded-command>
51+
</sentry:console>
4952
</sentry:tracing>
5053
</sentry:config>
5154
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sentry:
2+
tracing:
3+
console:
4+
excluded_commands:
5+
- foo:bar
6+
- bar:foo

tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ sentry:
4747
enabled: false
4848
cache:
4949
enabled: false
50+
console:
51+
excluded_commands:
52+
- app:command

tests/DependencyInjection/SentryExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ public function testTwigTracingExtensionIsRemovedWhenTwigTracingIsDisabled(): vo
361361
$this->assertFalse($container->hasDefinition(TwigTracingExtension::class));
362362
}
363363

364+
public function testConsoleTracingListenerIsConfiguredWhenTracingIsEnabled(): void
365+
{
366+
$container = $this->createContainerFromFixture('console_tracing_enabled');
367+
368+
$this->assertTrue($container->hasDefinition(TracingConsoleListener::class));
369+
$this->assertSame(['foo:bar', 'bar:foo'], $container->getDefinition(TracingConsoleListener::class)->getArgument(1));
370+
}
371+
364372
private function createContainerFromFixture(string $fixtureFile): ContainerBuilder
365373
{
366374
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([

tests/EventListener/TracingConsoleListenerTest.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ final class TracingConsoleListenerTest extends TestCase
2525
*/
2626
private $hub;
2727

28-
/**
29-
* @var TracingConsoleListener
30-
*/
31-
private $listener;
32-
3328
protected function setUp(): void
3429
{
3530
$this->hub = $this->createMock(HubInterface::class);
36-
$this->listener = new TracingConsoleListener($this->hub);
3731
}
3832

3933
/**
@@ -61,7 +55,8 @@ public function testHandleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHub
6155
->with($transaction)
6256
->willReturnSelf();
6357

64-
$this->listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
58+
$listener = new TracingConsoleListener($this->hub);
59+
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
6560
$command,
6661
$this->createMock(InputInterface::class),
6762
$this->createMock(OutputInterface::class)
@@ -77,15 +72,6 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
7772
$transactionContext->setOp('console.command');
7873
$transactionContext->setName('<unnamed command>');
7974

80-
yield [
81-
null,
82-
$transactionContext,
83-
];
84-
85-
$transactionContext = new TransactionContext();
86-
$transactionContext->setOp('console.command');
87-
$transactionContext->setName('<unnamed command>');
88-
8975
yield [
9076
new Command(),
9177
$transactionContext,
@@ -123,7 +109,8 @@ public function testHandleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHub(?Co
123109
}))
124110
->willReturnSelf();
125111

126-
$this->listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
112+
$listener = new TracingConsoleListener($this->hub);
113+
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
127114
$command,
128115
$this->createMock(InputInterface::class),
129116
$this->createMock(OutputInterface::class)
@@ -151,6 +138,19 @@ public function handleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHubDataProv
151138
];
152139
}
153140

141+
public function testHandleConsoleCommandEvent(): void
142+
{
143+
$this->hub->expects($this->never())
144+
->method('getSpan');
145+
146+
$listener = new TracingConsoleListener($this->hub, ['foo:bar']);
147+
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
148+
new Command('foo:bar'),
149+
$this->createMock(InputInterface::class),
150+
$this->createMock(OutputInterface::class)
151+
));
152+
}
153+
154154
public function testHandleConsoleTerminateEvent(): void
155155
{
156156
$span = new Span();
@@ -159,7 +159,8 @@ public function testHandleConsoleTerminateEvent(): void
159159
->method('getSpan')
160160
->willReturn($span);
161161

162-
$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
162+
$listener = new TracingConsoleListener($this->hub);
163+
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
163164
new Command(),
164165
$this->createMock(InputInterface::class),
165166
$this->createMock(OutputInterface::class),
@@ -175,11 +176,26 @@ public function testHandleConsoleTerminateEventDoesNothingIfNoSpanIsSetOnHub():
175176
->method('getSpan')
176177
->willReturn(null);
177178

178-
$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
179+
$listener = new TracingConsoleListener($this->hub);
180+
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
179181
new Command(),
180182
$this->createMock(InputInterface::class),
181183
$this->createMock(OutputInterface::class),
182184
0
183185
));
184186
}
187+
188+
public function testHandleConsoleTerminateEventDoesNothingIfCommandIsExcluded(): void
189+
{
190+
$this->hub->expects($this->never())
191+
->method('getSpan');
192+
193+
$listener = new TracingConsoleListener($this->hub, ['foo:bar']);
194+
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
195+
new Command('foo:bar'),
196+
$this->createMock(InputInterface::class),
197+
$this->createMock(OutputInterface::class),
198+
0
199+
));
200+
}
185201
}

0 commit comments

Comments
 (0)