Skip to content

Commit c31cf60

Browse files
authored
Fix flaky test and add more info in case of failure (#3650)
1 parent 2cb776a commit c31cf60

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

tests/ext/background-sender/agent_sampling-standalone-asm_01.phpt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,34 @@ $rr = new RequestReplayer();
2020

2121
$picked = 0;
2222
$notPicked = 0;
23-
$maxIterations = 5;
23+
$maxIterations = 15;
24+
$spanRecords = [];
2425
for ($i = 0; $i < $maxIterations; $i++)
2526
{
2627
//Do call and get sampling
2728
\DDTrace\start_span();
2829
\DDTrace\close_span();
2930
// Flush the span synchronously before reading from the request replayer
3031
dd_trace_internal_fn("synchronous_flush");
31-
$root = json_decode($rr->waitForDataAndReplay()["body"], true);
32-
$spans = $root["chunks"][0]["spans"] ?? $root[0];
32+
try {
33+
$request = $rr->waitForDataAndReplay();
34+
} catch (Exception $e) {
35+
// If no request yet, try next iteration
36+
continue;
37+
}
38+
$body = $request["body"] ?? null;
39+
if ($body === null) {
40+
continue;
41+
}
42+
$root = json_decode($body, true);
43+
if (!is_array($root)) {
44+
continue;
45+
}
46+
$spans = $root["chunks"][0]["spans"] ?? ($root[0] ?? null);
47+
$spanRecords[] = $spans;
48+
if (!is_array($spans) || !isset($spans[0]["metrics"]["_sampling_priority_v1"])) {
49+
continue;
50+
}
3351
$sampling = $spans[0]["metrics"]["_sampling_priority_v1"];
3452

3553
if ($sampling == 1) { // First pick this minute (or minute rollover)
@@ -54,6 +72,11 @@ for ($i = 0; $i < $maxIterations; $i++)
5472

5573
if ($picked == 1 && $notPicked == 2) {
5674
echo "All good" . PHP_EOL;
75+
} else {
76+
echo "Iterations: $i" . PHP_EOL;
77+
echo "Picked: $picked" . PHP_EOL;
78+
echo "notPicked: $notPicked" . PHP_EOL;
79+
echo var_dump($spanRecords);
5780
}
5881

5982
echo "Done" . PHP_EOL;

0 commit comments

Comments
 (0)