Skip to content

Commit bcb0c30

Browse files
authored
Improve tests stability (#2830)
* Remove flakiness from the crashtracker_segfault.phpt test * Add a sligh delay to avoid race condition in tests using metrics * Add a delay to reduce flakiness on pnctl tests * Make the test sets datadog.trace.agent_test_session_token
1 parent 201810f commit bcb0c30

7 files changed

Lines changed: 86 additions & 51 deletions

File tree

ext/sidecar.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ static uint8_t dd_sidecar_formatted_session_id[36];
2323
static void ddtrace_set_non_resettable_sidecar_globals(void) {
2424
ddtrace_format_runtime_id(&dd_sidecar_formatted_session_id);
2525
ddtrace_endpoint = ddtrace_sidecar_agent_endpoint();
26-
27-
if (ZSTR_LEN(get_global_DD_TRACE_AGENT_TEST_SESSION_TOKEN())) {
28-
ddog_endpoint_set_test_token(ddtrace_endpoint, dd_zend_string_to_CharSlice(get_global_DD_TRACE_AGENT_TEST_SESSION_TOKEN()));
29-
}
3026
}
3127

3228
// Set the globals that must be updated in case of fork
@@ -180,6 +176,10 @@ ddog_Endpoint *ddtrace_sidecar_agent_endpoint(void) {
180176
free(agent_url);
181177
}
182178

179+
if (ZSTR_LEN(get_global_DD_TRACE_AGENT_TEST_SESSION_TOKEN())) {
180+
ddog_endpoint_set_test_token(agent_endpoint, dd_zend_string_to_CharSlice(get_global_DD_TRACE_AGENT_TEST_SESSION_TOKEN()));
181+
}
182+
183183
return agent_endpoint;
184184
}
185185

