skywalking.c 40.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
  +----------------------------------------------------------------------+
  | PHP Version 7                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2017 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/time.h>


#include "main/SAPI.h" /* for sapi_module */
#include "zend_smart_str.h" /* for smart_str */
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
S
songzhian 已提交
33
#include "grpc/common_struct.h"
34 35 36 37 38 39 40 41 42 43 44
#include "php_skywalking.h"
#include "ext/standard/url.h" /* for php_url */

#include "ext/standard/basic_functions.h"
#include "ext/standard/php_math.h"
#include <string.h>
#include "ext/json/php_json.h"
#include "ext/date/php_date.h"
#include <curl/curl.h>
#include <curl/easy.h>

S
songzhian 已提交
45 46 47 48 49 50 51 52 53
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
#include <dirent.h>

54

S
songzhian 已提交
55 56 57
extern int goSkyGrpc(char*host, int  method, ParamDataStruct* paramData, void (*callfuct)( AppInstance* ) );

/* If you declare any globals in php_skywalking.h uncomment this:
58 59 60 61 62
*/
ZEND_DECLARE_MODULE_GLOBALS(skywalking)

/* True global resources - no need for thread safety here */
static int le_skywalking;
S
songzhian 已提交
63 64 65 66 67 68 69 70 71
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 sky_close = 0;
static int sky_increment_id = 0;
static long percent_int;
pthread_t pid_consumer;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

/* {{{ 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()
/* }}} */





S
songzhian 已提交
89

90 91 92 93 94 95 96
/* {{{ 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)
S
songzhian 已提交
97 98
    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)
99
    STD_PHP_INI_ENTRY("skywalking.header_client_ip_name", "api", PHP_INI_ALL, OnUpdateString, global_header_client_ip_name, zend_skywalking_globals, skywalking_globals)
S
songzhian 已提交
100 101
    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)

102 103 104 105 106 107 108 109 110 111 112
PHP_INI_END()

/* }}} */



/* {{{ skywalking_functions[]
 *
 * Every user visible function must have an entry in skywalking_functions[].
 */
const zend_function_entry skywalking_functions[] = {
S
songzhian 已提交
113
		/* For testing, remove later. */
114 115 116 117 118 119 120 121 122 123
	PHP_FE_END	/* Must be the last line in skywalking_functions[] */
};
/* }}} */



const zend_function_entry class_skywalking[] = {
	PHP_FE_END
};

S
songzhian 已提交
124 125 126



127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   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);
	}
S
songzhian 已提交
155
	start_node_span_of_curl();
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

	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 ;
		}
S
songzhian 已提交
195 196
		if(strlen(super_peer_host) != 0){
			make_span_curl_header(super_peer_host, zvalue);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
			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 accel_sky_curl_exec(INTERNAL_FUNCTION_PARAMETERS)
{
	zval		*zid;
	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
		return;
	}
	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);
		}
		//send setopt header
		zval function_name, params[3];
		ZVAL_COPY(&params[0], zid);
		ZVAL_LONG(&params[1], CURLOPT_HTTPHEADER);
		ZVAL_COPY(&params[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(&params[0]);
		zval_ptr_dtor(&params[2]);
		zval_ptr_dtor(&super_curl_header);
	}
	orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
S
songzhian 已提交
230
	end_node_span_of_curl( zid );
231 232 233 234 235 236 237 238
}
/* {{{ php_skywalking_init_globals
 */
