Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/Common/TracerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public function isolateLimitedTracer($fn, $tracer = null)
* This method executes a request into an ad-hoc web server configured with the provided envs and inis that is
* created and destroyed with the scope of this test.
*/
public function inWebServer($fn, $rootPath, $envs = [], $inis = [], &$curlInfo = null)
public function inWebServer($fn, $rootPath, $envs = [], $inis = [], $retryOnEmptyTraces = true, &$curlInfo = null)
{
$retries = 1;
$retries = $retryOnEmptyTraces ? 1 : 0;
do {
self::putEnv('DD_TRACE_SHUTDOWN_TIMEOUT=666666'); // Arbitrarily high value to avoid flakiness
self::putEnv('DD_TRACE_AGENT_RETRIES=3');
Expand Down
5 changes: 4 additions & 1 deletion tests/Composer/ComposerInteroperabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected function ddSetUp()

public function testComposerInteroperabilityWhenNoInitHook()
{
// Empty traces are the expected result, so they must not trigger the retry
// used by tests which expect the agent to receive a trace.
$traces = $this->inWebServer(
function ($execute) {
$output = $execute(GetSpec::create('default', '/'));
Expand All @@ -43,7 +45,8 @@ function ($execute) {
],
[
'datadog.trace.sources_path' => 'do_not_exists',
]
],
false
);

// Will fallback to installed sources, which does not include DDTrace, only api/, thus doing a Noop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function ($execute) use (&$response) {
'DD_TRACE_DEBUG' => 1,
],
[],
true,
$curlInfo
);

Expand Down
41 changes: 26 additions & 15 deletions tests/ext/background-sender/agent_sampling_sidecar.phpt
Comment thread
bwoebi marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ include __DIR__ . '/../includes/request_replayer.inc';
$contents = [];
$filename = null;

// Race conditions are annoying, especially with parallel test runs
// Wait until the sidecar has published this test invocation's response. Each
// invocation uses random markers so a repeated or parallel run cannot match a
// stale shared-memory file left by an earlier process.
function checkUpdated($marker) {
if (PHP_OS === "Linux") {
$retries = 100;
Expand All @@ -43,6 +45,7 @@ function checkUpdated($marker) {
foreach (glob("/dev/shm/*") as $f) {
var_dump($f, bin2hex(file_get_contents($f)));
}
throw new RuntimeException("Timed out waiting for sidecar sampling configuration marker: {$marker}");
}
}

Expand All @@ -56,37 +59,40 @@ function recordContents() {
$rr = new RequestReplayer();
$rr->replayRequest(); // cleanup possible leftover

$expected = [1,0,1];
$error = false;
$get_sampling = function() use ($rr, &$expected, &$error) {
$errors = [];
$get_sampling = function($label, $expected) use ($rr, &$errors) {
$root = json_decode($rr->waitForDataAndReplay()["body"], true);
$spans = $root["chunks"][0]["spans"] ?? $root[0];
$priority = $spans[0]["metrics"]["_sampling_priority_v1"];
if ($priority != array_shift($expected)) {
$error = true;
if ($priority != $expected) {
$errors[] = "{$label} sampling priority: expected {$expected}, got {$priority}";
}
return $priority;
};

$rr->setResponse(["rate_by_service" => ["service:,env:" => 0, "service:agent_sampling_sidecar_test,env:first" => 1]]);
$nonce = getmypid() . "-" . bin2hex(random_bytes(8));
$firstMarker = "service:agent-sampling-sidecar-sync-{$nonce},env:first";
$secondMarker = "service:agent-sampling-sidecar-sync-{$nonce},env:second";

$rr->setResponse(["rate_by_service" => ["service:,env:" => 0, $firstMarker => 1]]);

\DDTrace\start_span();
\DDTrace\close_span();

echo "Initial sampling: {$get_sampling()}\n";
echo "Initial sampling: {$get_sampling('Initial', 1)}\n";

checkUpdated("service:agent_sampling_sidecar_test,env:first");
checkUpdated($firstMarker);

$rr->setResponse(["rate_by_service" => ["service:,env:" => 0, "service:foo,env:none" => 1, "service:agent_sampling_sidecar_test,env:second" => 0]]);
$rr->setResponse(["rate_by_service" => ["service:,env:" => 0, "service:foo,env:none" => 1, $secondMarker => 0]]);

recordContents();
\DDTrace\start_span();
\DDTrace\close_span();
recordContents();

checkUpdated("service:agent_sampling_sidecar_test,env:second");
checkUpdated($secondMarker);

echo "Generic sampling: {$get_sampling()}\n";
echo "Generic sampling: {$get_sampling('Generic', 0)}\n";

// reset it for other tests
$rr->setResponse(["rate_by_service" => []]);
Expand All @@ -98,10 +104,15 @@ $s->env = "none";
\DDTrace\close_span();
recordContents();

echo "Specific sampling: {$get_sampling()}\n";
echo "Specific sampling: {$get_sampling('Specific', 1)}\n";

if ($error && PHP_OS === "Linux") {
var_dump($contents);
if ($errors) {
foreach ($errors as $error) {
echo "{$error}\n";
}
if (PHP_OS === "Linux") {
var_dump($contents);
}
}

?>
Expand Down
Loading