Skip to content

Commit b9cdb0a

Browse files
authored
Collect agent info and apply the env for remote config (#2930)
* Collect agent info and apply the env for remote config Signed-off-by: Bob Weinand <[email protected]> * Adapt to latest libdatadog * Fix double alloc Signed-off-by: Bob Weinand <[email protected]> * Fix tests Signed-off-by: Bob Weinand <[email protected]> * Exclude PHP 7.2 and 7.3 from tests for agent_env Signed-off-by: Bob Weinand <[email protected]> --------- Signed-off-by: Bob Weinand <[email protected]>
1 parent ea58159 commit b9cdb0a

File tree

17 files changed

+241
-49
lines changed

17 files changed

+241
-49
lines changed

Cargo.lock

+71-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ RUN_TESTS_CMD := REPORT_EXIT_STATUS=1 TEST_PHP_SRCDIR=$(PROJECT_ROOT) USE_TRACKE
4545

4646
C_FILES = $(shell find components components-rs ext src/dogstatsd zend_abstract_interface -name '*.c' -o -name '*.h' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
4747
TEST_FILES = $(shell find tests/ext -name '*.php*' -o -name '*.inc' -o -name '*.json' -o -name 'CONFLICTS' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
48-
RUST_FILES = $(BUILD_DIR)/Cargo.toml $(BUILD_DIR)/Cargo.lock $(shell find components-rs -name '*.c' -o -name '*.rs' -o -name 'Cargo.toml' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' ) $(shell find libdatadog/{alloc,build-common,crashtracker,crashtracker-ffi,ddcommon,ddcommon-ffi,ddsketch,ddtelemetry,ddtelemetry-ffi,dogstatsd-client,dynamic-configuration,ipc,live-debugger,live-debugger-ffi,remote-config,sidecar,sidecar-ffi,spawn_worker,tinybytes,tools/{cc_utils,sidecar_mockgen},trace-*,Cargo.toml} -type f \( -path "*/src*" -o -path "*/examples*" -o -path "*Cargo.toml" -o -path "*/build.rs" -o -path "*/tests/dataservice.rs" -o -path "*/tests/service_functional.rs" \) -not -path "*/ipc/build.rs" -not -path "*/sidecar-ffi/build.rs")
48+
RUST_FILES = $(BUILD_DIR)/Cargo.toml $(BUILD_DIR)/Cargo.lock $(shell find components-rs -name '*.c' -o -name '*.rs' -o -name 'Cargo.toml' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' ) $(shell find libdatadog/{alloc,build-common,crashtracker,crashtracker-ffi,data-pipeline,ddcommon,ddcommon-ffi,ddsketch,ddtelemetry,ddtelemetry-ffi,dogstatsd-client,dynamic-configuration,ipc,live-debugger,live-debugger-ffi,remote-config,sidecar,sidecar-ffi,spawn_worker,tinybytes,tools/{cc_utils,sidecar_mockgen},trace-*,Cargo.toml} -type f \( -path "*/src*" -o -path "*/examples*" -o -path "*Cargo.toml" -o -path "*/build.rs" -o -path "*/tests/dataservice.rs" -o -path "*/tests/service_functional.rs" \) -not -path "*/ipc/build.rs" -not -path "*/sidecar-ffi/build.rs")
4949
ALL_OBJECT_FILES = $(C_FILES) $(RUST_FILES) $(BUILD_DIR)/Makefile
5050
TEST_OPCACHE_FILES = $(shell find tests/opcache -name '*.php*' -o -name '.gitkeep' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
5151
TEST_STUB_FILES = $(shell find tests/ext -type d -name 'stubs' -exec find '{}' -type f \; | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )

components-rs/common.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,8 @@ typedef struct ddog_ContextKey {
821821
enum ddog_MetricType _1;
822822
} ddog_ContextKey;
823823

824+
typedef struct ddog_AgentInfoReader ddog_AgentInfoReader;
825+
824826
typedef struct ddog_AgentRemoteConfigReader ddog_AgentRemoteConfigReader;
825827

826828
typedef struct ddog_AgentRemoteConfigWriter_ShmHandle ddog_AgentRemoteConfigWriter_ShmHandle;
@@ -957,14 +959,23 @@ typedef struct ddog_crasht_Slice_CharSlice {
957959
typedef struct ddog_crasht_Config {
958960
struct ddog_crasht_Slice_CharSlice additional_files;
959961
bool create_alt_stack;
962+
bool use_alt_stack;
960963
/**
961964
* The endpoint to send the crash report to (can be a file://).
962965
* If None, the crashtracker will infer the agent host from env variables.
963966
*/
964967
const struct ddog_Endpoint *endpoint;
965968
enum ddog_crasht_StacktraceCollection resolve_frames;
966-
uint64_t timeout_secs;
967-
bool wait_for_receiver;
969+
/**
970+
* Timeout in milliseconds before the signal handler starts tearing things down to return.
971+
* This is given as a uint32_t, but the actual timeout needs to fit inside of an i32 (max
972+
* 2^31-1). This is a limitation of the various interfaces used to guarantee the timeout.
973+
*/
974+
uint32_t timeout_ms;
975+
/**
976+
* Optional filename for a unix domain socket if the receiver is used asynchonously
977+
*/
978+
ddog_CharSlice optional_unix_socket_filename;
968979
} ddog_crasht_Config;
969980

970981
typedef struct ddog_crasht_EnvVar {

components-rs/crashtracker.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ DDOG_CHECK_RETURN struct ddog_crasht_Result ddog_crasht_shutdown(void);
3939
* Reinitialize the crash-tracking infrastructure after a fork.
4040
* This should be one of the first things done after a fork, to minimize the
4141
* chance that a crash occurs between the fork, and this call.
42-
* In particular, reset the counters that track the profiler state machine,
43-
* and start a new receiver to collect data from this fork.
42+
* In particular, reset the counters that track the profiler state machine.
4443
* NOTE: An alternative design would be to have a 1:many sidecar listening on a
4544
* socket instead of 1:1 receiver listening on a pipe, but the only real
4645
* advantage would be to have fewer processes in `ps -a`.
@@ -73,15 +72,15 @@ struct ddog_crasht_Result ddog_crasht_update_on_fork(struct ddog_crasht_Config c
7372
* unexpected crash-handling behaviour.
7473
*/
7574
DDOG_CHECK_RETURN
76-
struct ddog_crasht_Result ddog_crasht_init_with_receiver(struct ddog_crasht_Config config,
77-
struct ddog_crasht_ReceiverConfig receiver_config,
78-
struct ddog_crasht_Metadata metadata);
75+
struct ddog_crasht_Result ddog_crasht_init(struct ddog_crasht_Config config,
76+
struct ddog_crasht_ReceiverConfig receiver_config,
77+
struct ddog_crasht_Metadata metadata);
7978

8079
/**
81-
* Initialize the crash-tracking infrastructure, writing to an unix socket in case of crash.
80+
* Initialize the crash-tracking infrastructure without launching the receiver.
8281
*
8382
* # Preconditions
84-
* None.
83+
* Requires `config` to be given with a `unix_socket_path`, which is normally optional.
8584
* # Safety
8685
* Crash-tracking functions are not reentrant.
8786
* No other crash-handler functions should be called concurrently.
@@ -90,8 +89,7 @@ struct ddog_crasht_Result ddog_crasht_init_with_receiver(struct ddog_crasht_Conf
9089
* unexpected crash-handling behaviour.
9190
*/
9291
DDOG_CHECK_RETURN
93-
struct ddog_crasht_Result ddog_crasht_init_with_unix_socket(struct ddog_crasht_Config config,
94-
ddog_CharSlice socket_path,
92+
struct ddog_crasht_Result ddog_crasht_init_without_receiver(struct ddog_crasht_Config config,
9593
struct ddog_crasht_Metadata metadata);
9694

9795
/**

components-rs/sidecar.h

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ void ddog_ph_file_drop(struct ddog_NativeFile ph);
3333

3434
ddog_MaybeError ddog_alloc_anon_shm_handle(uintptr_t size, struct ddog_ShmHandle **handle);
3535

36+
ddog_MaybeError ddog_alloc_anon_shm_handle_named(uintptr_t size,
37+
struct ddog_ShmHandle **handle,
38+
ddog_CharSlice name);
39+
3640
ddog_MaybeError ddog_map_shm(struct ddog_ShmHandle *handle,
3741
struct ddog_MappedMem_ShmHandle **mapped,
3842
void **pointer,
@@ -306,4 +310,19 @@ void ddog_sidecar_reconnect(struct ddog_SidecarTransport **transport,
306310
*/
307311
ddog_CharSlice ddog_sidecar_get_crashtracker_unix_socket_path(void);
308312

313+
/**
314+
* Gets an agent info reader.
315+
*/
316+
struct ddog_AgentInfoReader *ddog_get_agent_info_reader(const struct ddog_Endpoint *endpoint);
317+
318+
/**
319+
* Gets the current agent info environment (or empty if not existing)
320+
*/
321+
ddog_CharSlice ddog_get_agent_info_env(struct ddog_AgentInfoReader *reader, bool *changed);
322+
323+
/**
324+
* Drops the agent info reader.
325+
*/
326+
void ddog_drop_agent_info_reader(struct ddog_AgentInfoReader*);
327+
309328
#endif /* DDOG_SIDECAR_H */

config.m4

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ if test "$PHP_DDTRACE" != "no"; then
162162
dnl ddtrace.c comes first, then everything else alphabetically
163163
DD_TRACE_PHP_SOURCES="$EXTRA_PHP_SOURCES \
164164
ext/ddtrace.c \
165+
ext/agent_info.c \
165166
ext/arrays.c \
166167
ext/auto_flush.c \
167168
ext/autoload_php_files.c \

config.w32

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ if (PHP_DDTRACE != 'no') {
1515

1616
var version = PHP_VERSION * 100 + PHP_MINOR_VERSION;
1717

18-
var DDTRACE_EXT_SOURCES = "arrays.c";
18+
var DDTRACE_EXT_SOURCES = "agent_info.c";
19+
DDTRACE_EXT_SOURCES += " arrays.c";
1920
DDTRACE_EXT_SOURCES += " auto_flush.c";
2021
DDTRACE_EXT_SOURCES += " autoload_php_files.c";
2122
DDTRACE_EXT_SOURCES += " collect_backtrace.c";

dockerfiles/services/request-replayer/src/index.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function decodeDogStatsDMetrics($metrics)
7878
define('REQUEST_RC_CONFIGS_FILE', getenv('REQUEST_RC_CONFIGS_FILE') ?: ("$temp_location/rc_configs.json"));
7979
define('REQUEST_METRICS_FILE', getenv('REQUEST_METRICS_FILE') ?: ("$temp_location/metrics.json"));
8080
define('REQUEST_METRICS_LOG_FILE', getenv('REQUEST_METRICS_LOG_FILE') ?: ("$temp_location/metrics-log.txt"));
81+
define('REQUEST_AGENT_INFO_FILE', getenv('REQUEST_AGENT_INFO_FILE') ?: ("$temp_location/agent-info.txt"));
8182

8283
function logRequest($message, $data = '')
8384
{
@@ -91,8 +92,8 @@ function logRequest($message, $data = '')
9192
}
9293

9394
set_error_handler(function ($number, $message, $errfile, $errline) {
94-
if (error_reporting() == 0) {
95-
return;
95+
if (!($number & error_reporting())) {
96+
return true;
9697
}
9798
logRequest("Triggered error $number $message in $errfile on line $errline: " . (new \Exception)->getTraceAsString());
9899
trigger_error($message, $number);
@@ -142,6 +143,9 @@ function logRequest($message, $data = '')
142143
if (file_exists(REQUEST_NEXT_RESPONSE_FILE)) {
143144
unlink(REQUEST_NEXT_RESPONSE_FILE);
144145
}
146+
if (file_exists(REQUEST_AGENT_INFO_FILE)) {
147+
unlink(REQUEST_AGENT_INFO_FILE);
148+
}
145149
logRequest('Deleted request log');
146150
break;
147151
case '/next-response':
@@ -214,6 +218,16 @@ function logRequest($message, $data = '')
214218
file_put_contents(REQUEST_METRICS_FILE, json_encode($allMetrics));
215219
}
216220
break;
221+
case '/set-agent-info':
222+
$raw = file_get_contents('php://input');
223+
file_put_contents(REQUEST_AGENT_INFO_FILE, $raw);
224+
break;
225+
case '/info':
226+
$file = @file_get_contents(REQUEST_AGENT_INFO_FILE) ?: "{}";
227+
logRequest('Requested /info endpoint, returning ' . $file);
228+
header("datadog-agent-state: " . sha1($file));
229+
echo $file;
230+
break;
217231
default:
218232
$headers = getallheaders();
219233
if (isset($headers['X-Datadog-Diagnostic-Check']) || isset($headers['x-datadog-diagnostic-check'])) {

0 commit comments

Comments
 (0)