Skip to content

Commit

Permalink
Collect agent info and apply the env for remote config (#2930)
Browse files Browse the repository at this point in the history
* Collect agent info and apply the env for remote config

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>

* Adapt to latest libdatadog

* Fix double alloc

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>

* Fix tests

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>

* Exclude PHP 7.2 and 7.3 from tests for agent_env

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>

---------

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi authored Nov 15, 2024
1 parent ea58159 commit b9cdb0a
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 49 deletions.
92 changes: 71 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RUN_TESTS_CMD := REPORT_EXIT_STATUS=1 TEST_PHP_SRCDIR=$(PROJECT_ROOT) USE_TRACKE

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 }' )
TEST_FILES = $(shell find tests/ext -name '*.php*' -o -name '*.inc' -o -name '*.json' -o -name 'CONFLICTS' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
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")
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")
ALL_OBJECT_FILES = $(C_FILES) $(RUST_FILES) $(BUILD_DIR)/Makefile
TEST_OPCACHE_FILES = $(shell find tests/opcache -name '*.php*' -o -name '.gitkeep' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
TEST_STUB_FILES = $(shell find tests/ext -type d -name 'stubs' -exec find '{}' -type f \; | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
Expand Down
15 changes: 13 additions & 2 deletions components-rs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,8 @@ typedef struct ddog_ContextKey {
enum ddog_MetricType _1;
} ddog_ContextKey;

typedef struct ddog_AgentInfoReader ddog_AgentInfoReader;

typedef struct ddog_AgentRemoteConfigReader ddog_AgentRemoteConfigReader;

typedef struct ddog_AgentRemoteConfigWriter_ShmHandle ddog_AgentRemoteConfigWriter_ShmHandle;
Expand Down Expand Up @@ -957,14 +959,23 @@ typedef struct ddog_crasht_Slice_CharSlice {
typedef struct ddog_crasht_Config {
struct ddog_crasht_Slice_CharSlice additional_files;
bool create_alt_stack;
bool use_alt_stack;
/**
* The endpoint to send the crash report to (can be a file://).
* If None, the crashtracker will infer the agent host from env variables.
*/
const struct ddog_Endpoint *endpoint;
enum ddog_crasht_StacktraceCollection resolve_frames;
uint64_t timeout_secs;
bool wait_for_receiver;
/**
* Timeout in milliseconds before the signal handler starts tearing things down to return.
* This is given as a uint32_t, but the actual timeout needs to fit inside of an i32 (max
* 2^31-1). This is a limitation of the various interfaces used to guarantee the timeout.
*/
uint32_t timeout_ms;
/**
* Optional filename for a unix domain socket if the receiver is used asynchonously
*/
ddog_CharSlice optional_unix_socket_filename;
} ddog_crasht_Config;

typedef struct ddog_crasht_EnvVar {
Expand Down
16 changes: 7 additions & 9 deletions components-rs/crashtracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ DDOG_CHECK_RETURN struct ddog_crasht_Result ddog_crasht_shutdown(void);
* Reinitialize the crash-tracking infrastructure after a fork.
* This should be one of the first things done after a fork, to minimize the
* chance that a crash occurs between the fork, and this call.
* In particular, reset the counters that track the profiler state machine,
* and start a new receiver to collect data from this fork.
* In particular, reset the counters that track the profiler state machine.
* NOTE: An alternative design would be to have a 1:many sidecar listening on a
* socket instead of 1:1 receiver listening on a pipe, but the only real
* advantage would be to have fewer processes in `ps -a`.
Expand Down Expand Up @@ -73,15 +72,15 @@ struct ddog_crasht_Result ddog_crasht_update_on_fork(struct ddog_crasht_Config c
* unexpected crash-handling behaviour.
*/
DDOG_CHECK_RETURN
struct ddog_crasht_Result ddog_crasht_init_with_receiver(struct ddog_crasht_Config config,
struct ddog_crasht_ReceiverConfig receiver_config,
struct ddog_crasht_Metadata metadata);
struct ddog_crasht_Result ddog_crasht_init(struct ddog_crasht_Config config,
struct ddog_crasht_ReceiverConfig receiver_config,
struct ddog_crasht_Metadata metadata);

/**
* Initialize the crash-tracking infrastructure, writing to an unix socket in case of crash.
* Initialize the crash-tracking infrastructure without launching the receiver.
*
* # Preconditions
* None.
* Requires `config` to be given with a `unix_socket_path`, which is normally optional.
* # Safety
* Crash-tracking functions are not reentrant.
* No other crash-handler functions should be called concurrently.
Expand All @@ -90,8 +89,7 @@ struct ddog_crasht_Result ddog_crasht_init_with_receiver(struct ddog_crasht_Conf
* unexpected crash-handling behaviour.
*/
DDOG_CHECK_RETURN
struct ddog_crasht_Result ddog_crasht_init_with_unix_socket(struct ddog_crasht_Config config,
ddog_CharSlice socket_path,
struct ddog_crasht_Result ddog_crasht_init_without_receiver(struct ddog_crasht_Config config,
struct ddog_crasht_Metadata metadata);

/**
Expand Down
19 changes: 19 additions & 0 deletions components-rs/sidecar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void ddog_ph_file_drop(struct ddog_NativeFile ph);

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

ddog_MaybeError ddog_alloc_anon_shm_handle_named(uintptr_t size,
struct ddog_ShmHandle **handle,
ddog_CharSlice name);

ddog_MaybeError ddog_map_shm(struct ddog_ShmHandle *handle,
struct ddog_MappedMem_ShmHandle **mapped,
void **pointer,
Expand Down Expand Up @@ -306,4 +310,19 @@ void ddog_sidecar_reconnect(struct ddog_SidecarTransport **transport,
*/
ddog_CharSlice ddog_sidecar_get_crashtracker_unix_socket_path(void);

/**
* Gets an agent info reader.
*/
struct ddog_AgentInfoReader *ddog_get_agent_info_reader(const struct ddog_Endpoint *endpoint);

/**
* Gets the current agent info environment (or empty if not existing)
*/
ddog_CharSlice ddog_get_agent_info_env(struct ddog_AgentInfoReader *reader, bool *changed);

/**
* Drops the agent info reader.
*/
void ddog_drop_agent_info_reader(struct ddog_AgentInfoReader*);

#endif /* DDOG_SIDECAR_H */
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ if test "$PHP_DDTRACE" != "no"; then
dnl ddtrace.c comes first, then everything else alphabetically
DD_TRACE_PHP_SOURCES="$EXTRA_PHP_SOURCES \
ext/ddtrace.c \
ext/agent_info.c \
ext/arrays.c \
ext/auto_flush.c \
ext/autoload_php_files.c \
Expand Down
3 changes: 2 additions & 1 deletion config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ if (PHP_DDTRACE != 'no') {

var version = PHP_VERSION * 100 + PHP_MINOR_VERSION;

var DDTRACE_EXT_SOURCES = "arrays.c";
var DDTRACE_EXT_SOURCES = "agent_info.c";
DDTRACE_EXT_SOURCES += " arrays.c";
DDTRACE_EXT_SOURCES += " auto_flush.c";
DDTRACE_EXT_SOURCES += " autoload_php_files.c";
DDTRACE_EXT_SOURCES += " collect_backtrace.c";
Expand Down
18 changes: 16 additions & 2 deletions dockerfiles/services/request-replayer/src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function decodeDogStatsDMetrics($metrics)
define('REQUEST_RC_CONFIGS_FILE', getenv('REQUEST_RC_CONFIGS_FILE') ?: ("$temp_location/rc_configs.json"));
define('REQUEST_METRICS_FILE', getenv('REQUEST_METRICS_FILE') ?: ("$temp_location/metrics.json"));
define('REQUEST_METRICS_LOG_FILE', getenv('REQUEST_METRICS_LOG_FILE') ?: ("$temp_location/metrics-log.txt"));
define('REQUEST_AGENT_INFO_FILE', getenv('REQUEST_AGENT_INFO_FILE') ?: ("$temp_location/agent-info.txt"));

function logRequest($message, $data = '')
{
Expand All @@ -91,8 +92,8 @@ function logRequest($message, $data = '')
}

set_error_handler(function ($number, $message, $errfile, $errline) {
if (error_reporting() == 0) {
return;
if (!($number & error_reporting())) {
return true;
}
logRequest("Triggered error $number $message in $errfile on line $errline: " . (new \Exception)->getTraceAsString());
trigger_error($message, $number);
Expand Down Expand Up @@ -142,6 +143,9 @@ function logRequest($message, $data = '')
if (file_exists(REQUEST_NEXT_RESPONSE_FILE)) {
unlink(REQUEST_NEXT_RESPONSE_FILE);
}
if (file_exists(REQUEST_AGENT_INFO_FILE)) {
unlink(REQUEST_AGENT_INFO_FILE);
}
logRequest('Deleted request log');
break;
case '/next-response':
Expand Down Expand Up @@ -214,6 +218,16 @@ function logRequest($message, $data = '')
file_put_contents(REQUEST_METRICS_FILE, json_encode($allMetrics));
}
break;
case '/set-agent-info':
$raw = file_get_contents('php://input');
file_put_contents(REQUEST_AGENT_INFO_FILE, $raw);
break;
case '/info':
$file = @file_get_contents(REQUEST_AGENT_INFO_FILE) ?: "{}";
logRequest('Requested /info endpoint, returning ' . $file);
header("datadog-agent-state: " . sha1($file));
echo $file;
break;
default:
$headers = getallheaders();
if (isset($headers['X-Datadog-Diagnostic-Check']) || isset($headers['x-datadog-diagnostic-check'])) {
Expand Down
Loading

0 comments on commit b9cdb0a

Please sign in to comment.