Skip to content

Commit ce20a9e

Browse files
committed
fix: compilation error
Signed-off-by: Alexandre Rulleau <alexandre.rulleau@datadoghq.com>
1 parent 273d530 commit ce20a9e

14 files changed

+93
-99
lines changed

components-rs/telemetry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub unsafe extern "C" fn ddog_sidecar_telemetry_enqueueConfig_buffer(
143143
value: config_value.to_utf8_lossy().into_owned(),
144144
origin,
145145
config_id,
146+
seq_id: None,
146147
});
147148
buffer.buffer.push(SidecarAction::Telemetry(action));
148149
}

ext/sidecar.c

Lines changed: 29 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <php.h>
2+
#include <main/SAPI.h>
13
#include "ddtrace.h"
24
#include "auto_flush.h"
35
#include "compat_string.h"
@@ -162,7 +164,6 @@ static void dd_sidecar_on_reconnect(ddog_SidecarTransport *transport) {
162164

163165
}
164166

165-
// Subprocess connection mode - current default behavior
166167
ddog_SidecarTransport *ddtrace_sidecar_connect_subprocess(void) {
167168
if (!ddtrace_endpoint) {
168169
return NULL;
@@ -193,13 +194,11 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_subprocess(void) {
193194

194195
dd_sidecar_post_connect(&sidecar_transport, false, logpath);
195196

196-
// Set active mode
197197
ddtrace_sidecar_active_mode = DD_SIDECAR_CONNECTION_SUBPROCESS;
198198

199199
return sidecar_transport;
200200
}
201201

202-
// Thread connection mode - fallback when subprocess fails
203202
ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void) {
204203
if (!ddtrace_endpoint) {
205204
return NULL;
@@ -211,12 +210,10 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void) {
211210
bool is_master = (ddtrace_sidecar_master_pid == 0 || current_pid == ddtrace_sidecar_master_pid);
212211

213212
if (is_master) {
214-
// Set master PID
215213
if (ddtrace_sidecar_master_pid == 0) {
216214
ddtrace_sidecar_master_pid = current_pid;
217215
}
218216

219-
// Start master listener thread (only if not already running)
220217
if (!ddog_sidecar_is_master_listener_active(ddtrace_sidecar_master_pid)) {
221218
if (!ddtrace_ffi_try("Failed starting master listener thread",
222219
ddog_sidecar_connect_master(ddtrace_sidecar_master_pid))) {
@@ -228,7 +225,6 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void) {
228225
}
229226
}
230227

231-
// Connect as worker to master listener
232228
ddog_SidecarTransport *sidecar_transport;
233229
if (!ddtrace_ffi_try("Failed connecting to master listener (thread mode)",
234230
ddog_sidecar_connect_worker(ddtrace_sidecar_master_pid, &sidecar_transport))) {
@@ -244,7 +240,6 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void) {
244240

245241
dd_sidecar_post_connect(&sidecar_transport, false, logpath);
246242

247-
// Set active mode
248243
ddtrace_sidecar_active_mode = DD_SIDECAR_CONNECTION_THREAD;
249244

250245
return sidecar_transport;
@@ -255,15 +250,25 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void) {
255250
#endif
256251
}
257252

258-
// Auto-fallback connection logic
259-
ddog_SidecarTransport *ddtrace_sidecar_connect_with_fallback(void) {
253+
ddog_SidecarTransport *ddtrace_sidecar_connect(bool is_fork) {
254+
if (is_fork && ddtrace_sidecar_active_mode == DD_SIDECAR_CONNECTION_THREAD) {
255+
LOG(WARN, "Thread mode sidecar cannot be reset after fork, sidecar unavailable");
256+
return NULL;
257+
}
258+
259+
if (ddtrace_sidecar_active_mode == DD_SIDECAR_CONNECTION_SUBPROCESS) {
260+
return ddtrace_sidecar_connect_subprocess();
261+
} else if (ddtrace_sidecar_active_mode == DD_SIDECAR_CONNECTION_THREAD) {
262+
return ddtrace_sidecar_connect_thread();
263+
}
264+
260265
zend_long mode = get_global_DD_TRACE_SIDECAR_CONNECTION_MODE();
261266
ddog_SidecarTransport *transport = NULL;
262267

263268
switch (mode) {
264269
case DD_TRACE_SIDECAR_CONNECTION_MODE_SUBPROCESS:
265270
// Force subprocess only
266-
LOG(INFO, "Sidecar connection mode: subprocess (forced)");
271+
LOG(DEBUG, "Sidecar connection mode: subprocess (forced)");
267272
transport = ddtrace_sidecar_connect_subprocess();
268273
if (!transport) {
269274
LOG(ERROR, "Subprocess connection failed (mode=subprocess, no fallback)");
@@ -272,7 +277,7 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_with_fallback(void) {
272277

273278
case DD_TRACE_SIDECAR_CONNECTION_MODE_THREAD:
274279
// Force thread only
275-
LOG(INFO, "Sidecar connection mode: thread (forced)");
280+
LOG(DEBUG, "Sidecar connection mode: thread (forced)");
276281
transport = ddtrace_sidecar_connect_thread();
277282
if (!transport) {
278283
LOG(ERROR, "Thread connection failed (mode=thread, no fallback)");
@@ -281,14 +286,17 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_with_fallback(void) {
281286

282287
case DD_TRACE_SIDECAR_CONNECTION_MODE_AUTO:
283288
default:
284-
// Try subprocess first
285-
LOG(INFO, "Sidecar connection mode: auto (trying subprocess first)");
289+
// Try subprocess first, fallback to thread if needed
290+
LOG(DEBUG, "Sidecar connection mode: auto (trying subprocess first)");
286291
transport = ddtrace_sidecar_connect_subprocess();
287292

288293
if (transport) {
289-
LOG(INFO, "Connected to sidecar via subprocess");
294+
LOG(DEBUG, "Connected to sidecar via subprocess");
295+
} else if (!ddtrace_endpoint) {
296+
// Don't try fallback if endpoint is invalid - both modes need a valid endpoint
297+
// The "Invalid DD_TRACE_AGENT_URL" error was already logged during endpoint creation
290298
} else {
291-
// Fallback to thread mode
299+
// Subprocess failed but endpoint is valid - try thread mode fallback
292300
LOG(WARN, "Subprocess connection failed, falling back to thread mode");
293301
transport = ddtrace_sidecar_connect_thread();
294302

@@ -304,51 +312,8 @@ ddog_SidecarTransport *ddtrace_sidecar_connect_with_fallback(void) {
304312
return transport;
305313
}
306314

307-
static ddog_SidecarTransport *dd_sidecar_connection_factory_ex(bool is_fork) {
308-
// Should not happen, unless the agent url is malformed
309-
if (!ddtrace_endpoint) {
310-
return NULL;
311-
}
312-
ZEND_ASSERT(dogstatsd_endpoint != NULL);
313-
314-
dd_set_endpoint_test_token(dogstatsd_endpoint);
315-
316-
#ifdef _WIN32
317-
DDOG_PHP_FUNCTION = (const uint8_t *)zend_hash_func;
318-
#endif
319-
320-
char logpath[MAXPATHLEN];
321-
int error_fd = atomic_load(&ddtrace_error_log_fd);
322-
if (error_fd == -1 || ddtrace_get_fd_path(error_fd, logpath) < 0) {
323-
*logpath = 0;
324-
}
325-
326-
ddog_SidecarTransport *sidecar_transport;
327-
if (!ddtrace_ffi_try("Failed connecting to the sidecar", ddog_sidecar_connect_php(&sidecar_transport, logpath, dd_zend_string_to_CharSlice(get_global_DD_TRACE_LOG_LEVEL()), get_global_DD_INSTRUMENTATION_TELEMETRY_ENABLED(), dd_sidecar_on_reconnect, ddtrace_endpoint))) {
328-
dd_free_endpoints();
329-
return NULL;
330-
}
331-
332-
dd_sidecar_post_connect(&sidecar_transport, is_fork, logpath);
333-
334-
return sidecar_transport;
335-
}
336-
337-
ddog_SidecarTransport *dd_sidecar_connection_factory(void) {
338-
// Reconnect using the same mode that succeeded initially
339-
switch (ddtrace_sidecar_active_mode) {
340-
case DD_SIDECAR_CONNECTION_SUBPROCESS:
341-
return ddtrace_sidecar_connect_subprocess();
342-
343-
case DD_SIDECAR_CONNECTION_THREAD:
344-
return ddtrace_sidecar_connect_thread();
345-
346-
case DD_SIDECAR_CONNECTION_NONE:
347-
default:
348-
// Shouldn't happen, but fall back to auto mode
349-
LOG(WARN, "Reconnection attempted with no active mode, using fallback logic");
350-
return ddtrace_sidecar_connect_with_fallback();
351-
}
315+
static ddog_SidecarTransport *ddtrace_sidecar_connect_callback(void) {
316+
return ddtrace_sidecar_connect(false);
352317
}
353318

354319
bool ddtrace_sidecar_maybe_enable_appsec(bool *appsec_activation, bool *appsec_config) {
@@ -381,8 +346,7 @@ void ddtrace_sidecar_setup(bool appsec_activation, bool appsec_config) {
381346

382347
ddog_init_remote_config(get_global_DD_INSTRUMENTATION_TELEMETRY_ENABLED(), appsec_activation, appsec_config);
383348

384-
// Use fallback connection logic
385-
ddtrace_sidecar = ddtrace_sidecar_connect_with_fallback();
349+
ddtrace_sidecar = ddtrace_sidecar_connect(false);
386350
if (!ddtrace_sidecar) { // Something went wrong
387351
if (ddtrace_endpoint) {
388352
dd_free_endpoints();
@@ -405,7 +369,7 @@ void ddtrace_sidecar_minit(void) {
405369

406370
void ddtrace_sidecar_ensure_active(void) {
407371
if (ddtrace_sidecar) {
408-
ddtrace_sidecar_reconnect(&ddtrace_sidecar, dd_sidecar_connection_factory);
372+
ddtrace_sidecar_reconnect(&ddtrace_sidecar, ddtrace_sidecar_connect_callback);
409373
}
410374
}
411375

@@ -482,19 +446,8 @@ void ddtrace_reset_sidecar(void) {
482446
ddog_sidecar_transport_drop(ddtrace_sidecar);
483447
ddtrace_sidecar = NULL;
484448

485-
// Don't reconnect in thread mode after fork (Option A: documented incompatibility)
486-
if (ddtrace_sidecar_active_mode == DD_SIDECAR_CONNECTION_THREAD) {
487-
// Sidecar unavailable in child process after fork
488-
LOG(WARN, "Thread mode sidecar cannot be reset after fork, sidecar unavailable");
489-
if (ddtrace_endpoint) {
490-
dd_free_endpoints();
491-
}
492-
return;
493-
}
494-
495-
// For subprocess mode, reconnect with is_fork=true
496-
ddtrace_sidecar = dd_sidecar_connection_factory_ex(true);
497-
if (!ddtrace_sidecar) { // Something went wrong
449+
ddtrace_sidecar = ddtrace_sidecar_connect(true);
450+
if (!ddtrace_sidecar) {
498451
if (ddtrace_endpoint) {
499452
dd_free_endpoints();
500453
}

ext/sidecar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DDTRACE_PUBLIC struct telemetry_rc_info ddtrace_get_telemetry_rc_info(void);
3232
// Connection functions
3333
ddog_SidecarTransport *ddtrace_sidecar_connect_subprocess(void);
3434
ddog_SidecarTransport *ddtrace_sidecar_connect_thread(void);
35-
ddog_SidecarTransport *ddtrace_sidecar_connect_with_fallback(void);
35+
ddog_SidecarTransport *ddtrace_sidecar_connect(bool is_fork);
3636

3737
// Lifecycle functions
3838
void ddtrace_sidecar_minit(void);

tests/ext/appsec/sca_flag_is_sent_01.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_01-telemetry.out"
1717
--FILE_EXTERNAL--
1818
sca_test.inc
1919
--EXPECT--
20-
array(4) {
20+
array(5) {
2121
["name"]=>
2222
string(18) "appsec.sca_enabled"
2323
["value"]=>
@@ -26,6 +26,8 @@ array(4) {
2626
string(7) "default"
2727
["config_id"]=>
2828
NULL
29+
["seq_id"]=>
30+
NULL
2931
}
3032
string(4) "Sent"
3133
--CLEAN--

tests/ext/appsec/sca_flag_is_sent_02.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_02-telemetry.out"
1818
--FILE_EXTERNAL--
1919
sca_test.inc
2020
--EXPECT--
21-
array(4) {
21+
array(5) {
2222
["name"]=>
2323
string(18) "appsec.sca_enabled"
2424
["value"]=>
@@ -27,6 +27,8 @@ array(4) {
2727
string(7) "env_var"
2828
["config_id"]=>
2929
NULL
30+
["seq_id"]=>
31+
NULL
3032
}
3133
string(4) "Sent"
3234
--CLEAN--

tests/ext/appsec/sca_flag_is_sent_03.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_03-telemetry.out"
1818
--FILE_EXTERNAL--
1919
sca_test.inc
2020
--EXPECT--
21-
array(4) {
21+
array(5) {
2222
["name"]=>
2323
string(18) "appsec.sca_enabled"
2424
["value"]=>
@@ -27,6 +27,8 @@ array(4) {
2727
string(7) "env_var"
2828
["config_id"]=>
2929
NULL
30+
["seq_id"]=>
31+
NULL
3032
}
3133
string(4) "Sent"
3234
--CLEAN--

tests/ext/appsec/sca_flag_is_sent_04.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ datadog.appsec.sca_enabled=1
1818
--FILE_EXTERNAL--
1919
sca_test.inc
2020
--EXPECT--
21-
array(4) {
21+
array(5) {
2222
["name"]=>
2323
string(18) "appsec.sca_enabled"
2424
["value"]=>
@@ -27,6 +27,8 @@ array(4) {
2727
string(7) "env_var"
2828
["config_id"]=>
2929
NULL
30+
["seq_id"]=>
31+
NULL
3032
}
3133
string(4) "Sent"
3234
--CLEAN--

tests/ext/appsec/sca_flag_is_sent_05.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ datadog.appsec.sca_enabled=0
1818
--FILE_EXTERNAL--
1919
sca_test.inc
2020
--EXPECT--
21-
array(4) {
21+
array(5) {
2222
["name"]=>
2323
string(18) "appsec.sca_enabled"
2424
["value"]=>
@@ -27,6 +27,8 @@ array(4) {
2727
string(7) "env_var"
2828
["config_id"]=>
2929
NULL
30+
["seq_id"]=>
31+
NULL
3032
}
3133
string(4) "Sent"
3234
--CLEAN--

tests/ext/library_config/fleet_config.phpt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ DD_DYNAMIC_INSTRUMENTATION_ENABLED: true
7676
------ Telemetry ------
7777
array(5) {
7878
[0]=>
79-
array(4) {
79+
array(5) {
8080
["name"]=>
8181
string(3) "env"
8282
["value"]=>
@@ -85,9 +85,11 @@ array(5) {
8585
string(19) "fleet_stable_config"
8686
["config_id"]=>
8787
string(15) "42_fleet_config"
88+
["seq_id"]=>
89+
NULL
8890
}
8991
[1]=>
90-
array(4) {
92+
array(5) {
9193
["name"]=>
9294
string(7) "service"
9395
["value"]=>
@@ -96,9 +98,11 @@ array(5) {
9698
string(19) "fleet_stable_config"
9799
["config_id"]=>
98100
string(15) "42_fleet_config"
101+
["seq_id"]=>
102+
NULL
99103
}
100104
[2]=>
101-
array(4) {
105+
array(5) {
102106
["name"]=>
103107
string(24) "trace.generate_root_span"
104108
["value"]=>
@@ -107,9 +111,11 @@ array(5) {
107111
string(7) "default"
108112
["config_id"]=>
109113
NULL
114+
["seq_id"]=>
115+
NULL
110116
}
111117
[3]=>
112-
array(4) {
118+
array(5) {
113119
["name"]=>
114120
string(17) "trace.spans_limit"
115121
["value"]=>
@@ -118,9 +124,11 @@ array(5) {
118124
string(7) "env_var"
119125
["config_id"]=>
120126
NULL
127+
["seq_id"]=>
128+
NULL
121129
}
122130
[4]=>
123-
array(4) {
131+
array(5) {
124132
["name"]=>
125133
string(31) "dynamic_instrumentation.enabled"
126134
["value"]=>
@@ -129,6 +137,8 @@ array(5) {
129137
string(19) "fleet_stable_config"
130138
["config_id"]=>
131139
string(15) "42_fleet_config"
140+
["seq_id"]=>
141+
NULL
132142
}
133143
}
134144
--CLEAN--

0 commit comments

Comments
 (0)