/* 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;
S
songzhian 已提交
239
	skywalking_globals->global_sampling_rate = 99.22;
240
	skywalking_globals->global_auto_open = 1;
S
songzhian 已提交
241
	skywalking_globals->global_send_type = 1;
242
	skywalking_globals->global_header_client_ip_name = NULL;
S
songzhian 已提交
243
	skywalking_globals->global_app_grpc_trace = NULL;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}



static char *sky_json_encode(zval *parameter){

	int return_code;
	smart_str buf = {0};
	zend_long options = 0;
#if PHP_VERSION_ID >= 71000
	return_code = php_json_encode(&buf, parameter, (int)options);
	if (return_code != SUCCESS) {
		smart_str_free(&buf);
		return NULL;
	}
#else
	php_json_encode(&buf, parameter, (int)options);
#endif
	smart_str_0(&buf);
	return ZSTR_VAL(buf.s);
}

S
songzhian 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
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 = 15;
	int num = 0;
	
    while( 1 )
    { 
    
    	if(zend_hash_num_elements(Z_ARRVAL(glob_skywalking_globals_queue)) > 0){
			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(glob_skywalking_globals_queue), num_key, string_key, operand) {
				send_grpc_param(operand);
				zend_hash_index_del(Z_ARRVAL(glob_skywalking_globals_queue), num_key);
			} ZEND_HASH_FOREACH_END();
		}
			
        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);
}

351

S
songzhian 已提交
352 353

static void *write_log(char *text){
354 355 356
	char *log_path;
	char logFilename[100];
	char message[strlen(text)];
S
songzhian 已提交
357
	log_path = SKY_G(global_log_path);
358 359 360 361 362 363 364 365 366 367 368 369 370 371

	zend_string *date_fmt;
	time_t t;
	t = time(NULL);
	date_fmt = php_format_date("Ymd", 4, 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);
}

S
songzhian 已提交
372 373 374 375
/**
 * 获取接收到 SWTraceContext 的 header
// //  */
static zval receive_SWHeader_from_caller()
376
{
S
songzhian 已提交
377 378 379 380 381 382 383 384
	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);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	}

	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 *delim = zend_string_init(ZEND_STRL("|"), 0);
		php_explode(delim, sw_header_text, &sw_header_info_z, 10);



		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) {
S
songzhian 已提交
402
			zend_hash_str_update(Z_ARRVAL(sw_header_info),   sw_header_info_keys[num_key], strlen(sw_header_info_keys[num_key]), operand);
403 404
		} ZEND_HASH_FOREACH_END();
	}
S
songzhian 已提交
405 406

	add_assoc_zval(&glob_skywalking_globals_list, "_swHeaderInfo", &sw_header_info);
407 408 409 410
	return sw_header_info;

}

S
songzhian 已提交
411
static void set_sampling_rate(double degrees){
412 413 414 415 416 417 418 419 420 421


	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;
	}
S
songzhian 已提交
422
	
423 424
}

S
songzhian 已提交
425
static zval* generate_trace_id_for_array(int use_parent_tid, zval* z_trace_id){
426

S
songzhian 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	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(){
451
	zval *sw_header_info;
S
songzhian 已提交
452 453 454 455 456 457 458 459 460
	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(){
461
	zval *ret;
S
songzhian 已提交
462 463 464 465 466 467 468 469 470 471 472 473
	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;
474 475
	_trace_id = (char *)emalloc(sizeof(char)*180);
	bzero(_trace_id, 180);
S
songzhian 已提交
476
	if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){
477
		if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), "TraceId", sizeof("TraceId") - 1)) != NULL) {
S
songzhian 已提交
478
			zend_hash_str_update(Z_ARRVAL(glob_skywalking_globals_list), "_traceId", sizeof("_traceId") - 1, ret);
479 480 481
			return Z_STRVAL_P(ret);
		}
	}
S
songzhian 已提交
482
	if ((ret = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_traceId", sizeof("_traceId") - 1)) != NULL) {
483 484
		return Z_STRVAL_P(ret);
	}
S
songzhian 已提交
485 486 487 488
	_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);

489 490 491 492 493
	return _trace_id;

}


S
songzhian 已提交
494
static char *generate_distributed_trace_ids(){
495 496
	zval *sw_header_info;
	zval *ret;
S
songzhian 已提交
497
	if( (sw_header_info = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), "_swHeaderInfo", sizeof("_swHeaderInfo") - 1)) != NULL){
498 499 500 501
		if ((ret = zend_hash_str_find(Z_ARRVAL_P(sw_header_info), "DistributedTraceIds", sizeof("DistributedTraceIds") - 1)) != NULL) {
			return Z_STRVAL_P(ret);
		}
	}
S
songzhian 已提交
502 503 504 505
	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);
		}
506
	}
S
songzhian 已提交
507 508 509 510 511 512 513 514


	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);

515 516 517 518 519
	return _distributed_trace_ids;
}


static char *make_trace_id(){
S
songzhian 已提交
520 521 522
	int app_instance_id = get_app_instance_id();
	int sys_pid = getpid();
	char *millisecond = get_millisecond();
523 524
	char *makeTraceId;
   	makeTraceId = (char *)emalloc(sizeof(char)*180);
S
songzhian 已提交
525 526 527 528 529

   	bzero(makeTraceId, 80);

    sprintf(makeTraceId, "%d.%d.%s%d", app_instance_id, sys_pid, millisecond, sky_increment_id);

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    return makeTraceId;
}

