Skip to content

Commit 08b033e

Browse files
authored
fix: Symfony Messenger propagation to non-instrumented clients (#2956)
* Try using SerializerStamp * tests: Update Symfony v6.2 snapshots * tests: Update Symfony v7.0 snapshots * tests: Update Symfony v5.2 snapshots * tests: Update Symfony v4.4 snapshots * style: Remove debug params
1 parent b9cdb0a commit 08b033e

File tree

29 files changed

+1850
-243
lines changed

29 files changed

+1850
-243
lines changed

src/DDTrace/Integrations/SymfonyMessenger/DDTraceStamp.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/DDTrace/Integrations/SymfonyMessenger/SymfonyMessengerIntegration.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1919
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
2020
use Symfony\Component\Messenger\Stamp\SentStamp;
21+
use Symfony\Component\Messenger\Stamp\SerializerStamp;
2122
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
2223
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
2324
use function DDTrace\hook_method;
@@ -58,15 +59,11 @@ function (HookData $hook) {
5859
$envelope = $hook->args[0];
5960

6061
if (\ddtrace_config_distributed_tracing_enabled()) {
61-
$ddTraceStamp = $envelope->last(DDTraceStamp::class);
62-
63-
// Add distributed tracing stamp only if not already on the envelope
64-
if ($ddTraceStamp === null) {
65-
$tracingHeaders = \DDTrace\generate_distributed_tracing_headers();
66-
$hook->overrideArguments([
67-
$envelope->with(new DDTraceStamp($tracingHeaders))
68-
]);
69-
}
62+
$tracingHeaders = \DDTrace\generate_distributed_tracing_headers();
63+
$datadogHeaders = ['datadog' => $tracingHeaders];
64+
$hook->overrideArguments([
65+
$envelope->with(new SerializerStamp($datadogHeaders))
66+
]);
7067
}
7168
}
7269
);
@@ -101,13 +98,17 @@ function (SpanData $span, array $args, $envelope) use ($integration) {
10198
'receive'
10299
);
103100

