diff --git a/common/molten_shm.c b/common/molten_shm.c index c3fe2aa..ceb4434 100644 --- a/common/molten_shm.c +++ b/common/molten_shm.c @@ -42,7 +42,7 @@ void mo_shm_ctor(mo_shm_t *msm) MOLTEN_ERROR("[shm] shmat error:%d", errno); return; } - + memset(msm->mm, 0, msm->shm_size); /* lock ctx init */ if (mo_lock_init() == -1) { MOLTEN_ERROR("[shm] mo lock init error"); diff --git a/example/molten.php b/example/molten.php new file mode 100644 index 0000000..caf1779 --- /dev/null +++ b/example/molten.php @@ -0,0 +1,29 @@ +getTraceHeader("service"); +print_r($header); +$header['duration'] = 264778; +$header['binaryAnnotations'] = array( + array( + 'key' => 'service.connect', + 'value' => '192.168.1.108:9980', + 'endpoint' => array( + 'serviceName' => $header['name'], + 'ipv4' => '127.0.0.1', + 'port' => 80, + ), + ), + array( + 'key' => 'service.getResult', + 'value' => '192.168.1.108:9980', + 'endpoint' => array( + 'serviceName' => $header['name'], + 'ipv4' => '127.0.0.1', + 'port' => 80, + ), + ), +); +$m->addSpans($header); +$c=curl_init("http://localhost:1231");curl_exec($c); +// $c=curl_init("http://local.stats.chelun.com/test/index");curl_exec($c); \ No newline at end of file diff --git a/molten.c b/molten.c index 6a83c41..15be5a4 100644 --- a/molten.c +++ b/molten.c @@ -18,15 +18,11 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" #include "php_molten.h" #include "zend_extensions.h" #include "SAPI.h" #include "molten_chain.h" -#include "molten_log.h" #include "molten_util.h" #include "molten_slog.h" #include "php7_wrapper.h" @@ -42,7 +38,8 @@ return SUCCESS; \ } \ } \ -}while(0) +}while(0) + PHP_FUNCTION(molten_set_service_name); PHP_FUNCTION(molten_curl_setopt); PHP_FUNCTION(molten_curl_exec); @@ -50,6 +47,12 @@ PHP_FUNCTION(molten_curl_setopt_array); PHP_FUNCTION(molten_curl_reset); PHP_FUNCTION(molten_span_format); +static PHP_METHOD(molten, __construct); +static PHP_METHOD(molten, __destruct); +static PHP_METHOD(molten, set); +static PHP_METHOD(molten, addSpans); +static PHP_METHOD(molten, getTraceHeader); + void add_http_trace_header(mo_chain_t *pct, zval *header, char *span_id); static void frame_build(mo_frame_t *frame, zend_bool internal, unsigned char type, zend_execute_data *caller, zend_execute_data *ex, zend_op_array *op_array TSRMLS_DC); static void frame_destroy(mo_frame_t *frame); @@ -90,6 +93,21 @@ ZEND_DECLARE_MODULE_GLOBALS(molten) /* Make sapi_module accessable */ extern sapi_module_struct sapi_module; +ZEND_BEGIN_ARG_INFO_EX(arginfo_void, 0, 0, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_molten_set, 0, 0, 1) + ZEND_ARG_ARRAY_INFO(0, settings, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_molten_add_spans, 0, 0, 1) + ZEND_ARG_ARRAY_INFO(0, spans, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_molten_header, 0, 0, 1) + ZEND_ARG_INFO(0, name) +ZEND_END_ARG_INFO() + /* Every user visible function must have an entry in trace_functions[]. */ const zend_function_entry molten_functions[] = { PHP_FE(molten_set_service_name, NULL) @@ -101,6 +119,15 @@ const zend_function_entry molten_functions[] = { PHP_FE_END /* Must be the last line in trace_functions[] */ }; +static zend_function_entry molten_methods[] = { + PHP_ME(molten, __construct,arginfo_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) + PHP_ME(molten, __destruct, arginfo_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR) + PHP_ME(molten, set, arginfo_molten_set, ZEND_ACC_PUBLIC) + PHP_ME(molten, addSpans, arginfo_molten_add_spans, ZEND_ACC_PUBLIC) + PHP_ME(molten, getTraceHeader,arginfo_molten_header, ZEND_ACC_PUBLIC) + PHP_FE_END /* Must be the last line in trace_functions[] */ +}; + /* {{{ molten_deps */ static const zend_module_dep molten_deps[] = { ZEND_MOD_REQUIRED("json") @@ -372,7 +399,6 @@ PHP_FUNCTION(molten_curl_exec) mo_chain_add_span(&PTG(pcl), curl_span); pop_span_context(&PTG(span_stack)); } - } /* }}} */ @@ -450,6 +476,9 @@ PHP_FUNCTION(molten_span_format) } /* }}} */ +zend_class_entry molten_ce; +zend_class_entry *molten_class_entry_ptr; + /* {{{ molten_module_entry */ zend_module_entry molten_module_entry = { @@ -480,19 +509,23 @@ ZEND_GET_MODULE(molten) PHP_INI_BEGIN() STD_PHP_INI_ENTRY("molten.enable", "1", PHP_INI_SYSTEM, OnUpdateBool, enable, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sink_log_path", DEFAULT_LOG_DIR, PHP_INI_SYSTEM, OnUpdateString, chain_log_path, zend_molten_globals, molten_globals) - STD_PHP_INI_ENTRY("molten.service_name", "default", PHP_INI_ALL, OnUpdateString, service_name, zend_molten_globals, molten_globals) + + STD_PHP_INI_ENTRY("molten.service_name", "default", PHP_INI_SYSTEM, OnUpdateString, service_name, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.tracing_cli", "0", PHP_INI_SYSTEM, OnUpdateLong, tracing_cli, zend_molten_globals, molten_globals) - STD_PHP_INI_ENTRY("molten.sampling_type", "1", PHP_INI_SYSTEM, OnUpdateLong, sampling_type, zend_molten_globals, molten_globals) - STD_PHP_INI_ENTRY("molten.sampling_request", "1000", PHP_INI_SYSTEM, OnUpdateLong, sampling_request, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.sampling_type", "1", PHP_INI_SYSTEM, OnUpdateLong, sampling_type, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.sampling_request", "1000", PHP_INI_SYSTEM, OnUpdateLong, sampling_request, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sampling_rate", "64", PHP_INI_SYSTEM, OnUpdateLong, sampling_rate, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.socket_host", "127.0.0.1", PHP_INI_SYSTEM, OnUpdateString, socket_host, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.socket_port", "9981", PHP_INI_SYSTEM, OnUpdateLong, socket_port, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.span_format", "zipkin", PHP_INI_SYSTEM, OnUpdateString, span_format, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.report_interval", "60", PHP_INI_SYSTEM, OnUpdateLong, report_interval, zend_molten_globals, molten_globals) - STD_PHP_INI_ENTRY("molten.notify_uri", "", PHP_INI_SYSTEM, OnUpdateString, notify_uri, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.notify_uri", "", PHP_INI_SYSTEM, OnUpdateString, notify_uri, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.report_limit", "100", PHP_INI_SYSTEM, OnUpdateLong, report_limit, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sink_type", "1", PHP_INI_SYSTEM, OnUpdateLong, sink_type, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.output_type", "1", PHP_INI_SYSTEM, OnUpdateLong, output_type, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sink_http_uri", "", PHP_INI_SYSTEM, OnUpdateString, sink_http_uri, zend_molten_globals, molten_globals) - STD_PHP_INI_ENTRY("molten.sink_syslog_unix_socket", "", PHP_INI_SYSTEM, OnUpdateString, sink_syslog_unix_socket, zend_molten_globals, molten_globals) + STD_PHP_INI_ENTRY("molten.sink_syslog_unix_socket", "", PHP_INI_SYSTEM, OnUpdateString, sink_syslog_unix_socket, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sink_kafka_brokers", "", PHP_INI_SYSTEM, OnUpdateString, sink_kafka_brokers, zend_molten_globals, molten_globals) STD_PHP_INI_ENTRY("molten.sink_kafka_topic", "", PHP_INI_SYSTEM, OnUpdateString, sink_kafka_topic, zend_molten_globals, molten_globals) PHP_INI_END() @@ -514,6 +547,9 @@ PHP_MINIT_FUNCTION(molten) ZEND_INIT_MODULE_GLOBALS(molten, php_trace_init_globals, NULL); REGISTER_INI_ENTRIES(); + INIT_CLASS_ENTRY(molten_ce, "molten", molten_methods); + molten_class_entry_ptr = zend_register_internal_class(&molten_ce TSRMLS_CC); + if (!PTG(enable)) { return SUCCESS; } @@ -556,10 +592,12 @@ PHP_MINIT_FUNCTION(molten) /* module ctor */ mo_obtain_local_ip(PTG(ip)); + mo_shm_ctor(&PTG(msm)); mo_ctrl_ctor(&PTG(prt), &PTG(msm), PTG(notify_uri), PTG(ip), PTG(sampling_type), PTG(sampling_rate), PTG(sampling_request), PTG(pct).is_cli); + mo_span_ctor(&PTG(psb), PTG(span_format)); - mo_chain_log_ctor(&PTG(pcl), PTG(host_name), PTG(chain_log_path), PTG(sink_type), PTG(output_type), PTG(sink_http_uri), PTG(sink_syslog_unix_socket)); + mo_chain_log_ctor(&PTG(pcl), PTG(socket_host), PTG(socket_port), PTG(host_name), PTG(chain_log_path), PTG(sink_type), PTG(output_type), PTG(sink_http_uri), PTG(sink_syslog_unix_socket)); mo_intercept_ctor(&PTG(pit), &PTG(pct), &PTG(psb)); mo_rep_ctor(&PTG(pre), PTG(report_interval), PTG(report_limit)); @@ -629,7 +667,7 @@ PHP_RINIT_FUNCTION(molten) PTG(in_request) = 1; /* execute begin time */ - PTG(execute_begin_time) = (long) SG(global_request_time) * 1000000; + PTG(execute_begin_time) = mo_time_usec(); /* Join domain and path */ join_ori_url(&PTG(request_uri), 1); @@ -732,6 +770,66 @@ PHP_FUNCTION(molten_set_service_name) } /* }}} */ +static PHP_METHOD(molten, __construct) +{ + RETURN_TRUE; +} + +static PHP_METHOD(molten, __destruct) +{ + RETURN_TRUE; +} + +static PHP_METHOD(molten, set) +{ + zval *zset = NULL; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE) + { + return; + } + RETURN_TRUE; +} + +static PHP_METHOD(molten, addSpans) +{ + zval *span; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &span) == FAILURE) + { + return; + } + if (PTG(pct).pch.is_sampled == 1) { + mo_zval_add_ref(&span); + mo_chain_add_span1(&PTG(pcl), span); + } + RETURN_TRUE; +} + +static PHP_METHOD(molten, getTraceHeader) +{ + zval *span; + char *span_id = NULL; + char *parent_span_id = NULL; + + char *service_name = NULL; + size_t service_name_len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &service_name, &service_name_len) == FAILURE) + { + return; + } + if (PTG(pct).pch.is_sampled == 1) { + push_span_context(&PTG(span_stack)); + uint64_t start_time = mo_time_usec(); + retrieve_span_id(&PTG(span_stack), &span_id); + retrieve_parent_span_id(&PTG(span_stack), &parent_span_id); + + PTG(psb).start_span(&span, service_name, PTG(pct).pch.trace_id->val, span_id, parent_span_id, start_time, 0, &PTG(pct), AN_CLIENT); + pop_span_context(&PTG(span_stack)); + RETURN_ZVAL(span, 0, 0); + } else { + RETURN_BOOL(0); + } +} + /* {{{ Obtain zend function */ static inline zend_function *obtain_zend_function(zend_bool internal, zend_execute_data *ex, zend_op_array *op_array TSRMLS_DC) { diff --git a/molten_ctrl.c b/molten_ctrl.c index 1ea9584..770139e 100644 --- a/molten_ctrl.c +++ b/molten_ctrl.c @@ -197,7 +197,7 @@ void mo_ctrl_sampling(mo_ctrl_t *prt, mo_chain_t *pct) /* sampling rate */ if (prt->mcm->sampling_type == SAMPLING_RATE) { - if (check_hit_ratio(prt->mcm->sampling_rate)) { + if (check_hit_ratio(prt->mcm->sampling_rate,100)) { pct->pch.is_sampled = 1; } else { pct->pch.is_sampled = 0; diff --git a/molten_intercept.c b/molten_intercept.c index d2dac53..7c8c4a2 100644 --- a/molten_intercept.c +++ b/molten_intercept.c @@ -190,7 +190,7 @@ static void curl_multi_remove_handle_record(mo_interceptor_t *pit, mo_frame_t *f /* }}} */ /* {{{ Build curl bannotation */ -void build_curl_bannotation(zval *span, long timestamp, mo_interceptor_t *pit, zval *handle, char *method, zend_bool check_error) +void build_curl_bannotation(zval *span, long timestamp, mo_interceptor_t *pit, zval *handle, char *method, zend_bool check_error) { zval func; zval *args[1]; @@ -235,7 +235,7 @@ void build_curl_bannotation(zval *span, long timestamp, mo_interceptor_t *pit, z result = mo_call_user_function(EG(function_table), (zval **)NULL, &func, &ret, 1, args); if (result == SUCCESS) { if (Z_TYPE(ret) == IS_STRING && Z_STRLEN(ret) > 0) { - errstr = estrdup(Z_STRVAL(ret)); + errstr = estrdup(Z_STRVAL(ret)); } else { errstr = NULL; } @@ -1405,7 +1405,6 @@ void mo_intercept_ctor(mo_interceptor_t *pit, struct mo_chain_st *pct, mo_span_b pit->pct = pct; pit->psb = psb; - if (extension_loaded("PDO")) { ADD_INTERCEPTOR_TAG(pit, PDO); ADD_INTERCEPTOR_TAG(pit, PDOStatement); diff --git a/molten_intercept.h b/molten_intercept.h index e447cdb..b5e5588 100644 --- a/molten_intercept.h +++ b/molten_intercept.h @@ -44,7 +44,7 @@ #include "molten_log.h" #include "molten_util.h" #include "molten_chain.h" - +#include "molten_report.h" #define HEADER_INTERNAL_CALL 1 #define HEADER_OUT_CALL 0 @@ -82,7 +82,7 @@ typedef struct { /* relation struct */ struct mo_chain_st *pct; - mo_span_builder *psb; + mo_span_builder *psb; }mo_interceptor_t; diff --git a/molten_log.c b/molten_log.c index 9c4f368..d50ee3d 100644 --- a/molten_log.c +++ b/molten_log.c @@ -156,8 +156,69 @@ static void syslog_sink_shutdown(mo_chain_log_t *log) { } /* }}} */ +static int socket_connect(mo_chain_log_t *log) +{ + if (log->timeout > 0) { + double timeout = log->timeout; + int res; + struct timeval timeo; + timeo.tv_sec = (int) timeout; + timeo.tv_usec = (int) ((timeout - timeo.tv_sec) * 1000 * 1000); + setsockopt(log->socket, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeo, sizeof(timeo)); + setsockopt(log->socket, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeo, sizeof(timeo)); + } + int ret; + while (1) + { + ret = connect(log->socket, (struct sockaddr *) &log->inet_v4, log->socklen); + if (ret < 0) + { + if (errno == EINTR) + { + continue; + } + } + break; + } + if (ret >= 0) { + log->socket_active = 1; + } + return ret; +} + +/* {{{ init socket sink */ +static int socket_sink_init(mo_chain_log_t *log) +{ + //已经创建 + if (log->socket) { + return 1; + } + if (log->host == NULL) { + return 1; + } + log->inet_v4.sin_family = AF_INET; + log->inet_v4.sin_port = htons(log->port); + log->socklen = sizeof(log->inet_v4); + void *s_addr = NULL; + s_addr = &log->inet_v4.sin_addr.s_addr; + inet_pton(AF_INET, log->host, s_addr); +#ifdef SOCK_CLOEXEC + log->socket = socket(AF_INET,SOCK_STREAM | SOCK_CLOEXEC,0); +#else + log->socket = socket(AF_INET,SOCK_STREAM,0); +#endif + return 0; +} +/* }}} */ + +/* {{{ socket shutdown */ +static void socket_sink_shutdown(mo_chain_log_t *log) { + if (log->socket > 0) close(log->socket); +} +/* }}} */ + /* {{{ Log module ctor */ -void mo_chain_log_ctor(mo_chain_log_t *log, char *host_name, char *log_path, long sink_type, long output_type, char *post_uri, char *syslog_unix_socket) +void mo_chain_log_ctor(mo_chain_log_t *log, char *host, int port, char *host_name, char *log_path, long sink_type, long output_type, char *post_uri, char *syslog_unix_socket) { log->path = log_path; log->tm_yday = -1; @@ -173,6 +234,9 @@ void mo_chain_log_ctor(mo_chain_log_t *log, char *host_name, char *log_path, lon log->buf = pemalloc(ALLOC_LOG_SIZE, 1); log->total_size = ALLOC_LOG_SIZE; log->alloc_size = 0; + log->host = host; + log->port = port; + log->timeout = 0.5; /* set support type */ log->support_type = SINK_LOG | SINK_STD | SINK_SYSLOG ; @@ -185,6 +249,12 @@ void mo_chain_log_ctor(mo_chain_log_t *log, char *host_name, char *log_path, lon log->support_type |= SINK_KAFKA; #endif + + /* set sink none */ + if (!(sink_type & log->support_type)) { + log->sink_type = SINK_NONE; + } + /* todo for func cb, current use if else */ if (log->sink_type == SINK_LOG) { generate_log_path(log); @@ -211,11 +281,14 @@ void mo_chain_log_dtor(mo_chain_log_t *log) CLOSE_LOG_FD; } } - /* unix fd close */ if (log->sink_type == SINK_SYSLOG) { syslog_sink_shutdown(log); } + /* socket fd close */ + if (log->sink_type == SINK_SOCKET) { + socket_sink_shutdown(log); + } } /* }}} */ @@ -226,6 +299,9 @@ void mo_chain_log_init(mo_chain_log_t *log) log->alloc_size = 0; MO_ALLOC_INIT_ZVAL(log->spans); array_init(log->spans); + if (log->sink_type == SINK_SOCKET) { + socket_sink_init(log); + } } /* }}} */ @@ -243,6 +319,16 @@ void mo_chain_add_span(mo_chain_log_t *log, zval *span) } /* }}} */ +/* {{{ Add stack span to chain */ +void mo_chain_add_span1(mo_chain_log_t *log, zval *span) +{ + if (log == NULL || log->spans == NULL) { + return; + } + add_next_index_zval(log->spans, span); +} +/* }}} */ + /* {{{ Add chain log */ void mo_chain_log_add(mo_chain_log_t *log, char *buf, size_t size) { @@ -419,6 +505,46 @@ static void inline flush_log_to_syslog(mo_chain_log_t *log, char *bytes, int siz } /* }}} */ +/* {{{ flush log to socket */ +static void inline flush_log_to_socket(mo_chain_log_t *log, char *bytes, int length) +{ + assert(length > 0); + assert(bytes != NULL); + + //connect failed try to connect + if (log->socket_active == 0) { + socket_connect(log); + } + if (log->socket_active > 0) { + char *buff = NULL; + char *root = NULL; + buff = emalloc(length + 4); + root = buff; + int num = htonl(length); + memcpy(buff,&num,4); + memcpy(buff+4,bytes,length); + int len = length+4; + int written = 0; + int n; + + while (written < len) + { + n = send(log->socket, buff, len - written, 0); + if (n < 0) + { + if (errno == EINTR) + { + continue; + } + } + written += n; + buff += n; + } + efree(root); + } +} +/* }}} */ + /* {{{ pt write info to log */ void mo_log_write(mo_chain_log_t *log, char *bytes, int size) { @@ -453,6 +579,9 @@ void mo_log_write(mo_chain_log_t *log, char *bytes, int size) case SINK_SYSLOG: flush_log_to_syslog(log, bytes, size); break; + case SINK_SOCKET: + flush_log_to_socket(log, bytes, size); + break; #ifdef HAS_CURL case SINK_HTTP: send_data_by_http(log->post_uri, bytes); @@ -477,7 +606,7 @@ void mo_chain_log_flush(mo_chain_log_t *log) /* Init json encode function */ zval func; MO_ZVAL_STRING(&func, "json_encode", 1); - + if (log->output_type == SPANS_BREAK) { /* Encode one span one line , easy for debug */ HashTable *ht = Z_ARRVAL_P(log->spans); @@ -519,10 +648,11 @@ void mo_chain_log_flush(mo_chain_log_t *log) goto end; } } + SLOG(SLOG_INFO, "[sink] mo log flush detail size:%d", log->alloc_size); + mo_log_write(log, log->buf, log->alloc_size); - end: mo_zval_dtor(&func); mo_zval_ptr_dtor(&log->spans); diff --git a/molten_log.h b/molten_log.h index 965caa8..6c1196c 100644 --- a/molten_log.h +++ b/molten_log.h @@ -28,7 +28,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -50,7 +53,6 @@ #include "php.h" #include "Zend/zend_llist.h" -#include "molten_log.h" #include "molten_util.h" #include "php7_wrapper.h" #include "molten_slog.h" @@ -76,7 +78,7 @@ #ifdef HAS_KAFKA #define SINK_KAFKA 16 #endif - +#define SINK_SOCKET 6 /* only for ipv */ #define SPANS_WRAP 1<<0 #define SPANS_BREAK 1<<1 @@ -117,6 +119,15 @@ typedef struct { int sfd; /* unix domain syslog fd */ struct sockaddr_un server; /* server addr */ + /* socket tcp */ + char *host; + int port; + struct sockaddr_in inet_v4; + socklen_t socklen; //socket len + int socket; //fd + int socket_active; + double timeout; + /* sink http */ char *post_uri; /* post uri */ @@ -130,15 +141,24 @@ typedef struct { zval *spans; } mo_chain_log_t; +typedef struct { + char *key; /* error key */ + char *error; /* error message (truncate) */ + long timestamp; /* error occour timestamp */ +} mo_chain_error_t; + + /* function */ void mo_chain_log_init(mo_chain_log_t *log); -void mo_chain_log_ctor(mo_chain_log_t *log, char *host_name, char *log_path, long sink_type, long output_type, char *post_uri, char *syslog_unix_socket); +void mo_chain_log_ctor(mo_chain_log_t *log, char *host, int port, char *host_name, char *log_path, long sink_type, long output_type, char *post_uri, char *syslog_unix_socket); int mo_chain_log_set_file_path(char *new_path); void mo_chain_log_add(mo_chain_log_t *log, char *buf, size_t size); void mo_chain_log_flush(mo_chain_log_t *log); void mo_chain_log_dtor(mo_chain_log_t *log); void mo_chain_add_span(mo_chain_log_t *log, zval *span); +void mo_chain_log_dtor(mo_chain_log_t *log); void mo_log_write(mo_chain_log_t *log, char *bytes, int size); + #ifdef HAS_CURL void send_data_by_http(char *post_uri, char *post_data); #endif diff --git a/molten_report.c b/molten_report.c index 88cef7e..f423c90 100644 --- a/molten_report.c +++ b/molten_report.c @@ -33,7 +33,7 @@ void mo_rep_ctor(mo_report_t *pre, long rep_interval, long report_limit) pre->rep_interval = rep_interval; pre->report_limit = report_limit; - zend_llist_init(&pre->error_list, sizeof(mo_rep_error_t), rep_error_dtor, 1); + zend_llist_init(&pre->error_list, sizeof(mo_rep_error_t), rep_error_dtor, 1); /* init repi */ /* @@ -159,7 +159,7 @@ void mo_rep_record_data(mo_report_t *pre, mo_repi_t *pri, mo_chain_log_t *pcl, s } */ - if (check_interval(pre, usec)) { +// if (check_interval(pre, usec)) { /* smart_string_appendl(&send, REPORT_FLAG, sizeof(REPORT_FLAG) - 1); @@ -182,7 +182,7 @@ void mo_rep_record_data(mo_report_t *pre, mo_repi_t *pri, mo_chain_log_t *pcl, s MO_FREE_ALLOC_ZVAL(pack); */ - if (zend_llist_count > 0) { + if (zend_llist_count(&pre->error_list) > 0) { smart_string send = {0}; format_llist_to_array(&send, &pre->error_list); @@ -201,6 +201,6 @@ void mo_rep_record_data(mo_report_t *pre, mo_repi_t *pri, mo_chain_log_t *pcl, s */ zend_llist_init(&pre->error_list, sizeof(mo_rep_error_t), rep_error_dtor, 1); } - } +// } } /* ]}} */ diff --git a/molten_util.c b/molten_util.c index 103467f..4a53dbb 100644 --- a/molten_util.c +++ b/molten_util.c @@ -14,6 +14,7 @@ * limitations under the License. */ #include "molten_util.h" +#include #if RAND_MAX/256 >= 0xFFFFFFFFFFFFFF #define LOOP_COUNT 1 @@ -45,15 +46,11 @@ uint64_t rand_uint64(void) } /* check is hit or not */ -int check_hit_ratio(long base) +int check_hit_ratio(int probability , int divisor) { - struct timeval tv; - int seed = gettimeofday(&tv, NULL) == 0 ? tv.tv_usec * getpid() : getpid(); - srandom(seed); - int num = random(); - - /* the remainder 0 is choose by self */ - if (num % base == 0) { + int nrand; + nrand = (int64_t) ((float) divisor * php_combined_lcg()); + if (probability > 0 && nrand < probability) { return 1; } else { return 0; diff --git a/molten_util.h b/molten_util.h index 95aa2b1..340afd4 100644 --- a/molten_util.h +++ b/molten_util.h @@ -153,6 +153,6 @@ void bin2hex64(char **output, const uint64_t *input); void rand64hex(char **output); void build_span_id_random(char **span_id, char *parent_span_id, int span_count); void build_span_id_level(char **span_id, char *parent_span_id, int span_count); -int check_hit_ratio(long base); +int check_hit_ratio(int probability , int divisor); smart_string repr_zval(zval *zv, int limit TSRMLS_DC); #endif diff --git a/php7_wrapper.h b/php7_wrapper.h index 75547f8..a18c76d 100644 --- a/php7_wrapper.h +++ b/php7_wrapper.h @@ -32,6 +32,8 @@ #define smart_string_appendl smart_str_appendl #define smart_string_0 smart_str_0 +#define mo_zval_add_ref zval_add_ref + /* php_json_encode */ #define mo_php_json_encode php_json_encode @@ -159,6 +161,8 @@ static inline int mo_zend_hash_get_current_data(HashTable *ht, void **v) #include "ext/standard/php_smart_string.h" #include "Zend/zend_smart_str.h" +#define mo_zval_add_ref(p) Z_TRY_ADDREF_P(*p) + /* php_json_encode */ static void inline mo_php_json_encode(smart_string *s, zval *z, int options) { diff --git a/php_molten.h b/php_molten.h index be5a27f..b6e2eec 100644 --- a/php_molten.h +++ b/php_molten.h @@ -19,9 +19,6 @@ #define PHP_MOLTEN_VERSION "0.1.2beta" -extern zend_module_entry molten_module_entry; -#define phpext_trace_ptr &molten_module_entry - #ifdef PHP_WIN32 # define PHP_MOLTEN_API __declspec(dllexport) #elif defined(__GNUC__) && __GNUC__ >= 4 @@ -34,6 +31,24 @@ extern zend_module_entry molten_module_entry; #include "TSRM.h" #endif +#include "php.h" +#include "php_ini.h" +#include "php_globals.h" +#include "php_main.h" + +#include "php_streams.h" +#include "php_network.h" + +#include "zend_interfaces.h" +#include "zend_exceptions.h" +#include "zend_variables.h" +#include +#include +#include +#include +#include +#include + #include "molten_chain.h" #include "molten_intercept.h" #include "molten_span.h" @@ -45,6 +60,11 @@ extern zend_module_entry molten_module_entry; #include "molten_stack.h" #include "php7_wrapper.h" + +extern zend_module_entry molten_module_entry; +#define phpext_trace_ptr &molten_module_entry + + #ifndef HOST_NAME_MAX #define HOST_NAME_MAX 255 #endif @@ -61,13 +81,15 @@ ZEND_BEGIN_MODULE_GLOBALS(molten) long sampling_type; /* sampling type */ long sampling_request; /* sampling by request one minute */ - long sampling_rate; /* tracing sampling rate */ + long sampling_rate; /* tracing sampling rate */ char *chain_log_path; /* chain log path */ char *service_name; /* service name */ zend_bool tracing_cli; /* enable cli tracing */ char *span_format; /* the span format */ long report_interval; /* call ctrl interval */ long report_limit; /* report limit */ + char *socket_host; /* report socket host */ + int socket_port; /* report socket port */ char *notify_uri; /* notify uri */ long sink_type; /* log sink type */ long output_type; /* sink spans output type */ @@ -94,7 +116,6 @@ ZEND_BEGIN_MODULE_GLOBALS(molten) zend_bool in_request; /* determine in requeset life time */ ZEND_END_MODULE_GLOBALS(molten) - #ifdef ZEND_ENGINE_3 /* Always refer to the globals in your function as TRACE_G(variable). You are * encouraged to rename these macros something shorter, see examples in any