static char *get_millisecond(){
	struct timeval tv;
	gettimeofday(&tv,NULL);
	char *buffer;
	buffer = (char *)emalloc(sizeof(char)*20);
	bzero(buffer, 20);
	long millisecond;
	millisecond = tv.tv_sec*1000 + tv.tv_usec/1000;
	sprintf(buffer, "%ld",  millisecond);

	return buffer;
}



static char *get_page_url_and_peer(){

	zval *carrier = NULL;
	zval *s_https,*http_host, *request_uri;
	carrier = &PG(http_globals)[TRACK_VARS_SERVER];

	smart_str uri = {0};
	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);
}


S
songzhian 已提交
575
static char *build_SWheader_value(const char *peer_host){
576

S
songzhian 已提交
577 578 579
	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]  ;
580 581
	//zval SW_trace_context;
	SW_header = (char *)emalloc(sizeof(char)*250);
S
songzhian 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	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
		);
597 598 599 600 601


	return SW_header;
}

S
songzhian 已提交
602 603
static long generate_span_id(){

604
	long span_id;
S
songzhian 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618
	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);
	
619 620 621
	return span_id;
}

S
songzhian 已提交
622
static zend_always_inline zend_uchar is_sampling(){
623

S
songzhian 已提交
624
	zval *z_p_is_sampling , sampling_rate;
625 626 627 628
	int is_sampling_int;

	zend_long rand_val;

S
songzhian 已提交
629 630 631
	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) ){
632
		is_sampling_int = 0;
S
songzhian 已提交
633 634 635 636
		
		ZVAL_LONG(&sampling_rate, percent_int);

		if(Z_LVAL(sampling_rate) < 10000){
637
#if PHP_VERSION_ID >= 71000
S
songzhian 已提交
638
			rand_val = php_mt_rand_common(1, 10000);
639
#else
S
songzhian 已提交
640 641 642 643 644
			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);
645
#endif
S
songzhian 已提交
646 647
			if(Z_LVAL(sampling_rate) >= rand_val){
				is_sampling_int = 1;
648 649
			}
		}
S
songzhian 已提交
650 651 652 653 654
		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);
655 656 657 658
	}
	return Z_TYPE_P(z_p_is_sampling);
}

S
songzhian 已提交
659 660 661 662 663 664 665
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);
	}
}
666

S
songzhian 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
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;
712 713 714
}

static int is_auto_open(){
S
songzhian 已提交
715 716 717
	if(sky_close){
		return 0;
	}
718 719 720 721 722 723 724 725 726 727
	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;
}


