Skip to content

Commit 0d86cb3

Browse files
PROFeNoMbwoebi
authored andcommitted
tests: Add testSilencedSpansAreDropped for Symfony CLI Tests
1 parent be86236 commit 0d86cb3

13 files changed

Lines changed: 528 additions & 4 deletions

src/DDTrace/Integrations/Symfony/SymfonyIntegration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ function (HookData $hook) use ($controllerName, $integration) {
578578
*/
579579
foreach (['Symfony\Component\Console\Terminal::hasSttyAvailable', 'Symfony\Component\Console\Helper\QuestionHelper::isInteractiveInput'] as $method) {
580580
\DDTrace\install_hook($method, function (HookData $hook) {
581+
$hook->data = false;
581582
\DDTrace\active_stack()->spanCreationObservers[] = function (SpanData $span) use ($hook) {
582583
if ($hook->data) {
583584
return false;

tests/Common/TracerTestTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ public function inWebServer($fn, $rootPath, $envs = [], $inis = [], &$curlInfo =
227227
/**
228228
* This method executes a single script with the provided configuration.
229229
*/
230-
public function inCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $until = null, $throw = true)
230+
public function inCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $until = null, $throw = true, $checkTty = false)
231231
{
232232
$this->resetRequestDumper();
233-
$output = $this->executeCli($scriptPath, $customEnvs, $customInis, $arguments, $withOutput);
233+
$output = $this->executeCli($scriptPath, $customEnvs, $customInis, $arguments, $withOutput, false, false, $checkTty);
234234
usleep(100000); // Add a slight delay to give the request-replayer time to handle and store all requests.
235235
$out = [$this->parseTracesFromDumpedData($until, $throw)];
236236
if ($withOutput) {
@@ -239,7 +239,7 @@ public function inCli($scriptPath, $customEnvs = [], $customInis = [], $argument
239239
return $out;
240240
}
241241

242-
public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $skipSyncFlush = false, $withExitCode = false)
242+
public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $skipSyncFlush = false, $withExitCode = false, $checkTty = false)
243243
{
244244
$envs = (string) new EnvSerializer(array_merge(
245245
[
@@ -277,7 +277,11 @@ public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arg
277277
} elseif (\is_array($arguments)) {
278278
$arguments = implode(' ', array_map('escapeshellarg', $arguments));
279279
}
280-
$commandToExecute = "$envs " . PHP_BINARY . " $inis $script $arguments";
280+
if ($checkTty && !posix_isatty(STDOUT)) {
281+
$commandToExecute = "script -q -c \"$envs " . PHP_BINARY . " $inis $script $arguments\" /dev/null";
282+
} else {
283+
$commandToExecute = "$envs " . PHP_BINARY . " $inis $script $arguments";
284+
}
281285
$output = [];
282286
$exitCode = 0;
283287
$createHook = \DDTrace\install_hook('DDTrace\Integrations\Exec\ExecIntegration::createSpan', function (HookData $hook) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Terminal;
10+
11+
#[AsCommand(
12+
name: 'app:stty'
13+
)]
14+
class SttyCommand extends Command
15+
{
16+
protected function execute(InputInterface $input, OutputInterface $output): int
17+
{
18+
$terminal = new Terminal();
19+
$terminal->hasSttyAvailable();
20+
21+
return Command::SUCCESS;
22+
}
23+
}

tests/Frameworks/Symfony/Version_4_4/src/Command/SttyCommand.php

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Terminal;
9+
10+
class SttyCommand extends Command
11+
{
12+
protected static $defaultName = 'app:stty';
13+
14+
protected function execute(InputInterface $input, OutputInterface $output): int
15+
{
16+
$terminal = new Terminal();
17+
$terminal->hasSttyAvailable();
18+
19+
return Command::SUCCESS;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Terminal;
9+
10+
class SttyCommand extends Command
11+
{
12+
protected static $defaultName = 'app:stty';
13+
14+
protected function execute(InputInterface $input, OutputInterface $output): int
15+
{
16+
$terminal = new Terminal();
17+
$terminal->hasSttyAvailable();
18+
19+
return Command::SUCCESS;
20+
}
21+
}

tests/Integrations/CLI/Symfony/V4_4/CommonScenariosTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,32 @@
88

99
class CommonScenariosTest extends IntegrationTestCase
1010
{
11+
const FIELDS_TO_IGNORE = [
12+
'metrics.php.compilation.total_time_ms',
13+
'metrics.php.memory.peak_usage_bytes',
14+
'metrics.php.memory.peak_real_usage_bytes',
15+
'meta.error.stack',
16+
'meta._dd.p.tid',
17+
'meta.cmd.exit_code',
18+
];
19+
1120
public static function getConsoleScript()
1221
{
1322
return __DIR__ . '/../../../../Frameworks/Symfony/Version_4_4/bin/console';
1423
}
1524

25+
public function testSilencedSpansAreDropped()
26+
{
27+
list($traces) = $this->inCli(static::getConsoleScript(), [
28+
'DD_TRACE_CLI_ENABLED' => 'true',
29+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
30+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
31+
'DD_TRACE_EXEC_ENABLED' => 'true',
32+
], [], 'app:stty', false, null, true, true);
33+
34+
$this->snapshotFromTraces($traces, self::FIELDS_TO_IGNORE);
35+
}
36+
1637
public function testThrowCommand()
1738
{
1839
list($traces) = $this->inCli(self::getConsoleScript(), [

tests/Integrations/CLI/Symfony/V5_2/CommonScenariosTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,32 @@
88

99
class CommonScenariosTest extends IntegrationTestCase
1010
{
11+
const FIELDS_TO_IGNORE = [
12+
'metrics.php.compilation.total_time_ms',
13+
'metrics.php.memory.peak_usage_bytes',
14+
'metrics.php.memory.peak_real_usage_bytes',
15+
'meta.error.stack',
16+
'meta._dd.p.tid',
17+
'meta.cmd.exit_code',
18+
];
19+
1120
public static function getConsoleScript()
1221
{
1322
return __DIR__ . '/../../../../Frameworks/Symfony/Version_5_2/bin/console';
1423
}
1524

25+
public function testSilencedSpansAreDropped()
26+
{
27+
list($traces) = $this->inCli(static::getConsoleScript(), [
28+
'DD_TRACE_CLI_ENABLED' => 'true',
29+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
30+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
31+
'DD_TRACE_EXEC_ENABLED' => 'true',
32+
], [], 'app:stty', false, null, true, true);
33+
34+
$this->snapshotFromTraces($traces, self::FIELDS_TO_IGNORE);
35+
}
36+
1637
public function testThrowCommand()
1738
{
1839
list($traces) = $this->inCli(self::getConsoleScript(), [

tests/Integrations/CLI/Symfony/V6_2/CommonScenariosTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88

99
class CommonScenariosTest extends IntegrationTestCase
1010
{
11+
const FIELDS_TO_IGNORE = [
12+
'metrics.php.compilation.total_time_ms',
13+
'metrics.php.memory.peak_usage_bytes',
14+
'metrics.php.memory.peak_real_usage_bytes',
15+
'meta.error.stack',
16+
'meta._dd.p.tid',
17+
'meta.cmd.exit_code',
18+
];
19+
1120
public static function getConsoleScript()
1221
{
1322
return __DIR__ . '/../../../../Frameworks/Symfony/Version_6_2/bin/console';
@@ -18,6 +27,18 @@ public static function getTestedLibrary()
1827
return 'symfony/console';
1928
}
2029

30+
public function testSilencedSpansAreDropped()
31+
{
32+
list($traces) = $this->inCli(static::getConsoleScript(), [
33+
'DD_TRACE_CLI_ENABLED' => 'true',
34+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
35+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
36+
'DD_TRACE_EXEC_ENABLED' => 'true',
37+
], [], 'app:stty', false, null, true, true);
38+
39+
$this->snapshotFromTraces($traces, self::FIELDS_TO_IGNORE);
40+
}
41+
2142
public function testThrowCommand()
2243
{
2344
list($traces) = $this->inCli(static::getConsoleScript(), [
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[[
2+
{
3+
"name": "console",
4+
"service": "console",
5+
"resource": "console",
6+
"trace_id": 0,
7+
"span_id": 1,
8+
"parent_id": 0,
9+
"type": "cli",
10+
"meta": {
11+
"_dd.p.dm": "0",
12+
"_dd.p.tid": "67dd820c00000000",
13+
"runtime-id": "70a85d5f-2b3f-45ee-8fa6-493f858df4d9"
14+
},
15+
"metrics": {
16+
"_dd.agent_psr": 1,
17+
"_sampling_priority_v1": 1
18+
}
19+
},
20+
{
21+
"name": "command_execution",
22+
"service": "console",
23+
"resource": "stty",
24+
"trace_id": 0,
25+
"span_id": 2,
26+
"parent_id": 1,
27+
"type": "system",
28+
"meta": {
29+
"cmd.exec": "[\"stty\",\"-a\"]",
30+
"cmd.exit_code": "0",
31+
"component": "subprocess"
32+
}
33+
},
34+
{
35+
"name": "symfony.httpkernel.kernel.boot",
36+
"service": "symfony",
37+
"resource": "App\\Kernel",
38+
"trace_id": 0,
39+
"span_id": 3,
40+
"parent_id": 1,
41+
"type": "web",
42+
"meta": {
43+
"_dd.base_service": "console",
44+
"component": "symfony"
45+
}
46+
},
47+
{
48+
"name": "command_execution",
49+
"service": "console",
50+
"resource": "sh",
51+
"trace_id": 0,
52+
"span_id": 4,
53+
"parent_id": 1,
54+
"type": "system",
55+
"meta": {
56+
"cmd.shell": "stty -g",
57+
"component": "subprocess"
58+
}
59+
},
60+
{
61+
"name": "symfony.console.command",
62+
"service": "symfony",
63+
"resource": "symfony.console.command",
64+
"trace_id": 0,
65+
"span_id": 5,
66+
"parent_id": 1,
67+
"type": "cli",
68+
"meta": {
69+
"_dd.base_service": "console",
70+
"component": "symfony"
71+
}
72+
},
73+
{
74+
"name": "symfony.console.command.run",
75+
"service": "symfony",
76+
"resource": "app:stty",
77+
"trace_id": 0,
78+
"span_id": 6,
79+
"parent_id": 1,
80+
"type": "cli",
81+
"meta": {
82+
"_dd.base_service": "console",
83+
"component": "symfony",
84+
"symfony.console.command.class": "App\\Command\\SttyCommand"
85+
}
86+
},
87+
{
88+
"name": "symfony.console.terminate",
89+
"service": "symfony",
90+
"resource": "symfony.console.terminate",
91+
"trace_id": 0,
92+
"span_id": 7,
93+
"parent_id": 1,
94+
"type": "cli",
95+
"meta": {
96+
"_dd.base_service": "console",
97+
"component": "symfony"
98+
}
99+
}]]

0 commit comments

Comments
 (0)