Skip to content

Commit

Permalink
Fix distributed tracing on PHP 8
Browse files Browse the repository at this point in the history
The curl extension API was upgraded from a resource to an object in php/php-src#5402.
  • Loading branch information
SammyK committed Nov 19, 2020
1 parent 2ebb9c1 commit d53cf9f
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 405 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,11 @@ TEST_WEB_74 := \
test_opentracing_10

TEST_INTEGRATIONS_80 := \
test_integrations_curl \
test_integrations_mysqli \
test_integrations_pdo \
test_integrations_guzzle5 \
test_integrations_guzzle6 \
test_integrations_predis1

TEST_WEB_80 := \
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions bridge/dd_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function_exists('dd_trace_internal_fn') && \dd_trace_internal_fn('ddtrace_reload

// Required classes and functions
require __DIR__ . '/autoload.php';
if (\PHP_MAJOR_VERSION === 5) {
require __DIR__ . '/php5.php';
if (\PHP_MAJOR_VERSION === 8 || \PHP_MAJOR_VERSION === 5) {
require __DIR__ . '/curl_inject.php';
}

// Optional classes and functions
Expand Down
1 change: 0 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ if test "$PHP_DDTRACE" != "no"; then
ext/php8/configuration_php_iface.c \
ext/php8/ddtrace_string.c \
ext/php8/dispatch.c \
ext/php8/distributed_tracing.c \
ext/php8/dogstatsd_client.c \
ext/php8/engine_api.c \
ext/php8/engine_hooks.c \
Expand Down
4 changes: 1 addition & 3 deletions ext/php8/ddtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "ddtrace.h"
#include "ddtrace_string.h"
#include "dispatch.h"
#include "distributed_tracing.h"
#include "dogstatsd_client.h"
#include "engine_hooks.h"
#include "excluded_modules.h"
Expand Down Expand Up @@ -420,10 +419,10 @@ static PHP_RINIT_FUNCTION(ddtrace) {
dd_request_init_hook_rinit(TSRMLS_C);
}

ddtrace_internal_handlers_rinit();
ddtrace_engine_hooks_rinit(TSRMLS_C);
ddtrace_bgs_log_rinit(PG(error_log));
ddtrace_dispatch_init(TSRMLS_C);
ddtrace_distributed_tracing_rinit(TSRMLS_C);
DDTRACE_G(disable_in_current_request) = 0;

ddtrace_dogstatsd_client_rinit(TSRMLS_C);
Expand Down Expand Up @@ -458,7 +457,6 @@ static PHP_RSHUTDOWN_FUNCTION(ddtrace) {
ddtrace_internal_handlers_rshutdown();
ddtrace_dogstatsd_client_rshutdown(TSRMLS_C);

ddtrace_distributed_tracing_rshutdown(TSRMLS_C);
ddtrace_dispatch_destroy(TSRMLS_C);
ddtrace_free_span_id_stack(TSRMLS_C);
ddtrace_free_span_stacks(TSRMLS_C);
Expand Down
42 changes: 0 additions & 42 deletions ext/php8/ddtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ extern zend_class_entry *ddtrace_ce_fatal_error;
typedef struct ddtrace_span_ids_t ddtrace_span_ids_t;
typedef struct ddtrace_span_fci ddtrace_span_fci;

#if PHP_VERSION_ID >= 70000
zval *ddtrace_spandata_property_name(zval *spandata);
zval *ddtrace_spandata_property_resource(zval *spandata);
zval *ddtrace_spandata_property_service(zval *spandata);
zval *ddtrace_spandata_property_type(zval *spandata);
zval *ddtrace_spandata_property_meta(zval *spandata);
zval *ddtrace_spandata_property_metrics(zval *spandata);
#endif

BOOL_T ddtrace_tracer_is_limited(TSRMLS_D);

Expand All @@ -45,25 +43,6 @@ ZEND_BEGIN_MODULE_GLOBALS(ddtrace)
char *dogstatsd_port;
char *dogstatsd_buffer;

// PHP 7 uses ZEND_TLS for these
#if PHP_VERSION_ID < 70000
// Distributed tracing & curl
HashTable *dt_http_saved_curl_headers;
zend_bool back_up_http_headers;

/* These ones are used for measuring the call stack depth so that we can
* emit a warning prior to encountering a stack overflow.
*
* A 16-bit call depth would allow us to count to 65,535, which is way more
* than necessary. An 8-bit depth would be inadequate (255).
*/
bool should_warn_call_depth;
uint16_t call_depth;

// ext/curl's list entry resource type
int le_curl;
#endif

uint64_t trace_id;
ddtrace_span_ids_t *span_ids_top;
ddtrace_span_fci *open_spans_top;
Expand Down Expand Up @@ -92,13 +71,7 @@ ZEND_END_MODULE_GLOBALS(ddtrace)
* defines these macros without the comma in the definition site, so that it
* exists at the usage site.
*/
#if PHP_VERSION_ID < 70000
#define DDTRACE_ARG_INFO_SIZE(arg_info) ((zend_uint)(sizeof(arg_info) / sizeof(struct _zend_arg_info) - 1))
#elif PHP_VERSION_ID < 80100
#define DDTRACE_ARG_INFO_SIZE(arg_info) ((uint32_t)(sizeof(arg_info) / sizeof(struct _zend_internal_arg_info) - 1))
#else
#error Check if ZEND_FENTRY has changed in PHP 8.1 and if we need to update the macros
#endif

#define DDTRACE_FENTRY(zend_name, name, arg_info, flags) \
{ #zend_name, name, arg_info, DDTRACE_ARG_INFO_SIZE(arg_info), flags }
Expand All @@ -111,19 +84,4 @@ ZEND_END_MODULE_GLOBALS(ddtrace)
#define DDTRACE_FALIAS(name, alias, arg_info) DDTRACE_RAW_FENTRY(#name, zif_##alias, arg_info, 0)
#define DDTRACE_FE_END ZEND_FE_END

/* Currently used on PHP 5. After a zend_execute_ex has called the previous hook
* the execute_data cannot be trusted for some things, notably function_state.
* So we use this struct to back up the data.
*/
struct ddtrace_execute_data {
zval *This;
zend_class_entry *scope;
zend_function *fbc;
const zend_op *opline;
void **arguments;
zval *retval;
bool free_retval;
};
typedef struct ddtrace_execute_data ddtrace_execute_data;

#endif // DDTRACE_H
7 changes: 0 additions & 7 deletions ext/php8/distributed_tracing.c

This file was deleted.

11 changes: 0 additions & 11 deletions ext/php8/distributed_tracing.h

This file was deleted.

15 changes: 9 additions & 6 deletions ext/php8/engine_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ ZEND_RESULT_CODE ddtrace_call_method(zend_object *obj, zend_class_entry *ce, zen
}

ZEND_RESULT_CODE ddtrace_call_function(zend_function **fn_proxy, const char *name, size_t name_len, zval *retval,
int argc, zval argv[]) {
int argc, ...) {
zend_fcall_info fci = {
.size = sizeof(zend_fcall_info),
};
zend_fcall_info_cache fcc = {
.function_handler = (fn_proxy && *fn_proxy) ? *fn_proxy : NULL,
};

va_list argv;
va_start(argv, argc);
zend_fcall_info_argv(&fci, (uint32_t)argc, &argv);
va_end(argv);

if (!fcc.function_handler) {
// This avoids allocating a zend_string if fn_proxy is used
zval fname = ddtrace_zval_stringl(name, name_len);
Expand All @@ -111,12 +116,10 @@ ZEND_RESULT_CODE ddtrace_call_function(zend_function **fn_proxy, const char *nam
// ZVAL_COPY_VALUE(&fci.function_name, fcc.function_handler->common.function_name);

fci.retval = retval;
fci.params = argv;
#if PHP_VERSION_ID < 80000
fci.no_separation = 0; // allow for by-ref args
#endif
fci.param_count = argc;
ZEND_RESULT_CODE result = zend_call_function(&fci, &fcc);

zend_fcall_info_args_clear(&fci, 1);

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/php8/engine_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inline zval ddtrace_zval_undef(void) {
ZEND_RESULT_CODE ddtrace_call_method(zend_object *obj, zend_class_entry *ce, zend_function **fn_proxy,
const char *fname, size_t fname_len, zval *retval, int argc, zval *argv);
ZEND_RESULT_CODE ddtrace_call_function(zend_function **fn_proxy, const char *name, size_t name_len, zval *retval,
int argc, zval argv[]);
int argc, ...);

void ddtrace_write_property(zval *obj, const char *prop, size_t prop_len, zval *value);
bool ddtrace_property_exists(zval *object, zval *property);
Expand Down
Loading

0 comments on commit d53cf9f

Please sign in to comment.