S
songzhian 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/**
 * 当前机器ip
 * 
 * @since 2017年11月23日
 * @copyright
 * @return return_type
//  */
static char* _get_current_machine_ip(){

 
	char *ip;
	zval *carrier = NULL;
	ip  = (char *)emalloc(sizeof(char)*100);

	bzero(ip, 100);

	carrier = &PG(http_globals)[TRACK_VARS_SERVER];

	if (strcasecmp("cli", sapi_module.name) == 0){
		strcpy(ip, "127.0.0.1");
	}else{
		char hname[128];
    	struct hostent *hent;
     	gethostname(hname, sizeof(hname));
		hent = gethostbyname(hname);
		ip = inet_ntoa(*(struct in_addr*)(hent->h_addr_list[0]));
	}


    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");
S
songzhian 已提交
773
	add_assoc_long(father_nodes_data, SKYWALKING_PARENT_APPLICATION_ID, zend_atoi(parent_app_instance_id, strlen(parent_app_instance_id)));
S
songzhian 已提交
774 775
	
	char *span_id = generate_parent_info_from_header("SpanId");
S
songzhian 已提交
776
	add_assoc_long(father_nodes_data, SKYWALKING_PARENT_SPAN_ID, zend_atoi(span_id, strlen(span_id)));
S
songzhian 已提交
777 778 779 780 781 782 783
	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");
S
songzhian 已提交
784
	add_assoc_long(father_nodes_data, SKYWALKING_ENTRY_SERVICE_ID, zend_atoi(entry_app_id, strlen(entry_app_id)));
S
songzhian 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	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 nodeinit(){

	if(!sky_live_pthread(pid_consumer)){
		if (pthread_create(&pid_consumer, NULL, (void *) empty_consumer_thread_entry, NULL) < 0)
		{
			sky_close  = 1;
			return ;
		}
	}
	zval glob_all_node_data, segment, span_first_node_data;
	zval father_node_data;
	array_init(&glob_all_node_data);
	array_init(&span_first_node_data);
	array_init(&segment);
	array_init(&father_node_data);

	receive_SWHeader_from_caller();
	zval z_trace_id;
	array_init(&z_trace_id);
	generate_trace_id_for_array(DONOT_USE_PARENT_TRACE_ID, &z_trace_id);
	add_assoc_zval(&glob_all_node_data, SKYWALKING_DISTRIBUTED_TRACEIDS, &z_trace_id);

	add_assoc_zval(&segment, SKYWALKING_TRACE_SEGMENT_ID, &z_trace_id);

	add_assoc_long(&segment, SKYWALKING_APPLICATION_ID, get_app_id());
	add_assoc_long(&segment, SKYWALKING_APPLICATION_INSTANCE_ID, get_app_instance_id());

	get_father_node_data(&father_node_data);
	
	add_assoc_zval(&segment, SKYWALKING_FATHER_NODE_DATA, &father_node_data);
	add_assoc_zval(&glob_all_node_data, SKYWALKING_SEGMENT, &segment);


	add_assoc_long(&span_first_node_data, SKYWALKING_SPAN_ID, generate_span_id());

	add_assoc_long(&span_first_node_data, SKYWALKING_SPAN_TYPE_VALUE, 0);
	add_assoc_long(&span_first_node_data, SKYWALKING_SPAN_LAYER_VALUE, 3);

	char *l_millisecond = get_millisecond();
    long starttime =   zend_atol(l_millisecond, strlen(l_millisecond));
    add_assoc_long(&span_first_node_data, SKYWALKING_STARTTIME, starttime);
    add_assoc_long(&span_first_node_data, SKYWALKING_FATHER_SPAN_ID, -1);

    //add_assoc_string(&span_first_node_data, SKYWALKING_COMPONENT_ID, "php-server");
    add_assoc_string(&span_first_node_data, SKYWALKING_COMPONENT_NAME, "php-server");
    //add_assoc_string(&span_first_node_data, SKYWALKING_PEER_ID, "server");
    add_assoc_string(&span_first_node_data, SKYWALKING_PEER, "127.0.0.1");
    SKY_ADD_ASSOC_ZVAL(&span_first_node_data, SKYWALKING_LOGS);
    
	//add_assoc_string(&span_first_node_data, SKYWALKING_OPERATION_NAME_ID, get_page_url_and_peer());
	add_assoc_string(&span_first_node_data, SKYWALKING_OPERATION_NAME, get_page_url_and_peer());
	add_assoc_bool(&span_first_node_data, SKYWALKING_IS_ERROR, 0);
	
	zval t_tags, t_tags_tmp;
	array_init(&t_tags);
	array_init(&t_tags_tmp);

	zval z_url_k, z_url_v;
	ZVAL_STRING(&z_url_k, "url");
	ZVAL_STRING(&z_url_v, get_page_url_and_peer());
	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_v);
	add_index_zval(&t_tags, 0, &t_tags_tmp);
	add_assoc_zval(&span_first_node_data, SKYWALKING_TAGS, &t_tags);

	add_assoc_zval(&glob_skywalking_globals_list, "span_first_node_data", &span_first_node_data);
	add_assoc_zval(&glob_skywalking_globals_list, "all_node_data", &glob_all_node_data);

	SKY_ADD_ASSOC_ZVAL(&glob_skywalking_globals_list, "_span_node_data");
	SKY_ADD_ASSOC_ZVAL(&glob_skywalking_globals_list, "_spansNodeData");
}


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(&params[0], curl);
	ZVAL_STRING(&function_name,  "curl_getinfo");
	call_user_function(CG(function_table), NULL, &function_name, &curlInfo, 1, params);
	zval_ptr_dtor(&params[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);

}


static void *sky_flush_all(){
	zval *all_node_data, *span_first_node_data, *null_oject, span_bool_param, *span_int_param, *spans_node_data, *segment;
	all_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list),  ZEND_STRL("all_node_data"));
	segment = zend_hash_str_find(Z_ARRVAL_P(all_node_data),  ZEND_STRL(SKYWALKING_SEGMENT));

	zval *z_p_father_node_data = zend_hash_str_find(Z_ARRVAL_P(all_node_data),  ZEND_STRL(SKYWALKING_FATHER_NODE_DATA));
	//if (z_p_father_node_data == NULL || zend_hash_num_elements(Z_ARRVAL_P(z_p_father_node_data)) == 0){
	//	zend_hash_del(Z_ARRVAL_P(all_node_data), zend_string_init(ZEND_STRL(SKYWALKING_FATHER_NODE_DATA), 0));
	//}

	span_first_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("span_first_node_data"));
	char *l_millisecond = get_millisecond();
	add_assoc_long(span_first_node_data, SKYWALKING_ENDTIME, zend_atol(l_millisecond, strlen(l_millisecond)));

	spans_node_data = zend_hash_str_find(Z_ARRVAL(glob_skywalking_globals_list), ZEND_STRL("_spansNodeData"));

	sky_array_unshift(spans_node_data, span_first_node_data);
	char *l_f_millisecond = get_millisecond();
	//add_assoc_long(all_node_data, SKYWALKING_ENDTIME, zend_atol(l_f_millisecond, strlen(l_f_millisecond)));
	add_assoc_zval(segment, SKYWALKING_SPANS_NODE_DATA, spans_node_data);


	in_queue_log();
}