104-
$ddTraceStamp = $envelope->last(DDTraceStamp::class);
105-
if ($ddTraceStamp instanceof DDTraceStamp) {
106-
$tracingHeaders = $ddTraceStamp->getHeaders();
107-
if (\dd_trace_env_config('DD_TRACE_SYMFONY_MESSENGER_DISTRIBUTED_TRACING')) {
108-
\DDTrace\consume_distributed_tracing_headers($tracingHeaders);
109-
} else {
110-
$span->links[] = \DDTrace\SpanLink::fromHeaders($tracingHeaders);
101+
$serializerStamps = $envelope->all(SerializerStamp::class);
102+
foreach ($serializerStamps as $serializerStamp) {
103+
$context = $serializerStamp->getContext();
104+
if (isset($context['datadog'])) {
105+
$tracingHeaders = $context['datadog'];
106+
if (\dd_trace_env_config('DD_TRACE_SYMFONY_MESSENGER_DISTRIBUTED_TRACING')) {
107+
\DDTrace\consume_distributed_tracing_headers($tracingHeaders);
108+
} else {
109+
$span->links[] = \DDTrace\SpanLink::fromHeaders($tracingHeaders);
110+
}
111+
break;
111112
}
112113
}
113114
},

tests/Common/TracerTestTrait.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function inCli($scriptPath, $customEnvs = [], $customInis = [], $argument
228228
return $out;
229229
}
230230

231-
public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $skipSyncFlush = false)
231+
public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arguments = '', $withOutput = false, $skipSyncFlush = false, $withExitCode = false)
232232
{
233233
$envs = (string) new EnvSerializer(array_merge(
234234
[
@@ -267,16 +267,14 @@ public function executeCli($scriptPath, $customEnvs = [], $customInis = [], $arg
267267
$arguments = implode(' ', array_map('escapeshellarg', $arguments));
268268
}
269269
$commandToExecute = "$envs " . PHP_BINARY . " $inis $script $arguments";
270-
if ($withOutput) {
271-
$ret = (string) `$commandToExecute 2>&1`;
272-
} else {
273-
`$commandToExecute`;
274-
$ret = null;
275-
}
270+
$output = [];
271+
$exitCode = 0;
272+
exec($commandToExecute . ' 2>&1', $output, $exitCode);
273+
$ret = $withOutput ? implode("\n", $output) : null;
276274
if (!$skipSyncFlush && \dd_trace_env_config("DD_TRACE_SIDECAR_TRACE_SENDER")) {
277275
\dd_trace_synchronous_flush();
278276
}
279-
return $ret;
277+
return $withExitCode ? [$ret, $exitCode] : $ret;
280278
}
281279

282280
/**

tests/Frameworks/Symfony/Version_7_0/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"doctrine/doctrine-bundle": "^2.11",
1212
"doctrine/doctrine-migrations-bundle": "^3.3",
1313
"doctrine/orm": "^2.17",
14+
"phpdocumentor/reflection-docblock": "^5.6",
15+
"phpstan/phpdoc-parser": "^1.0",
1416
"symfony/console": "7.0.*",
1517
"symfony/doctrine-messenger": "^7.1",
1618
"symfony/dotenv": "7.0.*",
@@ -19,8 +21,11 @@
1921
"symfony/framework-bundle": "7.0.*",
2022
"symfony/messenger": "^7.1",
2123
"symfony/monolog-bundle": "^3.10",
24+
"symfony/property-access": "7.0.*",
25+
"symfony/property-info": "7.0.*",
2226
"symfony/runtime": "7.0.*",
2327
"symfony/security-bundle": "7.0.*",
28+
"symfony/serializer": "7.0.*",
2429
"symfony/twig-bundle": "7.0.*",
2530
"symfony/validator": "7.0.*",
2631
"symfony/yaml": "7.0.*",

tests/Frameworks/Symfony/Version_7_0/config/packages/messenger.yaml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
framework:
2-
messenger:
3-
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
4-
failure_transport: failed
2+
messenger:
3+
serializer:
4+
default_serializer: messenger.transport.symfony_serializer
5+
symfony_serializer:
6+
format: json
7+
context: { }
8+
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
9+
failure_transport: failed
510

6-
transports:
7-
# https://symfony.com/doc/current/messenger.html#transport-configuration
8-
async:
9-
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
10-
options:
11-
queue_name: high
12-
retry_strategy:
13-
max_retries: 0
14-
failed: 'doctrine://default?queue_name=failed'
15-
sync: 'sync://'
11+
transports:
12+
# https://symfony.com/doc/current/messenger.html#transport-configuration
13+
async:
14+
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
15+
options:
16+
queue_name: high
17+
retry_strategy:
18+
max_retries: 0
19+
failed: 'doctrine://default?queue_name=failed'
20+
sync: 'sync://'
1621

17-
routing:
18-
# Route your messages to the transports
19-
'App\Message\LuckyNumberNotification': async
22+
routing:
23+
# Route your messages to the transports
24+
'App\Message\LuckyNumberNotification': async
2025

2126

2227
# when@test:

tests/Integrations/Symfony/V4_4/MessengerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected static function getEnvs()
3737
'DD_SERVICE' => 'symfony_messenger_test',
3838
'DD_TRACE_DEBUG' => 'true',
3939
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
40+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
4041
'DD_TRACE_PHPREDIS_ENABLED' => 'false' // We are NOT testing the phpredis integration
4142
]);
4243
}
@@ -55,6 +56,7 @@ public function testAsyncSuccess()
5556
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
5657
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
5758
'DD_TRACE_DEBUG' => 'true',
59+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
5860
], [], ['messenger:consume', 'async', '--limit=1']);
5961

6062
$this->snapshotFromTraces(
@@ -78,6 +80,7 @@ public function testAsyncFailure()
7880
'DD_SERVICE' => 'symfony_messenger_test',
7981
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
8082
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
83+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
8184
], [], ['messenger:consume', 'async', '--limit=1']);
8285

8386
$this->snapshotFromTraces(
@@ -87,4 +90,26 @@ public function testAsyncFailure()
8790
true
8891
);
8992
}
93+
94+
public function testAsyncWithTracerDisabledOnConsume()
95+
{
96+
// GH Issue: https://github.com/DataDog/dd-trace-php/pull/2749#issuecomment-2467409884
97+
98+
$this->tracesFromWebRequestSnapshot(function () {
99+
$spec = GetSpec::create('Lucky number', '/lucky/number');
100+
$this->call($spec);
101+
}, self::FIELDS_TO_IGNORE);
102+
103+
list($output, $exitCode) = $this->executeCli(
104+
self::getConsoleScript(),
105+
[],
106+
['ddtrace.disable' => 'true'],
107+
['messenger:consume', 'async', '--limit=1'],
108+
true,
109+
true,
110+
true
111+
);
112+
113+
$this->assertEquals(0, $exitCode, "Command failed with exit code 1. Output: $output");
114+
}
90115
}

tests/Integrations/Symfony/V5_2/MessengerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected static function getEnvs()
3737
'DD_SERVICE' => 'symfony_messenger_test',
3838
'DD_TRACE_DEBUG' => 'true',
3939
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
40+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
4041
'DD_TRACE_PHPREDIS_ENABLED' => 'false' // We are NOT testing the phpredis integration
4142
]);
4243
}
@@ -55,6 +56,7 @@ public function testAsyncSuccess()
5556
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
5657
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
5758
'DD_TRACE_DEBUG' => 'true',
59+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
5860
], [], ['messenger:consume', 'async', '--limit=1']);
5961

6062
$this->snapshotFromTraces(
@@ -78,6 +80,7 @@ public function testAsyncFailure()
7880
'DD_SERVICE' => 'symfony_messenger_test',
7981
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
8082
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
83+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
8184
], [], ['messenger:consume', 'async', '--limit=1']);
8285

8386

@@ -88,4 +91,26 @@ public function testAsyncFailure()
8891
true
8992
);
9093
}
94+
95+
public function testAsyncWithTracerDisabledOnConsume()
96+
{
97+
// GH Issue: https://github.com/DataDog/dd-trace-php/pull/2749#issuecomment-2467409884
98+
99+
$this->tracesFromWebRequestSnapshot(function () {
100+
$spec = GetSpec::create('Lucky number', '/lucky/number');
101+
$this->call($spec);
102+
}, self::FIELDS_TO_IGNORE);
103+
104+
list($output, $exitCode) = $this->executeCli(
105+
self::getConsoleScript(),
106+
[],
107+
['ddtrace.disable' => 'true'],
108+
['messenger:consume', 'async', '--limit=1'],
109+
true,
110+
true,
111+
true
112+
);
113+
114+
$this->assertEquals(0, $exitCode, "Command failed with exit code 1. Output: $output");
115+
}
91116
}

tests/Integrations/Symfony/V6_2/MessengerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected static function getEnvs()
3737
'DD_SERVICE' => 'symfony_messenger_test',
3838
'DD_TRACE_DEBUG' => 'true',
3939
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
40+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
4041
'DD_TRACE_PHPREDIS_ENABLED' => 'false' // We are NOT testing the phpredis integration
4142
]);
4243
}
@@ -55,6 +56,7 @@ public function testAsyncSuccess()
5556
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
5657
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
5758
'DD_TRACE_DEBUG' => 'true',
59+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
5860
'DD_TRACE_PHPREDIS_ENABLED' => 'false' // We are NOT testing the phpredis integration
5961
], [], ['mess:cons', 'async', '--limit=1']);
6062

@@ -79,6 +81,7 @@ public function testAsyncFailure()
7981
'DD_SERVICE' => 'symfony_messenger_test',
8082
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
8183
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
84+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
8285
'DD_TRACE_PHPREDIS_ENABLED' => 'false' // We are NOT testing the phpredis integration
8386
], [], ['messenger:consume', 'async', '--limit=1']);
8487

@@ -89,4 +92,26 @@ public function testAsyncFailure()
8992
true
9093
);
9194
}
95+
96+
public function testAsyncWithTracerDisabledOnConsume()
97+
{
98+
// GH Issue: https://github.com/DataDog/dd-trace-php/pull/2749#issuecomment-2467409884
99+
100+
$this->tracesFromWebRequestSnapshot(function () {
101+
$spec = GetSpec::create('Lucky number', '/lucky/number');
102+
$this->call($spec);
103+
}, self::FIELDS_TO_IGNORE);
104+
105+
list($output, $exitCode) = $this->executeCli(
106+
self::getConsoleScript(),
107+
[],
108+
['ddtrace.disable' => 'true'],
109+
['messenger:consume', 'async', '--limit=1'],
110+
true,
111+
true,
112+
true
113+
);
114+
115+
$this->assertEquals(0, $exitCode, "Command failed with exit code 1. Output: $output");
116+
}
92117
}

tests/Integrations/Symfony/V7_0/MessengerTest.php

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

99
class MessengerTest extends WebFrameworkTestCase
1010
{
11+
public static $database = "symfony70";
12+
1113
const FIELDS_TO_IGNORE = [
1214
'metrics.php.compilation.total_time_ms',
1315
'metrics.php.memory.peak_usage_bytes',
@@ -37,6 +39,7 @@ protected static function getEnvs()
3739
'DD_SERVICE' => 'symfony_messenger_test',
3840
'DD_TRACE_DEBUG' => 'true',
3941
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
42+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
4043
]);
4144
}
4245

@@ -54,6 +57,7 @@ public function testAsyncSuccess()
5457
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
5558
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
5659
'DD_TRACE_DEBUG' => 'true',
60+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
5761
], [], ['messenger:consume', 'async', '--limit=1']);
5862

5963
$this->snapshotFromTraces(
@@ -77,6 +81,7 @@ public function testAsyncFailure()
7781
'DD_SERVICE' => 'symfony_messenger_test',
7882
'DD_TRACE_REMOVE_AUTOINSTRUMENTATION_ORPHANS' => 'true',
7983
'DD_TRACE_SYMFONY_MESSENGER_MIDDLEWARES' => 'true',
84+
'DD_INSTRUMENTATION_TELEMETRY_ENABLED' => 'false',
8085
], [], ['messenger:consume', 'async', '--limit=1']);
8186

8287
$this->snapshotFromTraces(
@@ -86,4 +91,26 @@ public function testAsyncFailure()
8691
true
8792
);
8893
}
94+
95+
public function testAsyncWithTracerDisabledOnConsume()
96+
{
97+
// GH Issue: https://github.com/DataDog/dd-trace-php/pull/2749#issuecomment-2467409884
98+
99+
$this->tracesFromWebRequestSnapshot(function () {
100+
$spec = GetSpec::create('Lucky number', '/lucky/number');
101+
$this->call($spec);
102+
}, self::FIELDS_TO_IGNORE);
103+
104+
list($output, $exitCode) = $this->executeCli(
105+
self::getConsoleScript(),
106+
[],
107+
['ddtrace.disable' => 'true'],
108+
['messenger:consume', 'async', '--limit=1'],
109+
true,
110+
true,
111+
true
112+
);
113+
114+
$this->assertEquals(0, $exitCode, "Command failed with exit code 1. Output: $output");
115+
}
89116
}

0 commit comments

Comments
 (0)