tests/Common/SnapshotTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ public function isolateTracerSnapshot(
344344
$fn($tracer);
345345

346346
if ($snapshotMetrics) {
347+
usleep(50000); // Add a slight delay to avoid a race condition where the "tracer-snapshot-end" metric is handled before a test metric.
347348
\DDTrace\dogstatsd_count("tracer-snapshot-end", 1);
348349
}
349350

tests/Common/TracerTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public function inCli($scriptPath, $customEnvs = [], $customInis = [], $argument
220220
{
221221
$this->resetRequestDumper();
222222
$output = $this->executeCli($scriptPath, $customEnvs, $customInis, $arguments, $withOutput);
223+
usleep(100000); // Add a slight delay to give the request-replayer time to handle and store all requests.
223224
$out = [$this->parseTracesFromDumpedData()];
224225
if ($withOutput) {
225226
$out[] = $output;

tests/Integrations/OpenAI/OpenAITest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private function checkErrors()
2929

3030
protected function ddSetUp()
3131
{
32-
// Note: Remember that DD_DOGSTATSD_URL=http://127.0.0.1:9876 is set in the Makefile call
32+
// Note: Remember that DD_DOGSTATSD_URL=http://request-replayer:80 is set in the Makefile call
3333
ini_set("log_errors", 1);
3434
ini_set("error_log", __DIR__ . "/openai.log");
3535
self::putEnvAndReloadConfig([

tests/ext/crashtracker_segfault.phpt

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,71 @@ if (getenv('SKIP_ASAN') || getenv('USE_ZEND_ALLOC') === '0') die("skip: intentio
77
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support %A in EXPECTF");
88
if (getenv('DD_TRACE_CLI_ENABLED') === '0') die("skip: tracer is disabled");
99
if (PHP_VERSION_ID < 70200) die("skip: TEST_PHP_EXTRA_ARGS is only available on PHP 7.2+");
10+
include __DIR__ . '/includes/skipif_no_dev_env.inc';
1011
?>
1112
--ENV--
12-
DD_TRACE_SIDECAR_TRACE_SENDER=1
13+
DD_TRACE_LOG_LEVEL=0
14+
DD_AGENT_HOST=request-replayer
15+
DD_TRACE_AGENT_PORT=80
1316
--INI--
14-
datadog.trace.agent_url="file://{PWD}/crashtracker_segfault_agent.out"
17+
datadog.trace.agent_test_session_token=tests/ext/crashtracker_segfault.phpt
1518
--FILE--
1619
<?php
1720

21+
include __DIR__ . '/includes/request_replayer.inc';
22+
$rr = new RequestReplayer();
23+
$rr->replayRequest(); // cleanup possible leftover
24+
1825
usleep(100000); // Let time to the sidecar to open the crashtracker socket
1926

2027
$php = getenv('TEST_PHP_EXECUTABLE');
2128
$args = getenv('TEST_PHP_ARGS')." ".getenv("TEST_PHP_EXTRA_ARGS");
2229
$cmd = $php." ".$args." -r 'posix_kill(posix_getpid(), 11);'";
2330
system($cmd);
2431

25-
for ($i = 0; $i < 100; ++$i) {
26-
$content = file_get_contents(__DIR__."/crashtracker_segfault_agent.out");
27-
if (false != strpos($content, '"signame": "SIGSEGV"')) {
28-
echo $content;
29-
break;
32+
$rr->waitForRequest(function ($request) {
33+
if ($request["uri"] != "/telemetry/proxy/api/v2/apmtelemetry") {
34+
return false;
35+
}
36+
$body = json_decode($request["body"], true);
37+
if ($body["request_type"] != "logs" || !isset($body["payload"][0]["message"])) {
38+
return false;
3039
}
31-
usleep(5000); // Let time for the crash report to be uploaded
32-
}
3340

41+
$payload = $body["payload"][0];
42+
$payload["message"] = json_decode($payload["message"], true);
43+
$output = json_encode($payload, JSON_PRETTY_PRINT);
44+
45+
echo $output;
46+
47+
return true;
48+
});
3449
?>
3550
--EXPECTF--
51+
%A{
52+
"message": {
53+
"additional_stacktraces": [],
54+
"files": {
3655
%A
37-
"counters": {
38-
%A
39-
},
40-
"files": {
41-
"/proc/self/maps": [
56+
},
57+
"metadata": {
58+
"library_name": "dd-trace-php",
59+
"library_version": "%s",
60+
"family": "php",
61+
"tags": [
4262
%A
43-
]
44-
},
45-
"incomplete": %s,
46-
"metadata": {
47-
"library_name": "dd-trace-php",
48-
"library_version": "%s",
49-
"family": "php",
50-
"tags": [
63+
]
64+
},
65+
"os_info": {
5166
%A
52-
]
53-
},
54-
"os_info": {
55-
%A
56-
},
57-
"proc_info": {
58-
"pid": %d
59-
},
60-
"siginfo": {
61-
"signum": 11,
62-
"signame": "SIGSEGV"
63-
},
64-
"timestamp": "%s",
65-
"uuid": "%s"
66-
%A
67-
--CLEAN--
68-
<?php
69-
70-
@unlink(__DIR__ . '/crashtracker_segfault_agent.out');
67+
},
68+
"span_ids": [],
69+
"tags": [],
70+
"trace_ids": []
71+
},
72+
"level": "ERROR",
73+
"count": 1,
74+
"stack_trace": "%s",
75+
"tags": "%ssigname:SIGSEGV%s",
76+
"is_sensitive": true
77+
}%A

tests/ext/includes/request_replayer.inc

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ class RequestReplayer
3030
usleep($this->flushInterval * 2 * 1000);
3131
}
3232

33+
public function waitForRequest($matcher)
34+
{
35+
$i = 0;
36+
do {
37+
if ($i++ == 100) {
38+
throw new Exception("wait for replay timeout");
39+
}
40+
usleep($this->flushInterval * 1000);
41+
42+
$requests = $this->replayAllRequests();
43+
if (is_array($requests)) {
44+
foreach ($requests as $request) {
45+
if ($matcher($request)) {
46+
return $request;
47+
}
48+
}
49+
}
50+
} while (true);
51+
}
52+
3353
public function waitForDataAndReplay($ignoreTelemetry = true)
3454
{
3555
$i = 0;
@@ -46,17 +66,22 @@ class RequestReplayer
4666
{
4767
// Request replayer now returns as many requests as were sent during a session.
4868
// For the scope of the tests, we are returning the very first one.
49-
$allRequests = json_decode(file_get_contents($this->endpoint . '/replay', false, stream_context_create([
50-
"http" => [
51-
"header" => "X-Datadog-Test-Session-Token: " . ini_get("datadog.trace.agent_test_session_token"),
52-
],
53-
])), true);
69+
$allRequests = $this->replayAllRequests();
5470
if ($allRequests && $ignoreTelemetry) {
5571
$allRequests = array_values(array_filter($allRequests, function ($v) { return $v["uri"] != '/telemetry/proxy/api/v2/apmtelemetry'; }));
5672
}
5773
return $allRequests ? $allRequests[0] : [];
5874
}
5975

76+
public function replayAllRequests()
77+
{
78+
return json_decode(file_get_contents($this->endpoint . '/replay', false, stream_context_create([
79+
"http" => [
80+
"header" => "X-Datadog-Test-Session-Token: " . ini_get("datadog.trace.agent_test_session_token"),
81+
],
82+
])), true);
83+
}
84+
6085
public function replayHeaders($showOnly = [])
6186
{
6287
$request = $this->waitForDataAndReplay();

tests/ext/request-replayer/dd_trace_exception_span_event.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ DDTrace\trace_method('ExceptionClass', 'exceptionMethod', function (SpanData $sp
3636
});
3737

3838
$rr = new RequestReplayer();
39+
$rr->replayRequest(); // cleanup possible leftover
3940

4041
try {
4142
$exceptionClass = new ExceptionClass();

0 commit comments

Comments
 (0)