static void appinit(){

	array_init(&glob_skywalking_globals_queue);
	appInstance.applicationInstanceId = -100000;
	appInstance.applicationId = -100000;
	if (pthread_create(&pid_consumer, NULL, (void *) empty_consumer_thread_entry, NULL) < 0)
    {
        sky_close  = 1;
		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, "");  
    }
	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;
    

	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 );
}


1036 1037 1038 1039 1040
/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(skywalking)
{
	ZEND_INIT_MODULE_GLOBALS(skywalking, php_skywalking_init_globals, NULL);
S
songzhian 已提交
1041 1042 1043 1044
	if(sky_close){
		return SUCCESS;
	}
	//data_register_hashtable();
1045
	REGISTER_INI_ENTRIES();
S
songzhian 已提交
1046

1047 1048 1049
	/* If you have INI entries, uncomment these lines
	*/
	if(SKY_G(global_auto_open)){
S
songzhian 已提交
1050 1051 1052 1053 1054 1055

		appinit();
		if( sky_close == 1 ){
			return SUCCESS;
		}
		set_sampling_rate(SKY_G(global_sampling_rate));
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		zend_function *old_function;
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_init", sizeof("curl_init")-1)) != NULL) {
			orig_curl_init = old_function->internal_function.handler;
			old_function->internal_function.handler = accel_sky_curl_init;
		}
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_setopt", sizeof("curl_setopt")-1)) != NULL) {
			orig_curl_setopt = old_function->internal_function.handler;
			old_function->internal_function.handler = accel_sky_curl_setopt;
		}
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_exec", sizeof("curl_exec")-1)) != NULL) {
			orig_curl_exec = old_function->internal_function.handler;
			old_function->internal_function.handler = accel_sky_curl_exec;
		}
	}

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(skywalking)
{
	UNREGISTER_INI_ENTRIES();
	/* uncomment this line if you have INI entries

	*/
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
S
songzhian 已提交
1088
/* {{{ PHP_RINIT_FUNCTION7
1089 1090 1091
 */
PHP_RINIT_FUNCTION(skywalking)
{
S
songzhian 已提交
1092

1093 1094 1095 1096
#if defined(COMPILE_DL_SKYWALKING) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif

S
songzhian 已提交
1097 1098 1099 1100 1101 1102
	array_init(&glob_skywalking_globals_list);
	//&& is_sampling() == IS_TRUE
	if( is_auto_open()  ){
		sky_increment_id ++;
		if(sky_increment_id >= 999999){
			sky_increment_id = 0; 
1103
		}
S
songzhian 已提交
1104
		nodeinit();
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	}

	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(skywalking)
{
	if(is_auto_open()){
S
songzhian 已提交
1117
		sky_flush_all();
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	}
	is_send_curl_header = 0;
	zval_ptr_dtor(&super_curl_header);
	array_init(&super_curl_header);

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(skywalking)
{
S
songzhian 已提交
1131

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	php_info_print_table_start();
	php_info_print_table_header(2, "skywalking support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */


/** {{{ PHP_GINIT_FUNCTION

PHP_GINIT_FUNCTION(skywalking_globals)
{
	memset(skywalking_globals, 0, sizeof(*skywalking_globals));
}
 }}} */



/* {{{ skywalking_module_entry
 */
zend_module_entry skywalking_module_entry = {
	STANDARD_MODULE_HEADER,
	"skywalking",
	skywalking_functions,
	PHP_MINIT(skywalking),
	PHP_MSHUTDOWN(skywalking),
	PHP_RINIT(skywalking),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(skywalking),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(skywalking),
	PHP_SKYWALKING_VERSION,
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

S
songzhian 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277


/********************************************  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));
}
// /* }}} */





1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
#ifdef COMPILE_DL_SKYWALKING
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(skywalking)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */