Skip to content

Commit

Permalink
Move collecting and flushing telemetry to after final request flush
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Weinand <[email protected]>
  • Loading branch information
bwoebi committed Mar 4, 2024
1 parent f5c4106 commit d0a2ed6
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions ext/ddtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ static void dd_disable_if_incompatible_sapi_detected(void) {
}
}

static void dd_post_sapi_deactivate_minit(void);

static PHP_MINIT_FUNCTION(ddtrace) {
UNUSED(type);

Expand Down Expand Up @@ -1109,6 +1111,7 @@ static PHP_MINIT_FUNCTION(ddtrace) {
ddtrace_dogstatsd_client_minit();
#endif
ddshared_minit();
dd_post_sapi_deactivate_minit();

dd_register_span_data_ce();
dd_register_fatal_error_ce();
Expand Down Expand Up @@ -1401,16 +1404,6 @@ static PHP_RSHUTDOWN_FUNCTION(ddtrace) {
DDTRACE_G(active_stack) = NULL;
}

dd_finalize_telemetry();
if (DDTRACE_G(last_flushed_root_service_name)) {
zend_string_release(DDTRACE_G(last_flushed_root_service_name));
DDTRACE_G(last_flushed_root_service_name) = NULL;
}
if (DDTRACE_G(last_flushed_root_env_name)) {
zend_string_release(DDTRACE_G(last_flushed_root_env_name));
DDTRACE_G(last_flushed_root_env_name) = NULL;
}

return SUCCESS;
}

Expand All @@ -1430,6 +1423,75 @@ zend_result ddtrace_post_deactivate(void) {
return SUCCESS;
}

static void dd_post_sapi_deactivate_do(void) {
dd_finalize_telemetry();
if (DDTRACE_G(last_flushed_root_service_name)) {
zend_string_release(DDTRACE_G(last_flushed_root_service_name));
DDTRACE_G(last_flushed_root_service_name) = NULL;
}
if (DDTRACE_G(last_flushed_root_env_name)) {
zend_string_release(DDTRACE_G(last_flushed_root_env_name));
DDTRACE_G(last_flushed_root_env_name) = NULL;
}
}

static int (*dd_prev_sapi_deactivate)(void);

// Start of request_rec from httpd.h, this has been unchanged forever, so we can just use this.
struct apache_request_rec {
void *pool;
void *connection;
void *server;
void *next;
void *prev;
void *main;
};
static void (*dd_ap_finalize_request_protocol)(struct apache_request_rec *r);

static int dd_post_apache_deactivate(void) {
int result = dd_prev_sapi_deactivate();

struct apache_request_rec *r = ((struct {
int state;
struct apache_request_rec *r;
} *) SG(server_context))->r;
// Check whether this is not an apache sub-request, otherwise we may not alter the state of the connection, i.e. here we don't gain any latency benefit
if (!r->main) {
dd_ap_finalize_request_protocol(r);
}

dd_post_sapi_deactivate_do();

return result;
}

static int dd_post_sapi_deactivate(void) {
int result = dd_prev_sapi_deactivate();
dd_post_sapi_deactivate_do();
return result;
}

static void dd_post_sapi_deactivate_minit(void) {
dd_prev_sapi_deactivate = sapi_module.deactivate;

if (ddtrace_active_sapi == DATADOG_PHP_SAPI_APACHE2HANDLER) {
#ifndef _WIN32
void *handle = dlopen(NULL, RTLD_LAZY);
#else
void *handle = GetModuleHandle(NULL); // not refcounted, must not be closed
#endif
dd_ap_finalize_request_protocol = DL_FETCH_SYMBOL(handle, "ap_finalize_request_protocol");
#ifndef _WIN32
dlclose(handle);
#endif
if (dd_ap_finalize_request_protocol) {
sapi_module.deactivate = dd_post_apache_deactivate;
return;
}
}
sapi_module.deactivate = dd_post_sapi_deactivate;
}

void ddtrace_disable_tracing_in_current_request(void) {
// PHP 8 has ZSTR_CHAR('0') which is nicer...
zend_string *zero = zend_string_init("0", 1, 0);
Expand Down

0 comments on commit d0a2ed6

Please sign in to comment.