diff --git a/sky-php7ext/config.m4 b/sky-php7ext/config.m4 index 0b522434ccec7dff33d43d190bc9867e7e7e7406..48b2f385ad1d8c17e06e81843419d1211552a7c3 100644 --- a/sky-php7ext/config.m4 +++ b/sky-php7ext/config.m4 @@ -98,6 +98,25 @@ if test "$PHP_SKYWALKING" != "no"; then PHP_NEW_EXTENSION(skywalking, \ skywalking.c \ + src/greeter_client.cc \ + src/grpc/ApplicationRegisterService-grpc.pb.cc \ + src/grpc/ApplicationRegisterService.pb.cc \ + src/grpc/Common-grpc.pb.cc \ + src/grpc/Common.pb.cc \ + src/grpc/DiscoveryService-grpc.pb.cc \ + src/grpc/DiscoveryService.pb.cc \ + src/grpc/Downstream-grpc.pb.cc \ + src/grpc/Downstream.pb.cc \ + src/grpc/JVMMetricsService-grpc.pb.cc \ + src/grpc/JVMMetricsService.pb.cc \ + src/grpc/KeyWithIntegerValue-grpc.pb.cc \ + src/grpc/KeyWithIntegerValue.pb.cc \ + src/grpc/KeyWithStringValue-grpc.pb.cc \ + src/grpc/KeyWithStringValue.pb.cc \ + src/grpc/NetworkAddressRegisterService-grpc.pb.cc \ + src/grpc/NetworkAddressRegisterService.pb.cc \ + src/grpc/TraceSegmentService-grpc.pb.cc \ + src/grpc/TraceSegmentService.pb.cc \ , $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_ADD_BUILD_DIR($ext_builddir/src/grpc) diff --git a/sky-php7ext/grpc/common_struct.h b/sky-php7ext/grpc/common_struct.h deleted file mode 100644 index 5b567fbe749fda2965208ead99a54d86e55964b0..0000000000000000000000000000000000000000 --- a/sky-php7ext/grpc/common_struct.h +++ /dev/null @@ -1,157 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: DiscoveryService.proto - -#ifndef DISCOVERY_CLIENT_C_H -#define DISCOVERY_CLIENT_C_H - - -#define METHOD__GO_REGISTER 1 - -#define METHOD__GO_REGISTER_INSTANCE 2 - -#define METHOD__SEND_TRACE_SEGMENT 3 - - -typedef struct _RegisterParamtruct -{ - char* appCode; -}RegisterParamtruct; - -typedef struct _RegisterInstanceParamtruct -{ - char* agentUUID; - char* osName;; - char* hostname; - char* ipv4s; - int applicationId; - long registerTime; - int processNo; -}RegisterInstanceParamtruct; - -typedef enum _SpanTypeStruct -{ - Entry_x, - Exit_x, - Local_x -}SpanTypeStruct; - -typedef enum _SpanLayerStruct -{ - Unknown_x, - Database_x, - RPCFramework_x, - Http_x, - MQ_x, - Cache_x -}SpanLayerStruct; - -typedef struct _KeyWithStringValueStruct -{ - int key; - char *value; -}KeyWithStringValueStruct; - -typedef struct _LogMessageStruct -{ - long time; - KeyWithStringValueStruct* data; -}LogMessageStruct; - -typedef enum _RefTypeStruct -{ - CrossProcess_x, - CrossThread_x -}RefTypeStruct; - -typedef struct _UniqueIdStruct -{ - long idParts; -}UniqueIdStruct; - -typedef struct _TraceSegmentReferenceStruct -{ - RefTypeStruct* refType; - UniqueIdStruct* parentTraceSegmentId; - int parentSpanId; - int parentApplicationInstanceId; - int networkAddress; - int networkAddressId; - int entryApplicationInstanceId; - char* entryServiceName; - int entryServiceId; - char* parentServiceName; - int* parentServiceId; -}TraceSegmentReferenceStruct; - -typedef struct _SpanObjectStruct -{ - int spanId; - int parentSpanId; - long startTime; - long endTime; - TraceSegmentReferenceStruct* refs; - int operationNameId; - char *operationName; - int peerId; - char *peer; - SpanTypeStruct* spanType; - SpanLayerStruct* spanLayer; - int componentId; - int isError; - KeyWithStringValueStruct* tags; - LogMessageStruct* logs; -}SpanObjectStruct; - - - -typedef struct _comListNode -{ - UniqueIdStruct* data; - struct _comListNode* next; -} comListNode; - - -typedef struct _comList -{ - struct _comListNode* head; - struct _comListNode* tail; - int count; -} comList; - - -typedef struct _TraceSegmentObjectStruct -{ - comList* traceSegmentIdList; - SpanObjectStruct *spans; - char *segment; - int applicationId; - int applicationInstanceId; - int isSizeLimited; -}TraceSegmentObjectStruct; - -typedef struct _SendTraceSegmentParamStruct -{ - TraceSegmentObjectStruct* traceSegment; -}SendTraceSegmentParamStruct; - -typedef union _ParamDataStruct -{ - RegisterParamtruct* registerParam; - RegisterInstanceParamtruct* registerInstanceParam; - SendTraceSegmentParamStruct* sendTraceSegmentParam; -} ParamDataStruct; - -typedef struct _UpstreamSegmentStruct -{ - UniqueIdStruct* globalTraceIds; - char *segment; -}UpstreamSegmentStruct; - -typedef struct AppInstanceStruct -{ - int applicationId; - int applicationInstanceId; -} AppInstance; - - -#endif /* #ifndef DISCOVERY_CLIENT_C_H */ \ No newline at end of file diff --git a/sky-php7ext/grpc/greeter_client.cc b/sky-php7ext/grpc/greeter_client.cc deleted file mode 100644 index 5cbbeec0407549f6ba164209c695b5590b6545de..0000000000000000000000000000000000000000 --- a/sky-php7ext/grpc/greeter_client.cc +++ /dev/null @@ -1,223 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include - -#include - -#include "ApplicationRegisterService.grpc.pb.h" -#include "DiscoveryService.grpc.pb.h" -#include "TraceSegmentService.grpc.pb.h" - -#include "common_struct.h" - -using grpc::Channel; -using grpc::ClientContext; -using grpc::Status; -using grpc::ClientReader; -using grpc::ClientReaderWriter; -using grpc::ClientWriter; - - -extern "C" int goSkyGrpc(char*host, int method, ParamDataStruct* paramData, void (*callfuct)( AppInstance* ) ); - -class GreeterClient { - public: - GreeterClient(std::shared_ptr channel) { - channel_ = channel; - } - - // Assembles the client's payload, sends it and presents the response back - // from the server. - AppInstance* goRegister(const std::string& appCode) { - std::unique_ptr stub_; - stub_ = ApplicationRegisterService::NewStub(channel_) ; - // Data we are sending to the server. - Application request; - request.set_applicationcode( appCode ); - // Container for the data we expect from the server. - ApplicationMapping reply; - // Context for the client. It could be used to convey extra information to - // the server and/or tweak certain RPC behaviors. - ClientContext context; - - - KeyWithIntegerValue kwiv; - // The actual RPC. - Status status = stub_->applicationCodeRegister(&context, request, &reply); - AppInstance* appInstance = (AppInstance*)malloc(sizeof(AppInstance)); - // Act upon its status. - if (status.ok()) { - kwiv = reply.application(); - appInstance->applicationId = kwiv.value(); - return appInstance; - } - return NULL; - } - - // Assembles the client's payload, sends it and presents the response back - // from the server. - AppInstance* goRegisterInstance( - std::string agentUUID, - std::string osName, - std::string hostname, - std::string ipv4s, - int applicationId, - long registerTime, - int processNo - ) { - - std::unique_ptr stub_; - stub_ = InstanceDiscoveryService::NewStub(channel_); - // Data we are sending to the server. - ApplicationInstance request; - request.set_applicationid( applicationId ); - request.set_agentuuid( agentUUID ); - request.set_registertime( registerTime ); - - OSInfo* osinfo = new OSInfo; - request.set_allocated_osinfo( osinfo ); - osinfo->set_osname( osName ); - osinfo->set_hostname( hostname ); - osinfo->set_processno( processNo ); - osinfo->add_ipv4s( ipv4s ); - - AppInstance* appInstancestruct = (AppInstance*)malloc(sizeof(AppInstance)); - - // Container for the data we expect from the server. - ApplicationInstanceMapping reply; - - // Context for the client. It could be used to convey extra information to - // the server and/or tweak certain RPC behaviors. - ClientContext context; - - // The actual RPC. - Status status = stub_->registerInstance(&context, request, &reply); - //free OSInfo - //delete osinfo; - // Act upon its status. - if (status.ok()) { - - appInstancestruct->applicationId = reply.applicationid(); - appInstancestruct->applicationInstanceId = reply.applicationinstanceid(); - return appInstancestruct; - } - return NULL; - - - } - - - void sendTrace( TraceSegmentObjectStruct* traceSegment ) { - - - std::unique_ptr stub_; - - stub_ = TraceSegmentService::NewStub(channel_); - // Data we are sending to the server. - UpstreamSegment request ; - //UniqueId - UniqueId* uniqueId; - - uniqueId = request.add_globaltraceids(); - comListNode* com_list = traceSegment->traceSegmentIdList->head; - while(com_list != NULL) - { - uniqueId->add_idparts(com_list->data->idParts); - com_list = com_list->next; - } - if( traceSegment->segment ){ - request.set_segment(traceSegment->segment); - - Downstream reply; - - ClientContext context; - - grpc_connectivity_state state = channel_->GetState(false); - - if( state == GRPC_CHANNEL_TRANSIENT_FAILURE ){ - return ; - } - - std::unique_ptr > writer( stub_->collect(&context, &reply)); - writer->Write(request); - writer->WritesDone(); - writer->Finish(); - - } - - } - - private: - std::shared_ptr channel_; -}; - - - -int goSkyGrpc(char *host, int method, ParamDataStruct* paramData, void (*callfuct)( AppInstance* ) ) { - - std::shared_ptr channel = grpc::CreateChannel( - host, grpc::InsecureChannelCredentials()); - static GreeterClient greeter(channel); - - switch (method) { - case METHOD__GO_REGISTER: - { - std::string appCode( paramData->registerParam->appCode); - callfuct(greeter.goRegister(appCode)); - } - break; - case METHOD__GO_REGISTER_INSTANCE: - - { - std::string agentUUID(paramData->registerInstanceParam->agentUUID); - std::string osName(paramData->registerInstanceParam->osName); - std::string hostname(paramData->registerInstanceParam->hostname); - std::string ipv4s(paramData->registerInstanceParam->ipv4s); - int applicationId = paramData->registerInstanceParam->applicationId; - long registerTime = paramData->registerInstanceParam->registerTime; - int processNo = paramData->registerInstanceParam->processNo; - callfuct( - greeter.goRegisterInstance( - agentUUID, - osName, - hostname, - ipv4s, - applicationId, - registerTime, - processNo - ) - ); - } - break; - case METHOD__SEND_TRACE_SEGMENT: - { - greeter.sendTrace( paramData->sendTraceSegmentParam->traceSegment ); - } - break; - default: - return 0; - break; - } - - - - return 1; -} diff --git a/sky-php7ext/php_skywalking.h b/sky-php7ext/php_skywalking.h index 01f1b65dae7ad04b3359173637925a815a85cea2..377c31fe891e809506b96ccbc28a65c4b7b79aa0 100644 --- a/sky-php7ext/php_skywalking.h +++ b/sky-php7ext/php_skywalking.h @@ -111,83 +111,41 @@ extern zend_module_entry skywalking_module_entry; void *SKY_ADD_ASSOC_ZVAL(zval *z, const char *k) { - zval null_array; - array_init(&null_array); - add_assoc_zval(z, k, &null_array); + zval null_array; + array_init(&null_array); + add_assoc_zval(z, k, &null_array); + return NULL; } -void *SKY_UPDATE_PROPERTY(zend_class_entry *cls, zval *tp, const char *name, size_t name_length) { - zval null_array; - array_init(&null_array); - zend_update_property(cls, tp, name, name_length, &null_array); -} -static void *write_log_text(char *text); static char *sky_json_encode(zval *parameter); -static char *build_SWheader_value(const char *peer_host); -static zval receive_SWHeader_from_caller(); +static long get_second(); static char *get_millisecond(); -static char *uniqid(); static char *generate_sw3(zend_long span_id, zend_string *peer_host, zend_string *operation_name); -static void make_trace_id(); -static zend_always_inline zend_uchar is_sampling(); -static char *generate_trace_id(); -static char *get_ip(); +static void generate_context(); static char *get_page_request_uri(); -static char *get_page_url_and_peer(); -static long generate_span_id(); -static void *write_log( char *text); -static zval *set_span_nodes_data(zval *node_data); +static void write_log( char *text); static void request_init(); -static long sky_array_unshift(zval *stack, zval *var); -static void set_sampling_rate(double degrees); -static void set_span_param_of_curl(char *peer_host); -static void make_span_curl_header(char *peer_host, zval *headers); -static void start_span(zval *this_pr); static int is_auto_open(); -void accel_sky_curl_init(INTERNAL_FUNCTION_PARAMETERS); -void accel_sky_curl_setopt(INTERNAL_FUNCTION_PARAMETERS); void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS); -void list_poll(comList* myroot); -static void *sky_flush_all(); +static void sky_flush_all(); static zval *get_first_span(); static zval *get_spans(); static char* _get_current_machine_ip(); -static int get_app_instance_id(); -static int get_app_id(); -static char *generate_parent_info_from_header(char *header_name); -static void start_node_span_of_curl(); -static char *generate_parent_trace_id(); -static zval* generate_trace_id_for_array(int use_parent_tid, zval* z_trace_id); -static char* _entry_app_name(); -static char *generate_distributed_trace_ids(); -static int _entry_app_instance_id(); -static void end_node_span_of_curl(zval *curl); -static void* send_grpc_param(zval *all_node_data); -static comList* create_com_list(); -static long _entry_app_name_operation_id(); -static long _parent_appname_operation_id(); -static int sky_live_pthread(pthread_t tid); -static int add_com_list(comList *com_list, UniqueIdStruct* data); -static void (*orig_curl_init)(INTERNAL_FUNCTION_PARAMETERS) = NULL; -static void (*orig_curl_setopt)(INTERNAL_FUNCTION_PARAMETERS) = NULL; static void (*orig_curl_exec)(INTERNAL_FUNCTION_PARAMETERS) = NULL; /* Declare any global variables you may need between the BEGIN and END macros here: */ ZEND_BEGIN_MODULE_GLOBALS(skywalking) - char *global_log_path; - char *global_header_client_ip_name; - char *global_app_code; - long global_send_type; - zend_bool global_auto_open; - double global_sampling_rate; - char *global_app_grpc_trace; - zval UpstreamSegment; - zval context; + char *log_path; + char *app_code; + zend_bool enable; + char *grpc; + zval UpstreamSegment; + zval context; ZEND_END_MODULE_GLOBALS(skywalking) extern ZEND_DECLARE_MODULE_GLOBALS(skywalking); @@ -225,7 +183,6 @@ ZEND_TSRMLS_CACHE_EXTERN() #ifdef _AIX #define SKY_OS_NAME "AIX" #endif - #else @@ -233,8 +190,12 @@ ZEND_TSRMLS_CACHE_EXTERN() #ifdef WINVER #define SKY_OS_NAME "Windows" #else +#ifdef __APPLE__ +#define SKY_OS_NAME "Darwin" +#else #define SKY_OS_NAME "Unknown" #endif +#endif #endif diff --git a/sky-php7ext/skywalking.c b/sky-php7ext/skywalking.c index 29c482f4b1b0c728856dc12364da5fabdcd85828..d727b2f06f6a229cc5544df592a9ae599c653a2e 100644 --- a/sky-php7ext/skywalking.c +++ b/sky-php7ext/skywalking.c @@ -30,11 +30,10 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" -#include "grpc/common_struct.h" #include "src/components.h" #include "php_skywalking.h" #include "ext/standard/url.h" /* for php_url */ -#include "ext/standard/php_var.h" +//#include "ext/standard/php_var.h" #include "ext/standard/basic_functions.h" #include "ext/standard/php_math.h" @@ -53,11 +52,11 @@ #include #include -// ipc -#include "sys/shm.h" +extern int applicationCodeRegister(char *grpc_server, char *code); -extern int goSkyGrpc(char*host, int method, ParamDataStruct* paramData, void (*callfuct)( AppInstance* ) ); +extern int registerInstance(char *grpc_server, int appId, long registertime, char *uuid, char *osname, char *hostname, + int processno, char *ipv4s); /* If you declare any globals in php_skywalking.h uncomment this: */ @@ -65,50 +64,19 @@ ZEND_DECLARE_MODULE_GLOBALS(skywalking) /* True global resources - no need for thread safety here */ static int le_skywalking; -static zval glob_skywalking_globals_list, glob_skywalking_globals_queue; -static char super_peer_host[50]; -static int is_send_curl_header = 0; -static zval super_curl_header; -static AppInstance appInstance; -static int application_instance; +static int application_instance = 0; +static int application_id = 0; static int sky_close = 0; static int sky_increment_id = 0; -static long percent_int; -zval sky_segment_queue; -zval sky_entry_span; -zval sky_exit_spans; -pthread_t pid_consumer; -zval temp; - -/* {{{ arginfo */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_sky_create, 0, 0, 1) - ZEND_ARG_INFO(0, appCode) -ZEND_END_ARG_INFO() -/* }}} */ - -/* {{{ arginfo */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_sky_void_arginfo, 0, 0, 0) - ZEND_ARG_INFO(0, appCode) -ZEND_END_ARG_INFO() -/* }}} */ - - - - - /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini*/ PHP_INI_BEGIN() - STD_PHP_INI_BOOLEAN("skywalking.auto_open", "1", PHP_INI_ALL, OnUpdateBool, global_auto_open, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.app_code", "", PHP_INI_ALL, OnUpdateString, global_app_code, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.log_path", "/tmp", PHP_INI_ALL, OnUpdateString, global_log_path, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.send_type", "0", PHP_INI_ALL, OnUpdateLong, global_send_type, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.sampling_rate", "/tmp", PHP_INI_ALL, OnUpdateString, global_sampling_rate, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.header_client_ip_name", "api", PHP_INI_ALL, OnUpdateString, global_header_client_ip_name, zend_skywalking_globals, skywalking_globals) - STD_PHP_INI_ENTRY("skywalking.grpc_trace", "127.0.0.1:50051", PHP_INI_ALL, OnUpdateString, global_app_grpc_trace, zend_skywalking_globals, skywalking_globals) - + STD_PHP_INI_BOOLEAN("skywalking.enable", "1", PHP_INI_ALL, OnUpdateBool, enable, zend_skywalking_globals, skywalking_globals) + STD_PHP_INI_ENTRY("skywalking.app_code", "", PHP_INI_ALL, OnUpdateString, app_code, zend_skywalking_globals, skywalking_globals) + STD_PHP_INI_ENTRY("skywalking.log_path", "/tmp", PHP_INI_ALL, OnUpdateString, log_path, zend_skywalking_globals, skywalking_globals) + STD_PHP_INI_ENTRY("skywalking.grpc", "127.0.0.1:11800", PHP_INI_ALL, OnUpdateString, grpc, zend_skywalking_globals, skywalking_globals) PHP_INI_END() /* }}} */ @@ -140,79 +108,6 @@ const zend_function_entry class_skywalking[] = { follow this convention for the convenience of others editing your code. */ -void accel_sky_curl_init(INTERNAL_FUNCTION_PARAMETERS) -{ - char *url = NULL; - size_t url_len = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &url, &url_len) == FAILURE) { - return; - } - if( url != NULL ){ - php_url *url_info = php_url_parse(url); - char *peer_host_s; - peer_host_s = url_info->host; - int peer_port = 0; - if(url_info->port){ - peer_port = url_info->port; - } - if(peer_port > 0){ - sprintf(super_peer_host, "%s:%d", peer_host_s, peer_port); - }else{ - sprintf(super_peer_host, "%s", peer_host_s); - } - php_url_free(url_info); - } - start_node_span_of_curl(); - - orig_curl_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); -} - -void accel_sky_curl_setopt(INTERNAL_FUNCTION_PARAMETERS) -{ - zval *zid, *zvalue; - zend_long options; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &zid, &options, &zvalue) == FAILURE) { - return; - } - - if( options == CURLOPT_URL ){ - - char *url = NULL; - if( Z_TYPE_P(zvalue) != IS_STRING){ - return ; - } - url = Z_STRVAL_P(zvalue); - php_url *url_info = php_url_parse(url); - char *peer_host_s; - peer_host_s = url_info->host; - int peer_port = 0; - if(url_info->port){ - peer_port = url_info->port; - } - if(peer_port > 0){ - sprintf(super_peer_host, "%s:%d", peer_host_s, peer_port); - }else{ - sprintf(super_peer_host, "%s", peer_host_s); - } - php_url_free(url_info); - } - if( options == CURLOPT_HTTPHEADER ){ - char *url = NULL; - if( Z_TYPE_P(zvalue) != IS_ARRAY){ - return ; - } - if(strlen(super_peer_host) != 0){ - make_span_curl_header(super_peer_host, zvalue); - orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU); - is_send_curl_header = 1; - } - ZVAL_COPY(&super_curl_header, zvalue); - RETURN_TRUE ; - } - orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU); -} - void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS) { zval *zid; @@ -235,45 +130,61 @@ void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS) long millisecond; millisecond = zend_atol(l_millisecond, strlen(l_millisecond)); add_assoc_long(&temp, "startTime", millisecond); - add_assoc_long(&temp, "spanType", 0); + add_assoc_long(&temp, "spanType", 1); add_assoc_long(&temp, "spanLayer", 3); add_assoc_long(&temp, "componentId", COMPONENT_HTTPCLIENT); - php_printf("%s", generate_sw3(Z_LVAL_P(span_id) + 1, zend_string_init("111", sizeof("111") - 1, 1), zend_string_init("222", sizeof("222") - 1, 1))); - -// zval temp; -// -// zend_array *arr = (zend_array *) pemalloc(sizeof(zend_array), 1); -// Z_ARR_P(&temp) = arr; -// Z_TYPE_INFO_P(&temp) = IS_ARRAY_EX; -// zend_hash_init(Z_ARRVAL_P(&temp), 0, NULL, NULL, 0); -// -// add_assoc_string(&temp, "tst", get_millisecond()); -// add_next_index_string(&sky_segment_queue, sky_json_encode(&temp)); -// if(!is_send_curl_header && super_peer_host != NULL){ -// -// if(Z_TYPE(super_curl_header) == IS_UNDEF || Z_TYPE(super_curl_header) == IS_NULL){ -// array_init(&super_curl_header); -// } + zval function_name,curlInfo; + zval params[1]; + ZVAL_COPY(¶ms[0], zid); + ZVAL_STRING(&function_name, "curl_getinfo"); + call_user_function(CG(function_table), NULL, &function_name, &curlInfo, 1, params); + zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo), ZEND_STRL("url")); + + php_url *url_info = php_url_parse( Z_STRVAL_P(z_url) ); + char peer[200]; + char operation_name[500]; + + int peer_port = 0; + if(url_info->port){ + peer_port = url_info->port; + } + if(peer_port > 0){ + sprintf(peer, "%s:%d", url_info->host, peer_port); + }else{ + sprintf(peer, "%s", url_info->host); + } + + if (url_info->query) { + sprintf(operation_name, "%s?%s", url_info->path, url_info->query); + } else { + sprintf(operation_name, "%s", url_info->path); + } + + char *sw3 = generate_sw3(Z_LVAL_P(span_id) + 1, zend_string_init(peer, sizeof(peer) - 1, 1), zend_string_init(operation_name, sizeof(operation_name) - 1, 1)); + + if (sw3 != NULL) { + zval z_sw3; + array_init(&z_sw3); + char headers_string[500]; + sprintf(headers_string, "sw3: %s", sw3); + add_next_index_string(&z_sw3, headers_string); // //send setopt header -// zval function_name, params[3]; -// ZVAL_COPY(¶ms[0], zid); -// ZVAL_LONG(¶ms[1], CURLOPT_HTTPHEADER); -// ZVAL_COPY(¶ms[2], &super_curl_header); -// ZVAL_STRING(&function_name, "curl_setopt"); -// zval return_function_value; -// call_user_function(CG(function_table), NULL, &function_name, &return_function_value, 3, params); -// zval_ptr_dtor(¶ms[0]); -// zval_ptr_dtor(¶ms[2]); -// zval_ptr_dtor(&super_curl_header); -// } + zval f_name, p[3]; + ZVAL_COPY(&p[0], zid); + ZVAL_LONG(&p[1], CURLOPT_HTTPHEADER); + ZVAL_COPY(&p[2], &z_sw3); + ZVAL_STRING(&f_name, "curl_setopt"); + zval return_function_value; + call_user_function(CG(function_table), NULL, &f_name, &return_function_value, 3, p); + zval_ptr_dtor(&p[0]); + zval_ptr_dtor(&p[2]); + } + orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU); - zval function_name,curlInfo; - zval params[1]; - ZVAL_COPY(¶ms[0], zid); - ZVAL_STRING(&function_name, "curl_getinfo"); + call_user_function(CG(function_table), NULL, &function_name, &curlInfo, 1, params); zval_ptr_dtor(¶ms[0]); @@ -282,24 +193,11 @@ void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS) l_millisecond = get_millisecond(); millisecond = zend_atol(l_millisecond, strlen(l_millisecond)); efree(l_millisecond); - zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo), ZEND_STRL("url")); z_http_code = zend_hash_str_find(Z_ARRVAL(curlInfo), ZEND_STRL("http_code")); add_assoc_long(&temp, "endTime", millisecond); - php_url *url_info = php_url_parse( Z_STRVAL_P(z_url) ); - char peer[200]; - - int peer_port = 0; - if(url_info->port){ - peer_port = url_info->port; - } - if(peer_port > 0){ - sprintf(peer, "%s:%d%s", url_info->host, peer_port); - }else{ - sprintf(peer, "%s", url_info->host); - } add_assoc_string(&temp, "operationName", url_info->path); add_assoc_string(&temp, "peer", peer); @@ -320,13 +218,10 @@ void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS) /* Uncomment this function if you have INI entries*/ static void php_skywalking_init_globals(zend_skywalking_globals *skywalking_globals) { - skywalking_globals->global_app_code = NULL; - skywalking_globals->global_log_path = NULL; - skywalking_globals->global_sampling_rate = 99.22; - skywalking_globals->global_auto_open = 1; - skywalking_globals->global_send_type = 1; - skywalking_globals->global_header_client_ip_name = NULL; - skywalking_globals->global_app_grpc_trace = NULL; + skywalking_globals->app_code = NULL; + skywalking_globals->log_path = NULL; + skywalking_globals->enable = 1; + skywalking_globals->grpc = NULL; } @@ -335,7 +230,7 @@ static char *sky_json_encode(zval *parameter){ int return_code; smart_str buf = {0}; - zend_long options = 0; + zend_long options = 64; #if PHP_VERSION_ID >= 71000 return_code = php_json_encode(&buf, parameter, (int)options); if (return_code != SUCCESS) { @@ -349,259 +244,27 @@ static char *sky_json_encode(zval *parameter){ return ZSTR_VAL(buf.s); } -static void make_span_curl_header(char *peer_host, zval *headers) -{ - char *headers_string, *tmp_headers_string; - tmp_headers_string = build_SWheader_value(peer_host); - headers_string = (char *)emalloc(sizeof(char)*250); - sprintf(headers_string, "sw3: %s", tmp_headers_string); - add_next_index_string (headers, headers_string); - efree(headers_string); -} - - -static void in_queue_log(){ - - zval *all_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("all_node_data")); - sky_array_unshift(&glob_skywalking_globals_queue, all_node_data); -} - - -static void *empty_consumer_thread_entry(zval *param) { - - zval *operand; - zend_string *string_key; - zend_ulong num_key; - int g_SegTypeVal = 1500; - int num = 0; - - while (1) { - FILE *f = fopen("/tmp/teteet1.txt", "a"); - fprintf(f, "count%d\n", zend_hash_num_elements(Z_ARRVAL(sky_segment_queue))); - if(zend_hash_num_elements(Z_ARRVAL(sky_segment_queue)) > 0){ - - ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(sky_segment_queue), num_key, string_key, operand) { - fprintf(f, "queue_empty%s\n", Z_STRVAL_P(operand)); - -// send_grpc_param(operand); -// zend_hash_index_del(Z_ARRVAL(glob_skywalking_globals_queue), num_key); - } ZEND_HASH_FOREACH_END(); - - } - fclose(f); - usleep(1000 * g_SegTypeVal); - } - - return NULL; -} - -static void* send_grpc_param(zval *all_node_data) -{ - zval *z_unique_id = zend_hash_str_find(Z_ARRVAL_P(all_node_data), ZEND_STRL(SKYWALKING_DISTRIBUTED_TRACEIDS)); - zval *operand; - zend_string *string_key; - zend_ulong num_key; - - TraceSegmentObjectStruct* traceSegment = (TraceSegmentObjectStruct *)malloc(sizeof(TraceSegmentObjectStruct)); - char *results = sky_json_encode( all_node_data ); - - char str_id_parts[20]; - long id_parts; - comList* com_list = create_com_list(); - - if(zend_hash_num_elements(Z_ARRVAL_P(z_unique_id)) > 0){ - ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(z_unique_id), num_key, string_key, operand) { - UniqueIdStruct *unique_id = (UniqueIdStruct *)malloc(sizeof(UniqueIdStruct)); - sprintf(str_id_parts, "%s", Z_STRVAL_P(operand)); - id_parts = zend_atol(str_id_parts, strlen(str_id_parts)); - unique_id->idParts = id_parts; - add_com_list(com_list, unique_id); - } ZEND_HASH_FOREACH_END(); - } - traceSegment->traceSegmentIdList = com_list; - traceSegment->segment = results; - ParamDataStruct* paramData; - paramData = (ParamDataStruct *)emalloc(sizeof(ParamDataStruct)); - paramData->sendTraceSegmentParam = (SendTraceSegmentParamStruct *)emalloc(sizeof(SendTraceSegmentParamStruct)); - paramData->sendTraceSegmentParam->traceSegment = traceSegment; - int global_send_type = SKY_G(global_send_type) ; - if( global_send_type & S_SEND_TYPE_WRITE ){ - write_log( results); - } - if( global_send_type & S_SEND_TYPE_GRPC ){ - goSkyGrpc( SKY_G(global_app_grpc_trace), METHOD__SEND_TRACE_SEGMENT, paramData, NULL); - } - efree(paramData->sendTraceSegmentParam); - efree(paramData); - free(traceSegment); -} - - - -static void *write_log(char *text){ - char *log_path; - char logFilename[100]; - char message[strlen(text)]; - log_path = SKY_G(global_log_path); - - zend_string *date_fmt; - time_t t; - t = time(NULL); - date_fmt = php_format_date("YmdHi", sizeof("YmdHi") - 1, t, 1); - - bzero(logFilename, 100); - sprintf(logFilename, "%s/skywalking.%s.log", ZSTR_VAL(php_string_tolower(zend_string_init(log_path, strlen(log_path), 0))), ZSTR_VAL(date_fmt)); - - bzero(message, strlen(text)); - sprintf(message, "%s\n", text); - _php_error_log_ex(3, message, strlen(message) + 1, logFilename, NULL); -} - -/** - * 获取接收到 SWTraceContext 的 header -// // */ -static zval receive_SWHeader_from_caller() -{ - zend_bool jit_initialization = PG(auto_globals_jit); - zval *ret; - zval sw_header_info; - array_init(&sw_header_info); - if (jit_initialization) { - zend_string *server_str = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0); - zend_is_auto_global(server_str); - zend_string_release(server_str); - } +static void write_log(char *text){ + if(application_instance != -100000) { + char *log_path; + char logFilename[100]; + char message[strlen(text) + 1]; + log_path = SKY_G(log_path); - if ((ret = zend_hash_str_find(Z_ARRVAL_P(zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"))), ZEND_STRL("HTTP_SWTRACECONTEXT"))) != NULL) { - zend_string *sw_header_text = zend_string_copy(Z_STR_P(ret)); - zval sw_header_info_z; - zval *operand; - zend_string *string_key; - zend_ulong num_key; - array_init(&sw_header_info_z); + zend_string *date_fmt; + time_t t; + t = time(NULL); + date_fmt = php_format_date("YmdHi", sizeof("YmdHi") - 1, t, 1); - zend_string *delim = zend_string_init(ZEND_STRL("|"), 0); - php_explode(delim, sw_header_text, &sw_header_info_z, 10); + bzero(logFilename, 100); + sprintf(logFilename, "%s/skywalking.%s.log", ZSTR_VAL(php_string_tolower(zend_string_init(log_path, strlen(log_path), 0))), ZSTR_VAL(date_fmt)); + bzero(message, strlen(text)); + sprintf(message, "%s\n", text); + _php_error_log_ex(3, message, strlen(message), logFilename, NULL); + } - - char *sw_header_info_keys[6] = {"TraceId", "SpanId", "AppCode", "PeerHost", "DistributedTraceIds", "IsSample"}; - ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(&sw_header_info_z), num_key, string_key, operand) { - zend_hash_str_update(Z_ARRVAL(sw_header_info), sw_header_info_keys[num_key], strlen(sw_header_info_keys[num_key]), operand); - } ZEND_HASH_FOREACH_END(); - } - - add_assoc_zval(&glob_skywalking_globals_list, "_swHeaderInfo", &sw_header_info); - return sw_header_info; - -} - -static void set_sampling_rate(double degrees){ - - - degrees = _php_math_round(degrees, 2, PHP_ROUND_HALF_UP); - percent_int = (int)(degrees*100); - if( percent_int <= 0){ - percent_int = 100; - } - if(percent_int > 10000){ - percent_int = 10000; - } - -} - -static zval* generate_trace_id_for_array(int use_parent_tid, zval* z_trace_id){ - - char *trace_id = generate_trace_id(); - if(use_parent_tid){ - trace_id = generate_parent_trace_id(); - }else{ - trace_id = generate_trace_id(); - } - - - php_explode(zend_string_init(ZEND_STRL("."), 0), zend_string_init(trace_id, strlen(trace_id), 0), z_trace_id, 10); - return z_trace_id; -} - -static char *generate_parent_info_from_header(char *header_name){ - zval *ret; - zval *sw_header_info; - if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){ - if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), header_name, sizeof(header_name) - 1)) != NULL) { - return Z_STRVAL_P(ret); - } - } - return NULL; -} - -static int have_parent_info_from_header(){ - zval *sw_header_info; - if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){ - if(zend_hash_num_elements(Z_ARRVAL_P(sw_header_info)) > 0){ - return 1; - } - } - return 0; -} - -static char *generate_parent_trace_id(){ - zval *ret; - zval *sw_header_info; - if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){ - if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), "TraceId", sizeof("TraceId") - 1)) != NULL) { - return Z_STRVAL_P(ret); - } - } -} - -static char *generate_trace_id(){ - zval *sw_header_info, z__trace_id; - zval *ret; - char *_trace_id; - _trace_id = (char *)emalloc(sizeof(char)*180); - bzero(_trace_id, 180); - if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){ - if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), "TraceId", sizeof("TraceId") - 1)) != NULL) { - zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_traceId", sizeof("_traceId") - 1, ret); - return Z_STRVAL_P(ret); - } - } - if ((ret = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_traceId", sizeof("_traceId") - 1)) != NULL) { - return Z_STRVAL_P(ret); - } -// _trace_id = make_trace_id(); - ZVAL_STRING(&z__trace_id, _trace_id); - zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_traceId", sizeof("_traceId") - 1, &z__trace_id); - - return _trace_id; - -} - - -static char *generate_distributed_trace_ids(){ - zval *sw_header_info; - zval *ret; - if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){ - if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), "DistributedTraceIds", sizeof("DistributedTraceIds") - 1)) != NULL) { - return Z_STRVAL_P(ret); - } - } - if ((ret = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "DistributedTraceIds", sizeof("DistributedTraceIds") - 1)) != NULL) { - if(Z_TYPE_P(ret) != IS_NULL){ - return Z_STRVAL_P(ret); - } - } - - - char *_distributed_trace_ids; - _distributed_trace_ids = (char *)emalloc(sizeof(char)*180); - bzero(_distributed_trace_ids, 180); -// _distributed_trace_ids = make_trace_id(); - add_assoc_string(&glob_skywalking_globals_list, "DistributedTraceIds", _distributed_trace_ids); - - return _distributed_trace_ids; } @@ -610,45 +273,45 @@ static char *generate_sw3(zend_long span_id, zend_string *peer_host, zend_string char *sw3 = (char *) emalloc(sizeof(char) * 180 + ZSTR_LEN(peer_host) + ZSTR_LEN(operation_name)); zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1); - zval *parentApplicationInstance = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentApplicationInstance", - sizeof("parentApplicationInstance") - 1); zval *entryApplicationInstance = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "entryApplicationInstance", sizeof("entryApplicationInstance") - 1); - zval *currentOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "operationName", - sizeof("operationName") - 1); + zval *entryOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "entryOperationName", + sizeof("entryOperationName") - 1); zval *distributedTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "distributedTraceId", sizeof("distributedTraceId") - 1); sprintf(sw3, "%s|%d|%d|%d|#%s|#%s|#%s|%s", Z_STRVAL_P(traceId), span_id, - Z_LVAL_P(parentApplicationInstance), Z_LVAL_P(entryApplicationInstance), ZSTR_VAL(peer_host), Z_STRVAL_P(currentOperationName), ZSTR_VAL(operation_name), Z_STRVAL_P(distributedTraceId)); + application_instance, Z_LVAL_P(entryApplicationInstance), ZSTR_VAL(peer_host), Z_STRVAL_P(entryOperationName), ZSTR_VAL(operation_name), Z_STRVAL_P(distributedTraceId)); + php_printf("%s", sw3); return sw3; } -static void make_trace_id() { - int sys_pid = getpid(); - char *millisecond = get_millisecond(); - char *makeTraceId; - makeTraceId = (char *) emalloc(sizeof(char) * 180); +static void generate_context() { + int sys_pid = getpid(); + long second = get_second(); + second = second * 10000 + sky_increment_id; + char *makeTraceId; + makeTraceId = (char *) emalloc(sizeof(char) * 180); - bzero(makeTraceId, 80); + bzero(makeTraceId, 80); - sprintf(makeTraceId, "%d.%d.%s%d", 0, sys_pid, millisecond, sky_increment_id); + sprintf(makeTraceId, "%d.%d.%ld", application_instance, sys_pid, second); - add_assoc_string(&SKYWALKING_G(context), "currentTraceId", makeTraceId); + add_assoc_string(&SKYWALKING_G(context), "currentTraceId", makeTraceId); - // parent - zval *carrier = NULL; - zval *sw3; + // parent + zval *carrier = NULL; + zval *sw3; - zend_bool jit_initialization = PG(auto_globals_jit); + zend_bool jit_initialization = PG(auto_globals_jit); - if (jit_initialization) { - zend_string *server_str = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0); - zend_is_auto_global(server_str); - zend_string_release(server_str); - } + if (jit_initialization) { + zend_string *server_str = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0); + zend_is_auto_global(server_str); + zend_string_release(server_str); + } carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER")); - sw3 = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW3", sizeof("HTTP_SW3") - 1); + sw3 = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW3", sizeof("HTTP_SW3") - 1); if (sw3 != NULL && Z_TYPE_P(sw3) == IS_STRING) { add_assoc_string(&SKYWALKING_G(context), "sw3", Z_STRVAL_P(sw3)); @@ -658,19 +321,34 @@ static void make_trace_id() { php_explode(zend_string_init(ZEND_STRL("|"), 0), Z_STR_P(sw3), &temp, 10); + add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", + zend_atol(Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 2)), + sizeof(Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 2))) - 1)); add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", - Z_LVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 3))); - add_assoc_string(&SKYWALKING_G(context), "firstOperationName", + zend_atol(Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 3)), + sizeof(Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 3))) - 1)); + add_assoc_string(&SKYWALKING_G(context), "entryOperationName", Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 5))); add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 7))); } else { add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance); add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance); + add_assoc_string(&SKYWALKING_G(context), "entryOperationName", get_page_request_uri()); add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId); } } + +static long get_second() { + struct timeval tv; + gettimeofday(&tv,NULL); + char *buffer; + buffer = (char *)emalloc(sizeof(char)*20); + bzero(buffer, 20); + return tv.tv_sec; +} + static char *get_millisecond(){ struct timeval tv; gettimeofday(&tv,NULL); @@ -711,196 +389,9 @@ static char *get_page_request_uri() { return ZSTR_VAL(uri.s); } -static char *get_page_url_and_peer(){ - - zval *carrier = NULL; - zval *s_https,*http_host, *request_uri; - - smart_str uri = {0}; - - zend_bool jit_initialization = PG(auto_globals_jit); - - if (jit_initialization) { - zend_string *server_str = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0); - zend_is_auto_global(server_str); - zend_string_release(server_str); - } - - if (strcasecmp("cli", sapi_module.name) == 0){ - smart_str_appendl(&uri, "cli", strlen("cli")); - }else{ - smart_str_appendl(&uri, "http", strlen("http")); - s_https = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTPS", sizeof("HTTPS") - 1); - - if(s_https != NULL && Z_STRVAL_P(s_https) == "on"){ - smart_str_appendc(&uri, 's'); - } - http_host = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_HOST", sizeof("HTTP_HOST") - 1); - smart_str_appendl(&uri, "://", strlen("://")); - smart_str_appendl(&uri, Z_STRVAL_P(http_host), strlen(Z_STRVAL_P(http_host))); - request_uri = zend_hash_str_find(Z_ARRVAL_P(carrier), "REQUEST_URI", sizeof("REQUEST_URI") - 1); - smart_str_appendl(&uri, Z_STRVAL_P(request_uri), strlen(Z_STRVAL_P(request_uri))); - } - smart_str_0(&uri); - return ZSTR_VAL(uri.s); -} - - -static char *build_SWheader_value(const char *peer_host){ - - char *trace_id, *app_code, *SW_header, *distributed_trace_ids; - int span_id, parent_app_instance_id, entry_app_instance_id, entry_appname_operation_id, parent_appname_operation_id; - char c_peer_host[55] ; - //zval SW_trace_context; - SW_header = (char *)emalloc(sizeof(char)*250); - trace_id = generate_trace_id(); - span_id = generate_span_id(); - parent_app_instance_id = get_app_instance_id(); - entry_app_instance_id = _entry_app_instance_id(); - app_code = SKY_G(global_app_code); - sprintf(c_peer_host, "#%s", peer_host); - entry_appname_operation_id = _entry_app_name_operation_id(); - parent_appname_operation_id = _parent_appname_operation_id(); - distributed_trace_ids = generate_distributed_trace_ids(); - - - sprintf(SW_header, "%s|%d|%d|%d|%s|%d|%d|%s", - trace_id, span_id, parent_app_instance_id, entry_app_instance_id, - c_peer_host, entry_appname_operation_id, parent_appname_operation_id, distributed_trace_ids - ); - - - return SW_header; -} - -static long generate_span_id(){ - - long span_id; - zval *swheaderinfo = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1); - if (swheaderinfo == NULL || zend_hash_num_elements(Z_ARRVAL_P(swheaderinfo)) == 0){ - span_id = 1; - }else{ - span_id = Z_LVAL_P(zend_hash_str_find(Z_ARRVAL_P(swheaderinfo), "SpanId", sizeof("SpanId") - 1)); - } - - if(!span_id){ - span_id = 1; - } - zval z_span_id; - ZVAL_LONG(&z_span_id, ++span_id); - zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_spanID", sizeof("_spanID") - 1, &z_span_id); - - return span_id; -} - -static zend_always_inline zend_uchar is_sampling(){ - - zval *z_p_is_sampling , sampling_rate; - int is_sampling_int; - - zend_long rand_val; - - z_p_is_sampling = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_isSampling", sizeof("_isSampling") - 1); - - if( z_p_is_sampling == NULL || ZVAL_IS_NULL(z_p_is_sampling) ){ - is_sampling_int = 0; - - ZVAL_LONG(&sampling_rate, percent_int); - - if(Z_LVAL(sampling_rate) < 10000){ -#if PHP_VERSION_ID >= 71000 - rand_val = php_mt_rand_common(1, 10000); -#else - if (!BG(mt_rand_is_seeded)) { - php_mt_srand(GENERATE_SEED()); - } - rand_val = (zend_long) (php_mt_rand() >> 1); - RAND_RANGE(rand_val, 1, 10000, PHP_MT_RAND_MAX); -#endif - if(Z_LVAL(sampling_rate) >= rand_val){ - is_sampling_int = 1; - } - } - zval z_readonly; - ZVAL_BOOL(&z_readonly, is_sampling_int); - - zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_isSampling", sizeof("_isSampling") - 1, &z_readonly); - return Z_TYPE(z_readonly); - } - return Z_TYPE_P(z_p_is_sampling); -} - -static char* _entry_app_name(){ - if(have_parent_info_from_header()){ - return generate_parent_info_from_header("EntryAppname"); - }else{ - return SKY_G(global_app_code); - } -} - -static int _entry_app_instance_id(){ - if(have_parent_info_from_header()){ - char *parent_app_instance_id = generate_parent_info_from_header("EntryAppInstanceid"); - - return zend_atoi(parent_app_instance_id, (int)strlen(parent_app_instance_id)); - }else{ - return get_app_instance_id(); - } -} -static long _entry_app_name_operation_id(){ - if(have_parent_info_from_header()){ - char *app_name_operation_id = generate_parent_info_from_header("EntryAppnameOperationId"); - - return zend_atol(app_name_operation_id, (int)strlen(app_name_operation_id)); - }else{ - zval *span_first_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("span_first_node_data")); - return Z_LVAL_P(zend_hash_str_find(Z_ARRVAL_P(span_first_node_data), ZEND_STRL(SKYWALKING_SPAN_ID))); - } -} - -static long _parent_appname_operation_id(){ - zval *span_first_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("span_first_node_data")); - return Z_LVAL_P(zend_hash_str_find(Z_ARRVAL_P(span_first_node_data), ZEND_STRL(SKYWALKING_SPAN_ID))); -} - - - - -void set_app_instance_id( AppInstance* appInste ){ - if( appInste != NULL ){ - appInstance.applicationInstanceId = appInste->applicationInstanceId; - } - -} -void set_app_id( AppInstance* appInste ){ - if( appInste != NULL ){ - appInstance.applicationId = appInste->applicationId; - } -} - -static int get_app_instance_id(){ - return appInstance.applicationInstanceId; -} -static int get_app_id(){ - return appInstance.applicationId; -} - -static int is_auto_open(){ - if(sky_close){ - return 0; - } - if((strcasecmp("cli", sapi_module.name) != 0) && (SG(sapi_headers).http_response_code != 200)){ - return 0; - } - if(SKY_G(global_auto_open)){ - return 1; - } - return 0; -} - /** - * 当前机器ip + * ip * * @since 2017年11月23日 * @copyright @@ -931,59 +422,26 @@ static char* _get_current_machine_ip(){ return ip; } -static zval* get_father_node_data(zval* father_nodes_data){ - - - - if(!have_parent_info_from_header()){ - return NULL; - } - - zval z_trace_id; - array_init(&z_trace_id); - add_assoc_zval(father_nodes_data, SKYWALKING_PARENT_TRACE_SEGMENT_ID, generate_trace_id_for_array(USE_PARENT_TRACE_ID, &z_trace_id)); - - char *parent_app_instance_id = generate_parent_info_from_header("ParentAppInstanceid"); - add_assoc_long(father_nodes_data, SKYWALKING_PARENT_APPLICATION_ID, zend_atoi(parent_app_instance_id, strlen(parent_app_instance_id))); - - char *span_id = generate_parent_info_from_header("SpanId"); - add_assoc_long(father_nodes_data, SKYWALKING_PARENT_SPAN_ID, zend_atoi(span_id, strlen(span_id))); - add_assoc_string(father_nodes_data, SKYWALKING_PARENT_SERVICE_ID, generate_parent_info_from_header("ParentAppname")); - add_assoc_string(father_nodes_data, SKYWALKING_PARENT_SERVICE_NAME, generate_parent_info_from_header("ParentAppname")); - add_assoc_string(father_nodes_data, SKYWALKING_NETWORK_ADDRESS_ID, generate_parent_info_from_header("PeerHost")); - add_assoc_string(father_nodes_data, SKYWALKING_NETWORK_ADDRESS, generate_parent_info_from_header("PeerHost")); - add_assoc_string(father_nodes_data, SKYWALKING_PARENT_SERVICE_ID, generate_parent_info_from_header("ParentAppname")); - add_assoc_string(father_nodes_data, SKYWALKING_ENTRY_APPLICATION_INSTANCE_ID, generate_parent_info_from_header("EntryAppInstanceid")); - char *entry_app_id = generate_parent_info_from_header("EntryAppId"); - add_assoc_long(father_nodes_data, SKYWALKING_ENTRY_SERVICE_ID, zend_atoi(entry_app_id, strlen(entry_app_id))); - add_assoc_string(father_nodes_data, SKYWALKING_ENTRY_SERVICE_NAME, generate_parent_info_from_header("EntryAppname")); - add_assoc_long(father_nodes_data, SKYWALKING_REF_TYPE_VALUE, 0); - - return father_nodes_data; -} - -static zval *set_span_nodes_data(zval *node_data){ - zval *spans_node_data; - spans_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("_spansNodeData")); - zend_hash_next_index_insert(Z_ARRVAL_P(spans_node_data), node_data); - zval_ptr_dtor(node_data); -} - static void request_init() { array_init(&SKYWALKING_G(context)); array_init(&SKYWALKING_G(UpstreamSegment)); - make_trace_id(); + generate_context(); + add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_instance", application_instance); + add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_id", application_id); SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "segment"); + SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "globalTraceIds"); + + zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1); zval traceSegmentObject; zval spans; array_init(&spans); array_init(&traceSegmentObject); - add_assoc_string(&traceSegmentObject, "traceSegmentId", "todo"); - add_assoc_string(&traceSegmentObject, "isSizeLimited", "todo"); + add_assoc_string(&traceSegmentObject, "traceSegmentId", Z_STRVAL_P(traceId)); + add_assoc_long(&traceSegmentObject, "isSizeLimited", 0); zval temp; array_init(&temp); @@ -995,7 +453,6 @@ static void request_init() { efree(l_millisecond); add_assoc_long(&temp, "startTime", millisecond); add_assoc_string(&temp, "operationName", get_page_request_uri()); - add_assoc_string(&SKYWALKING_G(context), "firstOperationName", get_page_request_uri()); // add_assoc_string(&sky_entry_span, "peer", "127.0.0.1"); add_assoc_long(&temp, "spanType", 0); add_assoc_long(&temp, "spanLayer", 3); @@ -1005,98 +462,19 @@ static void request_init() { add_assoc_zval(&traceSegmentObject, "spans", &spans); - zend_hash_str_update(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "segment", sizeof("segment") - 1, &traceSegmentObject); -} - - -static void start_node_span_of_curl(){ - - zval span_node_data, * z_span_id; - array_init(&span_node_data); - zval *span_first_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("span_first_node_data")); - - add_assoc_long(&span_node_data, SKYWALKING_SPAN_ID, generate_span_id()); - add_assoc_long(&span_node_data, SKYWALKING_FATHER_SPAN_ID, Z_LVAL_P(zend_hash_str_find(Z_ARRVAL_P(span_first_node_data), ZEND_STRL(SKYWALKING_SPAN_ID)))); - - char *l_millisecond = get_millisecond(); - long starttime = zend_atol(l_millisecond, strlen(l_millisecond)); - add_assoc_long(&span_node_data, SKYWALKING_STARTTIME, starttime); - //add_assoc_string(&span_node_data, SKYWALKING_COMPONENT_ID, "php-server"); - add_assoc_string(&span_node_data, SKYWALKING_COMPONENT_NAME, "php-curl"); - add_assoc_long(&span_node_data, SKYWALKING_SPAN_TYPE_VALUE, 1); - add_assoc_long(&span_node_data, SKYWALKING_SPAN_LAYER_VALUE, 3); - - zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_span_node_data", sizeof("_span_node_data") - 1, &span_node_data); -} - -static void end_node_span_of_curl(zval *curl){ - - - zval *span_node_data; - span_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("_span_node_data")); - - zval function_name,curlInfo; - zval params[1]; - ZVAL_COPY(¶ms[0], curl); - ZVAL_STRING(&function_name, "curl_getinfo"); - call_user_function(CG(function_table), NULL, &function_name, &curlInfo, 1, params); - zval_ptr_dtor(¶ms[0]); - - - zval *span_string_param, *span_first_node_data, *span_int_param, *span_bool_param,null_oject; - zval *z_http_code; - long millisecond; - char *l_millisecond = get_millisecond(); - millisecond = zend_atol(l_millisecond, strlen(l_millisecond)); - - zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo), ZEND_STRL("url")); - - z_http_code = zend_hash_str_find(Z_ARRVAL(curlInfo), ZEND_STRL("http_code")); - zval t_tags, t_tags_tmp; - array_init(&t_tags); - array_init(&t_tags_tmp); - - zval z_url_k; - ZVAL_STRING(&z_url_k, "url"); - zend_hash_str_update(Z_ARRVAL(t_tags_tmp), "k", sizeof("k") - 1, &z_url_k); - zend_hash_str_update(Z_ARRVAL(t_tags_tmp), "v", sizeof("v") - 1, z_url); - add_index_zval(&t_tags, 0, &t_tags_tmp); - - add_assoc_long(span_node_data, SKYWALKING_ENDTIME, millisecond); - - php_url *url_info = php_url_parse( Z_STRVAL_P(z_url) ); - char peer_operation[200]; - - int peer_port = 0; - if(url_info->port){ - peer_port = url_info->port; - } - if(peer_port > 0){ - sprintf(peer_operation, "%s:%d%s", url_info->host, peer_port, url_info->path); - }else{ - sprintf(peer_operation, "%s%s", url_info->host, url_info->path); - } - php_url_free(url_info); - - //add_assoc_string(&span_node_data, SKYWALKING_PEER_ID, "server"); - add_assoc_string(span_node_data, SKYWALKING_PEER, super_peer_host); - add_assoc_string(span_node_data, SKYWALKING_OPERATION_NAME, peer_operation); - SKY_ADD_ASSOC_ZVAL(span_node_data, SKYWALKING_LOGS); - //add_assoc_string(span_node_data, SKYWALKING_OPERATION_NAME_ID, z_url); - - if( Z_LVAL_P(z_http_code) !=200 ){ - add_assoc_bool(span_node_data, SKYWALKING_IS_ERROR, 1); - }else{ - add_assoc_bool(span_node_data, SKYWALKING_IS_ERROR, 0); - } - - add_assoc_zval(span_node_data, SKYWALKING_TAGS, &t_tags); - set_span_nodes_data(span_node_data); + zval globalTraceIds; + array_init(&globalTraceIds); + zval tmpGlobalTraceIds; + array_init(&tmpGlobalTraceIds); + ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(traceId)); + zend_hash_next_index_insert(Z_ARRVAL(globalTraceIds), &tmpGlobalTraceIds); + zend_hash_str_update(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "segment", sizeof("segment") - 1, &traceSegmentObject); + zend_hash_str_update(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "globalTraceIds", sizeof("globalTraceIds") - 1, &globalTraceIds); } -static void *sky_flush_all() { +static void sky_flush_all() { char *l_millisecond = get_millisecond(); long millisecond = zend_atol(l_millisecond, strlen(l_millisecond)); efree(l_millisecond); @@ -1129,65 +507,35 @@ static zval *get_spans() { static void module_init() { -// zend_array *arr = (zend_array *) pemalloc(sizeof(zend_array), 1); -// Z_ARR_P(&sky_segment_queue) = arr; -// Z_TYPE_INFO_P(&sky_segment_queue) = IS_ARRAY_EX; -// zend_hash_init(Z_ARRVAL_P(&sky_segment_queue), 0, NULL, NULL, 0); -// -// SKYWALKING_G(shm_id) = shmget((key_t)SHARED_MEMORY_KEY, 4096, 0666 | IPC_CREAT); -// -// if (SKYWALKING_G(shm_id) == -1) { -// return; -// } - - - - appInstance.applicationInstanceId = -100000; - appInstance.applicationId = -100000; -// if (pthread_create(&SKYWALKING_G(pid_consumer), NULL, (void *) empty_consumer_thread_entry, NULL) < 0) { -// sky_close = 1; -// return; -// } - - return; - char* app_code = SKY_G(global_app_code); - char* ipv4s = _get_current_machine_ip(); - char agentUUID[80]; - ParamDataStruct* paramData; - paramData = (ParamDataStruct *)emalloc(sizeof(ParamDataStruct)); - paramData->registerParam = (RegisterParamtruct *)emalloc(sizeof(RegisterParamtruct)); - paramData->registerParam->appCode = app_code; - int stu = goSkyGrpc( SKY_G(global_app_grpc_trace), METHOD__GO_REGISTER, paramData, set_app_id); - if( stu == 0 || appInstance.applicationId == -100000){ - sky_close = 1; - return ; - } - strcat(agentUUID, app_code); - strcat(agentUUID, ipv4s); - - paramData->registerInstanceParam = (RegisterInstanceParamtruct *)emalloc(sizeof(RegisterInstanceParamtruct)); - paramData->registerInstanceParam->agentUUID = agentUUID; - paramData->registerInstanceParam->osName = SKY_OS_NAME; - char hostname[100] = {0}; - if(gethostname(hostname, sizeof(hostname)) < 0){ - strcpy(hostname, ""); + application_instance = -100000; + application_id = -100000; + application_id = applicationCodeRegister(SKYWALKING_G(grpc), SKYWALKING_G(app_code)); + + if (application_id == -100000) { + sky_close = 1; + return; } - paramData->registerInstanceParam->ipv4s = ipv4s; - char *l_millisecond = get_millisecond(); - paramData->registerInstanceParam->registerTime = zend_atol(l_millisecond, strlen(l_millisecond)); - paramData->registerInstanceParam->processNo = getpid(); - paramData->registerInstanceParam->hostname = hostname; - paramData->registerInstanceParam->applicationId = appInstance.applicationId; + char *ipv4s = _get_current_machine_ip(); + char uuid[80]; + strcat(uuid, SKYWALKING_G(app_code)); + strcat(uuid, ipv4s); - stu = goSkyGrpc( SKY_G(global_app_grpc_trace), METHOD__GO_REGISTER_INSTANCE, paramData, set_app_instance_id); - if( stu == 0 || appInstance.applicationInstanceId == -100000){ - //close skywalking - sky_close = 1; - return ; - } - efree( paramData->registerParam ); - efree( paramData ); + char hostname[100] = {0}; + if (gethostname(hostname, sizeof(hostname)) < 0) { + strcpy(hostname, ""); + } + + char *l_millisecond = get_millisecond(); + long millisecond = zend_atol(l_millisecond, strlen(l_millisecond)); + efree(l_millisecond); + application_instance = registerInstance(SKYWALKING_G(grpc), application_id, millisecond, uuid, SKY_OS_NAME, hostname, getpid(), + ipv4s); + + if (application_id == -100000) { + sky_close = 1; + return; + } } @@ -1195,15 +543,12 @@ static void module_init() { */ PHP_MINIT_FUNCTION (skywalking) { ZEND_INIT_MODULE_GLOBALS(skywalking, php_skywalking_init_globals, NULL); - if (sky_close) { - return SUCCESS; - } //data_register_hashtable(); REGISTER_INI_ENTRIES(); /* If you have INI entries, uncomment these lines */ - if (SKY_G(global_auto_open)) { + if (SKYWALKING_G(enable)) { module_init(); if (sky_close == 1) { return SUCCESS; @@ -1228,9 +573,6 @@ PHP_MSHUTDOWN_FUNCTION(skywalking) /* uncomment this line if you have INI entries */ - FILE *f = fopen("/tmp/teteet1.txt", "a"); - fprintf(f, "mclose%s\n", "ok"); - fclose(f); return SUCCESS; } /* }}} */ @@ -1244,14 +586,13 @@ PHP_RINIT_FUNCTION(skywalking) #if defined(COMPILE_DL_SKYWALKING) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif - //&& is_sampling() == IS_TRUE -// if( is_auto_open() ){ -// sky_increment_id ++; -// if(sky_increment_id >= 999999){ -// sky_increment_id = 0; -// } + if (SKYWALKING_G(enable)) { + sky_increment_id++; + if (sky_increment_id >= 9999) { + sky_increment_id = 0; + } request_init(); -// } + } return SUCCESS; } @@ -1263,14 +604,9 @@ PHP_RINIT_FUNCTION(skywalking) PHP_RSHUTDOWN_FUNCTION(skywalking) { - if(is_auto_open()){ + if(SKYWALKING_G(enable)){ sky_flush_all(); } - php_var_dump(&SKYWALKING_G(context), 0); -// php_var_dump(&sky_segment_queue, 0); -// is_send_curl_header = 0; -// zval_ptr_dtor(&super_curl_header); -// array_init(&super_curl_header); return SUCCESS; } @@ -1292,16 +628,6 @@ PHP_MINFO_FUNCTION(skywalking) /* }}} */ -/** {{{ PHP_GINIT_FUNCTION - -PHP_GINIT_FUNCTION(skywalking_globals) -{ - memset(skywalking_globals, 0, sizeof(*skywalking_globals)); -} - }}} */ - - - /* {{{ skywalking_module_entry */ zend_module_entry skywalking_module_entry = { @@ -1318,115 +644,6 @@ zend_module_entry skywalking_module_entry = { }; /* }}} */ - - -/******************************************** transplantThe coming code *****************************************/ - -static comList* create_com_list(){ - comList *com_list = (comList *)malloc(sizeof(comList)); - memset(com_list, 0, sizeof(comList)); - com_list->count = 0 ; - com_list->head = NULL; - com_list->tail = NULL; - - return com_list; -} - -static int add_com_list(comList *com_list, UniqueIdStruct* data){ - - comListNode *com_list_node = (comListNode *)malloc(sizeof(comListNode));; - com_list_node->next = NULL; - com_list_node->data = data; - com_list->count ++; - if(com_list->head == NULL){ - com_list->head = com_list_node; - }else{ - com_list->tail->next = com_list_node; - } - com_list->tail = com_list_node; - return 1; -} - -static int sky_live_pthread(pthread_t tid) -{ - - int pthread_kill_err; - - pthread_kill_err = pthread_kill(tid, 0); - if(pthread_kill_err == ESRCH){ - return 0; - } else if(pthread_kill_err == EINVAL){ - return 0; - } else { - return 1; - } -} - -static long sky_array_unshift(zval *stack, zval *var) -{ - //7.0之前 - //php_splice(Z_ARRVAL_P(stack), 0, 0, args, argc, NULL TSRMLS_CC); - HashTable new_hash; /* New hashtable for the stack */ - - zend_string *key; - zval *value; - - - zend_hash_init(&new_hash, zend_hash_num_elements(Z_ARRVAL_P(stack)) + 1, NULL, ZVAL_PTR_DTOR, 0); - - zend_hash_next_index_insert_new(&new_hash, var); - if (EXPECTED(Z_ARRVAL_P(stack)->u.v.nIteratorsCount == 0)) { - ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(stack), key, value) { - if (key) { - zend_hash_add_new(&new_hash, key, value); - } else { - zend_hash_next_index_insert_new(&new_hash, value); - } - } ZEND_HASH_FOREACH_END(); - } else { - uint32_t old_idx; - uint32_t new_idx = 0; - uint32_t iter_pos = zend_hash_iterators_lower_pos(Z_ARRVAL_P(stack), 0); - - ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(stack), key, value) { - if (key) { - zend_hash_add_new(&new_hash, key, value); - } else { - zend_hash_next_index_insert_new(&new_hash, value); - } - old_idx = (Bucket*)value - Z_ARRVAL_P(stack)->arData; - if (old_idx == iter_pos) { - zend_hash_iterators_update(Z_ARRVAL_P(stack), old_idx, new_idx); - iter_pos = zend_hash_iterators_lower_pos(Z_ARRVAL_P(stack), iter_pos + 1); - } - new_idx++; - } ZEND_HASH_FOREACH_END(); - } - - - Z_ARRVAL_P(stack)->u.v.nIteratorsCount = 0; - Z_ARRVAL_P(stack)->pDestructor = NULL; - zend_hash_destroy(Z_ARRVAL_P(stack)); - - Z_ARRVAL_P(stack)->u.v.flags = new_hash.u.v.flags; - Z_ARRVAL_P(stack)->nTableSize = new_hash.nTableSize; - Z_ARRVAL_P(stack)->nTableMask = new_hash.nTableMask; - Z_ARRVAL_P(stack)->nNumUsed = new_hash.nNumUsed; - Z_ARRVAL_P(stack)->nNumOfElements = new_hash.nNumOfElements; - Z_ARRVAL_P(stack)->nNextFreeElement = new_hash.nNextFreeElement; - Z_ARRVAL_P(stack)->arData = new_hash.arData; - Z_ARRVAL_P(stack)->pDestructor = new_hash.pDestructor; - - zend_hash_internal_pointer_reset(Z_ARRVAL_P(stack)); - - return zend_hash_num_elements(Z_ARRVAL_P(stack)); -} -// /* }}} */ - - - - - #ifdef COMPILE_DL_SKYWALKING #ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() diff --git a/sky-php7ext/skywalking.php b/sky-php7ext/skywalking.php index e0dffe0f718bf201580bebd60d12cdd424db721c..4b75b38cbcee3826569bbe586d50d5b6c8e0cfeb 100644 --- a/sky-php7ext/skywalking.php +++ b/sky-php7ext/skywalking.php @@ -1,5 +1,8 @@ fwrite("124124"); diff --git a/sky-php7ext/src/greeter_client.cc b/sky-php7ext/src/greeter_client.cc new file mode 100644 index 0000000000000000000000000000000000000000..edc17c29f529ed4662bbd3fea116629a86b87621 --- /dev/null +++ b/sky-php7ext/src/greeter_client.cc @@ -0,0 +1,124 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include + +#include + +#include "grpc/ApplicationRegisterService.grpc.pb.h" +#include "grpc/DiscoveryService.grpc.pb.h" +#include "grpc/TraceSegmentService.grpc.pb.h" + +using grpc::Channel; +using grpc::ClientContext; +using grpc::Status; +using grpc::ClientReader; +using grpc::ClientReaderWriter; +using grpc::ClientWriter; + + +extern "C" int applicationCodeRegister(char *grpc_server, char *code); + +extern "C" int +registerInstance(char *grpc_server, int appId, long registertime, char *uuid, char *osname, char *hostname, + int processno, char *ipv4s); + +class GreeterClient { +public: + GreeterClient(std::shared_ptr channel) { + channel_ = channel; + } + + int applicationCodeRegister(const std::string &code) { + + std::unique_ptr stub_; + stub_ = ApplicationRegisterService::NewStub(channel_); + + Application request; + + request.set_applicationcode(code); + + ApplicationMapping reply; + + ClientContext context; + + Status status = stub_->applicationCodeRegister(&context, request, &reply); + + if (status.ok()) { + return reply.application().value(); + } + + return -100000; + } + + int registerInstance(int applicationid, int registertime, char *uuid, char *osname, char *hostname, int processno, + char *ipv4s) { + + std::unique_ptr stub_; + stub_ = InstanceDiscoveryService::NewStub(channel_); + + ApplicationInstance request; + + request.set_agentuuid(uuid); + request.set_applicationid(applicationid); + request.set_registertime(registertime); + + + OSInfo *osInfo = new OSInfo; + request.set_allocated_osinfo(osInfo); + osInfo->set_osname(osname); + osInfo->set_hostname(hostname); + osInfo->set_processno(processno); + osInfo->add_ipv4s(ipv4s); + + + ApplicationInstanceMapping reply; + + ClientContext context; + + Status status = stub_->registerInstance(&context, request, &reply); + + if (status.ok()) { + return reply.applicationinstanceid(); + } + + return -1; + + } + +private: + std::shared_ptr channel_; +}; + + +int applicationCodeRegister(char *grpc_server, char *code) { + GreeterClient greeter(grpc::CreateChannel(grpc_server, grpc::InsecureChannelCredentials())); + + std::string c(code); +// + return greeter.applicationCodeRegister(c); +} + +int +registerInstance(char *grpc_server, int appId, long registertime, char *uuid, char *osname, char *hostname, + int processno, char *ipv4s) { + GreeterClient greeter(grpc::CreateChannel(grpc_server, grpc::InsecureChannelCredentials())); + return greeter.registerInstance(appId, registertime, uuid, osname, hostname, processno, ipv4s); +} diff --git a/sky-php7ext/src/report/json.hpp b/sky-php7ext/src/report/json.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1e7cf51e0ac88e186ebfc73e67178c617d9de7f8 --- /dev/null +++ b/sky-php7ext/src/report/json.hpp @@ -0,0 +1,20274 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ +| | |__ | | | | | | version 3.4.0 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2013-2018 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef NLOHMANN_JSON_HPP +#define NLOHMANN_JSON_HPP + +#define NLOHMANN_JSON_VERSION_MAJOR 3 +#define NLOHMANN_JSON_VERSION_MINOR 4 +#define NLOHMANN_JSON_VERSION_PATCH 0 + +#include // all_of, find, for_each +#include // assert +#include // and, not, or +#include // nullptr_t, ptrdiff_t, size_t +#include // hash, less +#include // initializer_list +#include // istream, ostream +#include // iterator_traits, random_access_iterator_tag +#include // accumulate +#include // string, stoi, to_string +#include // declval, forward, move, pair, swap + +// #include +#ifndef NLOHMANN_JSON_FWD_HPP +#define NLOHMANN_JSON_FWD_HPP + +#include // int64_t, uint64_t +#include // map +#include // allocator +#include // string +#include // vector + +/*! +@brief namespace for Niels Lohmann +@see https://github.com/nlohmann +@since version 1.0.0 +*/ +namespace nlohmann +{ +/*! +@brief default JSONSerializer template argument + +This serializer ignores the template arguments and uses ADL +([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl)) +for serialization. +*/ +template +struct adl_serializer; + +template class ObjectType = + std::map, + template class ArrayType = std::vector, + class StringType = std::string, class BooleanType = bool, + class NumberIntegerType = std::int64_t, + class NumberUnsignedType = std::uint64_t, + class NumberFloatType = double, + template class AllocatorType = std::allocator, + template class JSONSerializer = + adl_serializer> +class basic_json; + +/*! +@brief JSON Pointer + +A JSON pointer defines a string syntax for identifying a specific value +within a JSON document. It can be used with functions `at` and +`operator[]`. Furthermore, JSON pointers are the base for JSON patches. + +@sa [RFC 6901](https://tools.ietf.org/html/rfc6901) + +@since version 2.0.0 +*/ +template +class json_pointer; + +/*! +@brief default JSON class + +This type is the default specialization of the @ref basic_json class which +uses the standard template types. + +@since version 1.0.0 +*/ +using json = basic_json<>; +} // namespace nlohmann + +#endif + +// #include + + +// This file contains all internal macro definitions +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdocumentation" +#endif + +// allow for portable deprecation warnings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) + #define JSON_DEPRECATED __declspec(deprecated) +#else + #define JSON_DEPRECATED +#endif + +// allow to disable exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// manual branch prediction +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_LIKELY(x) __builtin_expect(!!(x), 1) + #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else + #define JSON_LIKELY(x) x + #define JSON_UNLIKELY(x) x +#endif + +// C++ language standard detection +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json + +// #include + + +#include // not +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type + +namespace nlohmann +{ +namespace detail +{ +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +template +using uncvref_t = typename std::remove_cv::type>::type; + +// implementation of C++14 index_sequence and affiliates +// source: https://stackoverflow.com/a/32223343 +template +struct index_sequence +{ + using type = index_sequence; + using value_type = std::size_t; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +template +struct merge_and_renumber; + +template +struct merge_and_renumber, index_sequence> + : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; + +template +struct make_index_sequence + : merge_and_renumber < typename make_index_sequence < N / 2 >::type, + typename make_index_sequence < N - N / 2 >::type > {}; + +template<> struct make_index_sequence<0> : index_sequence<> {}; +template<> struct make_index_sequence<1> : index_sequence<0> {}; + +template +using index_sequence_for = make_index_sequence; + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static constexpr T value{}; +}; + +template +constexpr T static_const::value; +} // namespace detail +} // namespace nlohmann + +// #include + + +#include // not +#include // numeric_limits +#include // false_type, is_constructible, is_integral, is_same, true_type +#include // declval + +// #include + +// #include + +// #include + + +#include + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; +} // namespace detail +} // namespace nlohmann + + +// http://en.cppreference.com/w/cpp/experimental/is_detected +namespace nlohmann +{ +namespace detail +{ +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + void operator=(nonesuch const&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template