skywalking.c 36.3 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"
H
heyanlong 已提交
33
#include "src/components.h"
34 35
#include "php_skywalking.h"
#include "ext/standard/url.h" /* for php_url */
H
heyanlong 已提交
36
#include "ext/standard/php_var.h"
37 38 39 40 41 42 43 44

#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>

G
goerzh 已提交
54 55
#include "b64.h"

H
report  
heyanlong 已提交
56
extern int applicationCodeRegister(char *grpc_server, char *code);
57

H
heyanlong 已提交
58
extern int registerInstance(char *grpc_server, int appId, long registertime, char *osname, char *hostname,
H
report  
heyanlong 已提交
59
                            int processno, char *ipv4s);
S
songzhian 已提交
60 61

/* If you declare any globals in php_skywalking.h uncomment this:
62 63 64 65 66
*/
ZEND_DECLARE_MODULE_GLOBALS(skywalking)

/* True global resources - no need for thread safety here */
static int le_skywalking;
H
report  
heyanlong 已提交
67 68
static int application_instance = 0;
static int application_id = 0;
S
songzhian 已提交
69 70 71
static int sky_close = 0;
static int sky_increment_id = 0;

72 73 74 75
/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini*/
PHP_INI_BEGIN()
H
heyanlong 已提交
76
	STD_PHP_INI_BOOLEAN("skywalking.enable",   	"0", PHP_INI_ALL, OnUpdateBool, enable, zend_skywalking_globals, skywalking_globals)
H
heyanlong 已提交
77
	STD_PHP_INI_ENTRY("skywalking.version",   	"5", PHP_INI_ALL, OnUpdateLong, version, zend_skywalking_globals, skywalking_globals)
H
report  
heyanlong 已提交
78 79 80
	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)
G
goerzh 已提交
81
    STD_PHP_INI_ENTRY("skywalking.header_version", "2", PHP_INI_ALL, OnUpdateLong, header_version, zend_skywalking_globals, skywalking_globals)
82 83 84 85 86 87 88 89 90 91 92
PHP_INI_END()

/* }}} */



/* {{{ skywalking_functions[]
 *
 * Every user visible function must have an entry in skywalking_functions[].
 */
const zend_function_entry skywalking_functions[] = {
S
songzhian 已提交
93
		/* For testing, remove later. */
94 95 96 97 98 99 100 101 102 103
	PHP_FE_END	/* Must be the last line in skywalking_functions[] */
};
/* }}} */



const zend_function_entry class_skywalking[] = {
	PHP_FE_END
};

S
songzhian 已提交
104 105 106



107 108 109 110 111 112
/* 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.
*/

H
heyanlong 已提交
113
void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS)
114 115 116 117 118 119
{
	zval		*zid;
	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
		return;
	}

H
heyanlong 已提交
120
	int is_send = 1;
H
heyanlong 已提交
121

H
report  
heyanlong 已提交
122 123 124 125 126
    zval function_name,curlInfo;
    zval params[1];
    ZVAL_COPY(&params[0], zid);
    ZVAL_STRING(&function_name,  "curl_getinfo");
    call_user_function(CG(function_table), NULL, &function_name, &curlInfo, 1, params);
H
memory  
heyanlong 已提交
127 128 129
    zval_dtor(&function_name);
    zval_dtor(&params[0]);

H
report  
heyanlong 已提交
130
    zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo),  ZEND_STRL("url"));
H
heyanlong 已提交
131
    char *url_str = Z_STRVAL_P(z_url);
H
heyanlong 已提交
132 133
    if(strlen(url_str) <= 0) {
        zval_dtor(&curlInfo);
H
heyanlong 已提交
134
        is_send = 0;
H
heyanlong 已提交
135
    }
H
heyanlong 已提交
136
    php_url *url_info = NULL;
H
heyanlong 已提交
137 138 139 140 141 142
    if(is_send == 1) {
        url_info = php_url_parse(url_str);
        if(url_info->scheme == NULL || url_info->host == NULL) {
            zval_dtor(&curlInfo);
            php_url_free(url_info);
            is_send = 0;
H
heyanlong 已提交
143 144 145
        }
    }

G
goerzh 已提交
146
    char *sw = NULL;
H
heyanlong 已提交
147 148 149
    zval *spans = NULL;
    zval *last_span = NULL;
    zval *span_id = NULL;
H
heyanlong 已提交
150
    char *peer = NULL;
H
heyanlong 已提交
151 152
    ssize_t operation_name_l = 0;
    char *operation_name = NULL;
H
heyanlong 已提交
153 154 155 156 157 158
    if (is_send == 1) {
        int peer_port = 0;
        if (url_info->port) {
            peer_port = url_info->port;
        } else {
            if (strcasecmp("http", url_info->scheme) == 0) {
H
heyanlong 已提交
159
                peer_port = 80;
H
heyanlong 已提交
160
            } else {
H
heyanlong 已提交
161
                peer_port = 443;
H
heyanlong 已提交
162 163
            }
        }
H
heyanlong 已提交
164

H
heyanlong 已提交
165 166 167
        peer = (char *) emalloc(strlen(url_info->scheme) + 3 + strlen(url_info->host) + 7);
        bzero(peer, strlen(url_info->scheme) + 3 + strlen(url_info->host) + 7);
        sprintf(peer, "%s://%s:%d", url_info->scheme, url_info->host, peer_port);
H
report  
heyanlong 已提交
168

H
heyanlong 已提交
169 170
        if (url_info->query) {
            if (url_info->path == NULL) {
H
heyanlong 已提交
171
                operation_name_l = snprintf(NULL, 0, "%s?%s", "/", url_info->query);
H
heyanlong 已提交
172
                operation_name = (char *) emalloc(operation_name_l + 1);
H
heyanlong 已提交
173
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
174
                sprintf(operation_name, "%s?%s", "/", url_info->query);
H
heyanlong 已提交
175
            } else {
H
heyanlong 已提交
176
                operation_name_l = snprintf(NULL, 0, "%s?%s", url_info->path, url_info->query);
H
heyanlong 已提交
177
                operation_name = (char *) emalloc(operation_name_l + 1);
H
heyanlong 已提交
178
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
179
                sprintf(operation_name, "%s?%s", url_info->path, url_info->query);
H
heyanlong 已提交
180
            }
H
heyanlong 已提交
181
        } else {
H
heyanlong 已提交
182
            if (url_info->path == NULL) {
H
heyanlong 已提交
183
                operation_name_l = snprintf(NULL, 0, "%s", "/");
H
heyanlong 已提交
184
                operation_name = (char *) emalloc(operation_name_l + 1);
H
heyanlong 已提交
185
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
186
                sprintf(operation_name, "%s", "/");
H
heyanlong 已提交
187
            } else {
H
heyanlong 已提交
188
                operation_name_l = snprintf(NULL, 0, "%s", url_info->path);
H
heyanlong 已提交
189
                operation_name = (char *) emalloc(operation_name_l + 1);
H
heyanlong 已提交
190
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
191
                sprintf(operation_name, "%s", url_info->path);
H
heyanlong 已提交
192
            }
H
heyanlong 已提交
193
        }
H
heyanlong 已提交
194 195 196 197

        spans = get_spans();
        last_span = zend_hash_index_find(Z_ARRVAL_P(spans), zend_hash_num_elements(Z_ARRVAL_P(spans)) - 1);
        span_id = zend_hash_str_find(Z_ARRVAL_P(last_span), "spanId", sizeof("spanId") - 1);
G
goerzh 已提交
198 199 200 201 202
        if (SKYWALKING_G(header_version) == 1) {
            sw = generate_sw3(Z_LVAL_P(span_id) + 1, peer, operation_name);
        } else if (SKYWALKING_G(header_version) == 2) {
            sw = generate_sw6(Z_LVAL_P(span_id) + 1, peer, operation_name);
        }
H
report  
heyanlong 已提交
203 204
    }

H
heyanlong 已提交
205

G
goerzh 已提交
206
    if (sw != NULL) {
H
heyanlong 已提交
207 208 209 210 211 212 213 214 215
        zval *option = NULL;
        int is_init = 0;
        option = zend_hash_index_find(Z_ARRVAL_P(&SKYWALKING_G(curl_header)), Z_RES_HANDLE_P(zid));

        if(option == NULL) {
            option = emalloc(sizeof(zval));
            bzero(option, sizeof(zval));
            array_init(option);
            is_init = 1;
H
heyanlong 已提交
216
        }
H
heyanlong 已提交
217

G
goerzh 已提交
218
        add_next_index_string(option, sw);
H
heyanlong 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        add_index_bool(&SKYWALKING_G(curl_header_send), (zend_ulong)Z_RES_HANDLE_P(zid), IS_TRUE);

        zval func;
        zval argv[3];
        zval ret;
        ZVAL_STRING(&func, "curl_setopt");

        ZVAL_COPY(&argv[0], zid);
        ZVAL_LONG(&argv[1], CURLOPT_HTTPHEADER);
        ZVAL_COPY(&argv[2], option);
        call_user_function(CG(function_table), NULL, &func, &ret, 3, argv);
        zval_dtor(&ret);
        zval_dtor(&func);
        if(is_init == 1) {
            zval_ptr_dtor(option);
            efree(option);
H
heyanlong 已提交
235
        }
H
heyanlong 已提交
236 237 238
        zval_dtor(&argv[0]);
        zval_dtor(&argv[1]);
        zval_dtor(&argv[2]);
G
goerzh 已提交
239
        efree(sw);
H
report  
heyanlong 已提交
240
    }
H
heyanlong 已提交
241 242 243 244

    zval temp;
    char *l_millisecond;
    long millisecond;
H
heyanlong 已提交
245
    if(is_send == 1) {
H
heyanlong 已提交
246

H
heyanlong 已提交
247
        array_init(&temp);
H
heyanlong 已提交
248

H
heyanlong 已提交
249 250 251 252 253 254 255 256 257
        add_assoc_long(&temp, "spanId", Z_LVAL_P(span_id) + 1);
        add_assoc_long(&temp, "parentSpanId", 0);
        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);
        add_assoc_long(&temp, "startTime", millisecond);
        add_assoc_long(&temp, "spanType", 1);
        add_assoc_long(&temp, "spanLayer", 3);
        add_assoc_long(&temp, "componentId", COMPONENT_HTTPCLIENT);
H
report  
heyanlong 已提交
258
    }
H
heyanlong 已提交
259 260


H
heyanlong 已提交
261
	orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
262

H
heyanlong 已提交
263 264 265 266 267 268 269 270
    if (is_send == 1) {
        zval function_name_1, curlInfo_1;
        zval params_1[1];
        ZVAL_COPY(&params_1[0], zid);
        ZVAL_STRING(&function_name_1, "curl_getinfo");
        call_user_function(CG(function_table), NULL, &function_name_1, &curlInfo_1, 1, params_1);
        zval_dtor(&params_1[0]);
        zval_dtor(&function_name_1);
H
heyanlong 已提交
271

H
heyanlong 已提交
272 273 274 275
        zval *z_http_code;
        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);
H
heyanlong 已提交
276

H
heyanlong 已提交
277
        z_http_code = zend_hash_str_find(Z_ARRVAL(curlInfo_1), ZEND_STRL("http_code"));
H
heyanlong 已提交
278

H
heyanlong 已提交
279
        add_assoc_long(&temp, "endTime", millisecond);
H
heyanlong 已提交
280 281


H
heyanlong 已提交
282 283 284 285
        add_assoc_string(&temp, "operationName", operation_name);
        add_assoc_string(&temp, "peer", peer);
        efree(peer);
        efree(operation_name);
H
heyanlong 已提交
286

H
heyanlong 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300
        php_url_free(url_info);

        if (Z_LVAL_P(z_http_code) != 200) {
            add_assoc_long(&temp, "isError", 1);
        } else {
            add_assoc_long(&temp, "isError", 0);
        }
        zval _refs;
        array_init(&_refs);
        add_assoc_zval(&temp, "refs", &_refs);
        zend_hash_next_index_insert(Z_ARRVAL_P(spans), &temp);
        zval_dtor(&curlInfo_1);
        zval_dtor(&curlInfo);
    }
301
}
H
heyanlong 已提交
302 303 304 305 306 307 308 309 310

void sky_curl_setopt_handler(INTERNAL_FUNCTION_PARAMETERS) {
    zval *zid, *zvalue;
    zend_long options;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &zid, &options, &zvalue) == FAILURE) {
        return;
    }

H
heyanlong 已提交
311 312 313 314 315 316 317 318 319 320
    zval *is_send = zend_hash_index_find(Z_ARRVAL_P(&SKYWALKING_G(curl_header_send)), Z_RES_HANDLE_P(zid));
    //
    if (is_send != NULL &&  CURLOPT_HTTPHEADER == options && Z_TYPE_P(is_send) == IS_TRUE) {
        add_index_bool(&SKYWALKING_G(curl_header_send), Z_RES_HANDLE_P(zid), IS_FALSE);
        orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    } else {
        if (CURLOPT_HTTPHEADER == options && Z_TYPE_P(zvalue) == IS_ARRAY) {
            zval copy_header;
            ZVAL_DUP(&copy_header, zvalue);
            add_index_zval(&SKYWALKING_G(curl_header), Z_RES_HANDLE_P(zid), &copy_header);
H
heyanlong 已提交
321
        } else {
H
heyanlong 已提交
322
            orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
323 324 325
        }
    }
}
H
heyanlong 已提交
326 327

void sky_curl_setopt_array_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
328 329 330
    zval *zid, *arr, *entry;
    zend_ulong option;
    zend_string *string_key;
H
heyanlong 已提交
331 332 333 334 335 336

    ZEND_PARSE_PARAMETERS_START(2, 2)
            Z_PARAM_RESOURCE(zid)
            Z_PARAM_ARRAY(arr)
    ZEND_PARSE_PARAMETERS_END();

H
heyanlong 已提交
337 338 339 340 341 342
    zval *http_header = zend_hash_index_find(Z_ARRVAL_P(arr), CURLOPT_HTTPHEADER);

    if (http_header != NULL) {
        zval copy_header;
        ZVAL_DUP(&copy_header, http_header);
        add_index_zval(&SKYWALKING_G(curl_header), Z_RES_HANDLE_P(zid), &copy_header);
H
heyanlong 已提交
343
    }
H
heyanlong 已提交
344 345 346 347

    orig_curl_setopt_array(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}

H
heyanlong 已提交
348 349 350 351 352 353 354
void sky_curl_close_handler(INTERNAL_FUNCTION_PARAMETERS) {
    zval *zid;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
        return;
    }

H
heyanlong 已提交
355 356 357
    zval *http_header = zend_hash_index_find(Z_ARRVAL_P(&SKYWALKING_G(curl_header)), Z_RES_HANDLE_P(zid));
    if (http_header != NULL) {
        zend_hash_index_del(Z_ARRVAL_P(&SKYWALKING_G(curl_header)), Z_RES_HANDLE_P(zid));
H
heyanlong 已提交
358 359 360 361
    }

    orig_curl_close(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
362 363 364 365 366
/* {{{ php_skywalking_init_globals
 */
/* Uncomment this function if you have INI entries*/
static void php_skywalking_init_globals(zend_skywalking_globals *skywalking_globals)
{
H
report  
heyanlong 已提交
367 368
	skywalking_globals->app_code = NULL;
	skywalking_globals->log_path = NULL;
H
heyanlong 已提交
369 370
	skywalking_globals->enable = 0;
	skywalking_globals->version = 5;
H
report  
heyanlong 已提交
371
	skywalking_globals->grpc = NULL;
G
goerzh 已提交
372
	skywalking_globals->header_version = 2;
373 374 375 376 377 378 379
}



static char *sky_json_encode(zval *parameter){

	smart_str buf = {0};
H
report  
heyanlong 已提交
380
	zend_long options = 64;
381 382 383 384 385 386 387 388 389 390
#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);
H
memory  
heyanlong 已提交
391 392 393
	char *bufs = ZSTR_VAL(buf.s);
	smart_str_free(&buf);
	return bufs;
394 395
}

S
songzhian 已提交
396

H
memory  
heyanlong 已提交
397 398
static void write_log(char *text) {
    if (application_instance != -100000) {
H
report  
heyanlong 已提交
399 400 401 402
        char *log_path;
        char logFilename[100];
        char message[strlen(text) + 1];
        log_path = SKY_G(log_path);
403

H
heyanlong 已提交
404
        zend_string *_log_path, *_log_path_lower;
H
memory  
heyanlong 已提交
405 406
        _log_path = zend_string_init(log_path, strlen(log_path), 0);
        _log_path_lower = php_string_tolower(_log_path);
407

H
report  
heyanlong 已提交
408
        bzero(logFilename, 100);
H
heyanlong 已提交
409
        sprintf(logFilename, "%s/skywalking.%d-%d.log", ZSTR_VAL(_log_path_lower), get_second(), getpid());
410

H
memory  
heyanlong 已提交
411 412
        zend_string_release(_log_path);
        zend_string_release(_log_path_lower);
H
report  
heyanlong 已提交
413 414 415 416
        bzero(message, strlen(text));
        sprintf(message, "%s\n", text);
        _php_error_log_ex(3, message, strlen(message), logFilename, NULL);
    }
417 418 419 420

}


H
heyanlong 已提交
421
static char *generate_sw3(zend_long span_id, char *peer_host, char *operation_name) {
H
heyanlong 已提交
422

H
heyanlong 已提交
423 424 425 426 427 428 429
    zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1);
    zval *entryApplicationInstance = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "entryApplicationInstance",
                                                        sizeof("entryApplicationInstance") - 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);
H
heyanlong 已提交
430 431 432 433 434 435 436 437 438 439
    ssize_t sw3_l = 0;
    sw3_l = snprintf(NULL, 0, "sw3: %s|%d|%d|%d|#%s|#%s|#%s|%s", Z_STRVAL_P(traceId), span_id,
                     application_instance, Z_LVAL_P(entryApplicationInstance), peer_host,
                     Z_STRVAL_P(entryOperationName), operation_name, Z_STRVAL_P(distributedTraceId));
    char *sw3 = (char*)emalloc(sw3_l + 1);
    bzero(sw3, sw3_l + 1);
    snprintf(sw3, sw3_l + 1, "sw3: %s|%d|%d|%d|#%s|#%s|#%s|%s", Z_STRVAL_P(traceId), span_id,
             application_instance, Z_LVAL_P(entryApplicationInstance), peer_host,
             Z_STRVAL_P(entryOperationName), operation_name, Z_STRVAL_P(distributedTraceId));
    return sw3;
H
heyanlong 已提交
440 441
}

G
goerzh 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static char *generate_sw6(zend_long span_id, char *peer_host, char *operation_name) {
    zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1);
    zval *entryApplicationInstance = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "entryApplicationInstance",
                                                        sizeof("entryApplicationInstance") - 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);

    ContextCarrier *contextCarrier;

    contextCarrier = emalloc(sizeof(*contextCarrier));
    zval_b64_encode(&contextCarrier->primaryDistributedTraceId, Z_STRVAL_P(distributedTraceId));
    zval_b64_encode(&contextCarrier->traceSegmentId, Z_STRVAL_P(traceId));
    zval_b64_encode(&contextCarrier->peerHost, peer_host);
    zval_b64_encode(&contextCarrier->entryEndpointName, Z_STRVAL_P(entryOperationName));
    zval_b64_encode(&contextCarrier->parentEndpointName, operation_name);

    ssize_t sw6_l = 0;
G
fix bug  
goerzh 已提交
461 462 463 464
    sw6_l = snprintf(NULL, 0, "sw6: 1-%s-%s-%d-%d-%d-%s-%s-%s", Z_STRVAL(contextCarrier->primaryDistributedTraceId),
                     Z_STRVAL(contextCarrier->traceSegmentId), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
                     Z_STRVAL(contextCarrier->peerHost), Z_STRVAL(contextCarrier->entryEndpointName),
                     Z_STRVAL(contextCarrier->parentEndpointName));
G
goerzh 已提交
465 466 467

    char *sw6 = (char*)emalloc(sw6_l + 1);
    bzero(sw6, sw6_l + 1);
G
fix bug  
goerzh 已提交
468 469 470 471
    snprintf(sw6, sw6_l + 1, "sw6: 1-%s-%s-%d-%d-%d-%s-%s-%s", Z_STRVAL(contextCarrier->primaryDistributedTraceId),
             Z_STRVAL(contextCarrier->traceSegmentId), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
             Z_STRVAL(contextCarrier->peerHost), Z_STRVAL(contextCarrier->entryEndpointName),
             Z_STRVAL(contextCarrier->parentEndpointName));
G
goerzh 已提交
472 473 474 475 476 477

    efree(contextCarrier);

    return sw6;
}

H
heyanlong 已提交
478 479 480 481
static zend_string *trim_sharp(zval *tmp) {
    return php_trim(Z_STR_P(tmp), "#", sizeof("#") - 1, 1);
}

G
goerzh 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495
static void zval_b64_encode(zval *out, char *in) {
    char *enc = b64_encode(in, strlen(in));
    zend_string *str = zend_string_init(enc, strlen(enc)-1, 0);
    ZVAL_STR(out, str);
    free(enc);
}

static void zval_b64_decode(zval *out, char *in) {
    char *dec = b64_decode(in, strlen(in));
    zend_string *str = zend_string_init(dec, strlen(dec)-1, 0);
    ZVAL_STR(out, str);
    free(dec);
}

H
report  
heyanlong 已提交
496 497 498 499 500 501
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);
H
heyanlong 已提交
502

H
heyanlong 已提交
503
    bzero(makeTraceId, sizeof(char) * 180);
S
songzhian 已提交
504

H
report  
heyanlong 已提交
505
    sprintf(makeTraceId, "%d.%d.%ld", application_instance, sys_pid, second);
S
songzhian 已提交
506

H
report  
heyanlong 已提交
507
    add_assoc_string(&SKYWALKING_G(context), "currentTraceId", makeTraceId);
H
heyanlong 已提交
508
    add_assoc_long(&SKYWALKING_G(context), "isChild", 0);
H
heyanlong 已提交
509

H
report  
heyanlong 已提交
510 511
    // parent
    zval *carrier = NULL;
G
goerzh 已提交
512
    ContextCarrier *contextCarrier = NULL;
H
heyanlong 已提交
513

H
report  
heyanlong 已提交
514
    zend_bool jit_initialization = PG(auto_globals_jit);
H
heyanlong 已提交
515

H
report  
heyanlong 已提交
516 517 518 519 520
    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);
    }
H
heyanlong 已提交
521
    carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"));
G
goerzh 已提交
522 523
    if (SKYWALKING_G(header_version) == 2) {
        zval *sw6;
G
goerzh 已提交
524
        sw6 = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW6", sizeof("HTTP_SW6") - 1);
G
goerzh 已提交
525 526
        if (sw6 != NULL && Z_TYPE_P(sw6) == IS_STRING && Z_STRLEN_P(sw6) > 10) {
            add_assoc_string(&SKYWALKING_G(context), "sw6", Z_STRVAL_P(sw6));
G
goerzh 已提交
527
            contextCarrier = emalloc(sizeof(*contextCarrier));
G
goerzh 已提交
528 529 530 531 532 533

            zval temp;
            array_init(&temp);
            php_explode(zend_string_init(ZEND_STRL("-"), 0), Z_STR_P(sw6), &temp, 10);

            if (zend_array_count(Z_ARRVAL_P(&temp)) >= 7) {
G
goerzh 已提交
534 535 536 537 538 539 540 541
                zval_b64_decode(&contextCarrier->primaryDistributedTraceId, Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 1)));
                zval_b64_decode(&contextCarrier->traceSegmentId, Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 2)));
                ZVAL_COPY(&contextCarrier->spanId, zend_hash_index_find(Z_ARRVAL(temp), 3));
                ZVAL_COPY(&contextCarrier->parentServiceInstanceId, zend_hash_index_find(Z_ARRVAL(temp), 4));
                ZVAL_COPY(&contextCarrier->entryServiceInstanceId, zend_hash_index_find(Z_ARRVAL(temp), 5));
                zval_b64_decode(&contextCarrier->peerHost, Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 6)));
                zval_b64_decode(&contextCarrier->entryEndpointName, Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 7)));
                zval_b64_decode(&contextCarrier->parentEndpointName, Z_STRVAL_P(zend_hash_index_find(Z_ARRVAL(temp), 8)));
G
goerzh 已提交
542
            }
H
heyanlong 已提交
543
        }
G
goerzh 已提交
544 545 546 547 548 549
    } else if (SKYWALKING_G(header_version) == 1) {
        zval *sw3;
        sw3 = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW3", sizeof("HTTP_SW3") - 1);

        if (sw3 != NULL && Z_TYPE_P(sw3) == IS_STRING && Z_STRLEN_P(sw3) > 10) {
            add_assoc_string(&SKYWALKING_G(context), "sw3", Z_STRVAL_P(sw3));
G
goerzh 已提交
550
            contextCarrier = emalloc(sizeof(*contextCarrier));
G
goerzh 已提交
551 552 553 554 555 556 557

            zval temp;
            array_init(&temp);

            php_explode(zend_string_init(ZEND_STRL("|"), 0), Z_STR_P(sw3), &temp, 10);

            if (zend_array_count(Z_ARRVAL_P(&temp)) >= 8) {
G
goerzh 已提交
558 559 560 561 562 563 564 565
                ZVAL_COPY(&contextCarrier->traceSegmentId, zend_hash_index_find(Z_ARRVAL(temp), 0));
                ZVAL_COPY(&contextCarrier->spanId, zend_hash_index_find(Z_ARRVAL(temp), 1));
                ZVAL_COPY(&contextCarrier->parentServiceInstanceId, zend_hash_index_find(Z_ARRVAL(temp), 2));
                ZVAL_COPY(&contextCarrier->entryServiceInstanceId, zend_hash_index_find(Z_ARRVAL(temp), 3));
                ZVAL_COPY(&contextCarrier->peerHost, zend_hash_index_find(Z_ARRVAL(temp), 4));
                ZVAL_COPY(&contextCarrier->entryEndpointName, zend_hash_index_find(Z_ARRVAL(temp), 5));
                ZVAL_COPY(&contextCarrier->parentEndpointName, zend_hash_index_find(Z_ARRVAL(temp), 6));
                ZVAL_COPY(&contextCarrier->primaryDistributedTraceId, zend_hash_index_find(Z_ARRVAL(temp), 7));
G
goerzh 已提交
566 567 568 569 570 571 572 573 574 575
            }
        }
    }

    if (contextCarrier != NULL) {
        zval child;
        array_init(&child);
        ZVAL_LONG(&child, 1);
        zend_hash_str_update(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1, &child);

G
goerzh 已提交
576
        add_assoc_string(&SKYWALKING_G(context), "parentTraceSegmentId", Z_STRVAL(contextCarrier->traceSegmentId));
G
goerzh 已提交
577
        add_assoc_long(&SKYWALKING_G(context), "parentSpanId",
G
goerzh 已提交
578
                       zend_atol(Z_STRVAL(contextCarrier->spanId), sizeof(Z_STRVAL(contextCarrier->spanId)) - 1));
G
goerzh 已提交
579
        add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance",
G
goerzh 已提交
580
                       zend_atol(Z_STRVAL(contextCarrier->parentServiceInstanceId), sizeof(Z_STRVAL(contextCarrier->parentServiceInstanceId)) - 1));
G
goerzh 已提交
581
        add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance",
G
goerzh 已提交
582 583 584 585 586 587 588
                       zend_atol(Z_STRVAL(contextCarrier->entryServiceInstanceId), sizeof(Z_STRVAL(contextCarrier->entryServiceInstanceId)) - 1));
        add_assoc_str(&SKYWALKING_G(context), "networkAddress", trim_sharp(&contextCarrier->peerHost));
        add_assoc_str(&SKYWALKING_G(context), "entryOperationName", trim_sharp(&contextCarrier->entryEndpointName));
        add_assoc_str(&SKYWALKING_G(context), "parentOperationName", trim_sharp(&contextCarrier->parentEndpointName));
        add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", Z_STRVAL(contextCarrier->primaryDistributedTraceId));

        efree(contextCarrier);
H
heyanlong 已提交
589 590 591
    } else {
        add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance);
        add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance);
H
report  
heyanlong 已提交
592
        add_assoc_string(&SKYWALKING_G(context), "entryOperationName", get_page_request_uri());
H
heyanlong 已提交
593
        add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId);
G
goerzh 已提交
594 595
    };

H
memory  
heyanlong 已提交
596
    efree(makeTraceId);
597 598
}

H
report  
heyanlong 已提交
599 600 601 602 603 604 605

static long get_second() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec;
}

606 607 608 609 610 611 612 613 614 615 616 617 618 619
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;
}


H
heyanlong 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static char *get_page_request_uri() {
    zval *carrier = NULL;
    zval *request_uri;

    smart_str uri = {0};

    if (strcasecmp("cli", sapi_module.name) == 0) {
        smart_str_appendl(&uri, "cli", strlen("cli"));
    } else {
        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);
        }
        carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"));

        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);
H
memory  
heyanlong 已提交
643 644 645
    char *uris = ZSTR_VAL(uri.s);
    smart_str_free(&uri);
    return uris;
H
heyanlong 已提交
646
}
647 648


S
songzhian 已提交
649
/**
H
report  
heyanlong 已提交
650
 * ip
S
songzhian 已提交
651 652 653 654 655 656 657
 * 
 * @since 2017年11月23日
 * @copyright
 * @return return_type
//  */
static char* _get_current_machine_ip(){

H
heyanlong 已提交
658

S
songzhian 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	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;
}

H
heyanlong 已提交
681
static void request_init() {
S
songzhian 已提交
682

H
heyanlong 已提交
683
    array_init(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
684
    array_init(&SKYWALKING_G(curl_header_send));
H
heyanlong 已提交
685 686
    array_init(&SKYWALKING_G(context));
	array_init(&SKYWALKING_G(UpstreamSegment));
S
songzhian 已提交
687

H
report  
heyanlong 已提交
688
    generate_context();
S
songzhian 已提交
689

H
report  
heyanlong 已提交
690
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_instance", application_instance);
H
heyanlong 已提交
691
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "pid", getppid());
H
report  
heyanlong 已提交
692
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_id", application_id);
H
heyanlong 已提交
693
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "version", SKYWALKING_G(version));
H
heyanlong 已提交
694
	SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "segment");
H
report  
heyanlong 已提交
695 696 697
	SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "globalTraceIds");

	zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1);
S
songzhian 已提交
698

H
heyanlong 已提交
699 700 701 702
	zval traceSegmentObject;
	zval spans;
	array_init(&spans);
	array_init(&traceSegmentObject);
H
report  
heyanlong 已提交
703 704
	add_assoc_string(&traceSegmentObject, "traceSegmentId", Z_STRVAL_P(traceId));
	add_assoc_long(&traceSegmentObject, "isSizeLimited", 0);
S
songzhian 已提交
705

H
heyanlong 已提交
706 707
	zval temp;
	array_init(&temp);
S
songzhian 已提交
708

H
heyanlong 已提交
709 710 711 712 713 714 715
    add_assoc_long(&temp, "spanId", 0);
    add_assoc_long(&temp, "parentSpanId", -1);
    char *l_millisecond = get_millisecond();
    long millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
    efree(l_millisecond);
    add_assoc_long(&temp, "startTime", millisecond);
    add_assoc_string(&temp, "operationName", get_page_request_uri());
H
report  
heyanlong 已提交
716
    add_assoc_string(&temp, "peer", "");
H
heyanlong 已提交
717 718 719
    add_assoc_long(&temp, "spanType", 0);
    add_assoc_long(&temp, "spanLayer", 3);
    add_assoc_long(&temp, "componentId", COMPONENT_HTTPCLIENT);
S
songzhian 已提交
720

H
heyanlong 已提交
721 722 723 724
    zval *isChild = zend_hash_str_find(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1);
    // refs
    zval refs;
    array_init(&refs);
H
heyanlong 已提交
725 726 727 728 729

    zval globalTraceIds;
    array_init(&globalTraceIds);
    zval tmpGlobalTraceIds;

H
heyanlong 已提交
730
    if(Z_LVAL_P(isChild) == 1) {
H
memory  
heyanlong 已提交
731 732
        zval ref;
        array_init(&ref);
H
heyanlong 已提交
733 734 735 736 737 738
        zval *parentTraceSegmentId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentTraceSegmentId", sizeof("parentTraceSegmentId") - 1);
        zval *parentSpanId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentSpanId", sizeof("parentSpanId") - 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 *entryOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "entryOperationName", sizeof("entryOperationName") - 1);
        zval *networkAddress = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "networkAddress", sizeof("networkAddress") - 1);
H
heyanlong 已提交
739
        zval *parentOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentOperationName", sizeof("parentOperationName") - 1);
H
heyanlong 已提交
740
        zval *distributedTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "distributedTraceId", sizeof("distributedTraceId") - 1);
H
heyanlong 已提交
741 742 743 744 745 746 747
        add_assoc_long(&ref, "type", 0);
        add_assoc_string(&ref, "parentTraceSegmentId", Z_STRVAL_P(parentTraceSegmentId));
        add_assoc_long(&ref, "parentSpanId", Z_LVAL_P(parentSpanId));
        add_assoc_long(&ref, "parentApplicationInstanceId", Z_LVAL_P(parentApplicationInstance));
        add_assoc_string(&ref, "networkAddress", Z_STRVAL_P(networkAddress));
        add_assoc_long(&ref, "entryApplicationInstanceId", Z_LVAL_P(entryApplicationInstance));
        add_assoc_string(&ref, "entryServiceName", Z_STRVAL_P(entryOperationName));
H
heyanlong 已提交
748
        add_assoc_string(&ref, "parentServiceName", Z_STRVAL_P(parentOperationName));
H
heyanlong 已提交
749
        zend_hash_next_index_insert(Z_ARRVAL(refs), &ref);
H
heyanlong 已提交
750 751 752
        ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(distributedTraceId));
    } else {
        ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(traceId));
H
heyanlong 已提交
753 754 755 756

    }

    zend_hash_str_add(Z_ARRVAL(temp), "refs", sizeof("refs") - 1, &refs);
H
heyanlong 已提交
757
    zend_hash_next_index_insert(Z_ARRVAL(spans), &temp);
S
songzhian 已提交
758

H
heyanlong 已提交
759
    add_assoc_zval(&traceSegmentObject, "spans", &spans);
S
songzhian 已提交
760

H
report  
heyanlong 已提交
761
    zend_hash_next_index_insert(Z_ARRVAL(globalTraceIds), &tmpGlobalTraceIds);
S
songzhian 已提交
762

H
report  
heyanlong 已提交
763 764
    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);
S
songzhian 已提交
765 766 767
}


H
report  
heyanlong 已提交
768
static void sky_flush_all() {
S
songzhian 已提交
769
	char *l_millisecond = get_millisecond();
H
heyanlong 已提交
770 771 772 773
	long millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
	efree(l_millisecond);

	zval *span = get_first_span();
S
songzhian 已提交
774

H
heyanlong 已提交
775 776
	add_assoc_long(span, "endTime", millisecond);
	if ((SG(sapi_headers).http_response_code >= 500)) {
H
heyanlong 已提交
777
		add_assoc_long(span, "isError", 1);
H
heyanlong 已提交
778
	} else {
H
heyanlong 已提交
779
		add_assoc_long(span, "isError", 0);
H
heyanlong 已提交
780
	}
S
songzhian 已提交
781

H
heyanlong 已提交
782 783
	write_log(sky_json_encode(&SKYWALKING_G(UpstreamSegment)));
}
S
songzhian 已提交
784

H
heyanlong 已提交
785 786 787 788 789 790
static zval *get_first_span() {
	zval *segment = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "segment", sizeof("segment") - 1);
	zval *spans = zend_hash_str_find(Z_ARRVAL_P(segment), "spans", sizeof("spans") - 1);
	zval *span = zend_hash_index_find(Z_ARRVAL_P(spans), 0);
	return span;
}
S
songzhian 已提交
791

H
heyanlong 已提交
792 793 794 795
static zval *get_spans() {
	zval *segment = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "segment", sizeof("segment") - 1);
	zval *spans = zend_hash_str_find(Z_ARRVAL_P(segment), "spans", sizeof("spans") - 1);
	return spans;
S
songzhian 已提交
796 797 798
}


H
heyanlong 已提交
799 800
static void module_init() {

H
report  
heyanlong 已提交
801 802
    application_instance = -100000;
    application_id = -100000;
H
report  
heyanlong 已提交
803 804 805 806 807 808

    int i = 0;

    do {
        application_id = applicationCodeRegister(SKYWALKING_G(grpc), SKYWALKING_G(app_code));

H
report  
heyanlong 已提交
809 810 811 812
        if(application_id == -100000) {
            sleep(1);
        }

H
report  
heyanlong 已提交
813 814
        i++;
    } while (application_id == -100000 && i <= 3);
H
report  
heyanlong 已提交
815 816 817 818

    if (application_id == -100000) {
        sky_close = 1;
        return;
S
songzhian 已提交
819
    }
H
heyanlong 已提交
820

H
report  
heyanlong 已提交
821
    char *ipv4s = _get_current_machine_ip();
S
songzhian 已提交
822

H
report  
heyanlong 已提交
823 824 825 826 827 828 829 830 831
    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);

H
report  
heyanlong 已提交
832 833
    i = 0;
    do {
H
heyanlong 已提交
834
        application_instance = registerInstance(SKYWALKING_G(grpc), application_id, millisecond, SKY_OS_NAME,
H
report  
heyanlong 已提交
835 836
                                                hostname, getpid(),
                                                ipv4s);
H
report  
heyanlong 已提交
837 838 839
        if(application_instance == -100000) {
            sleep(1);
        }
H
report  
heyanlong 已提交
840 841 842
        i++;
    } while (application_instance == -100000 && i <= 3);

H
heyanlong 已提交
843

H
report  
heyanlong 已提交
844
    if (application_instance == -100000) {
H
report  
heyanlong 已提交
845 846 847
        sky_close = 1;
        return;
    }
S
songzhian 已提交
848 849 850
}


851 852
/* {{{ PHP_MINIT_FUNCTION
 */
H
heyanlong 已提交
853
PHP_MINIT_FUNCTION (skywalking) {
H
docs  
heyanlong 已提交
854
	ZEND_INIT_MODULE_GLOBALS(skywalking, php_skywalking_init_globals, NULL);
S
songzhian 已提交
855
	//data_register_hashtable();
856 857 858
	REGISTER_INI_ENTRIES();
	/* If you have INI entries, uncomment these lines
	*/
H
report  
heyanlong 已提交
859
	if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
860 861
		module_init();
		if (sky_close == 1) {
S
songzhian 已提交
862 863
			return SUCCESS;
		}
H
heyanlong 已提交
864
//		set_sampling_rate(SKY_G(global_sampling_rate));
865
		zend_function *old_function;
H
heyanlong 已提交
866
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_exec", sizeof("curl_exec") - 1)) != NULL) {
867
			orig_curl_exec = old_function->internal_function.handler;
H
heyanlong 已提交
868
			old_function->internal_function.handler = sky_curl_exec_handler;
869
		}
H
heyanlong 已提交
870 871 872 873
        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 = sky_curl_setopt_handler;
        }
H
heyanlong 已提交
874 875 876 877
        if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_setopt_array", sizeof("curl_setopt_array")-1)) != NULL) {
            orig_curl_setopt_array = old_function->internal_function.handler;
            old_function->internal_function.handler = sky_curl_setopt_array_handler;
        }
H
heyanlong 已提交
878 879 880 881
        if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_close", sizeof("curl_close")-1)) != NULL) {
            orig_curl_close = old_function->internal_function.handler;
            old_function->internal_function.handler = sky_curl_close_handler;
        }
882 883 884 885 886 887 888 889 890 891 892
	}

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(skywalking)
{
	UNREGISTER_INI_ENTRIES();
H
memory  
heyanlong 已提交
893
    return SUCCESS;
894 895 896 897
}
/* }}} */

/* Remove if there's nothing to do at request start */
S
songzhian 已提交
898
/* {{{ PHP_RINIT_FUNCTION7
899 900 901
 */
PHP_RINIT_FUNCTION(skywalking)
{
S
songzhian 已提交
902

903 904 905
#if defined(COMPILE_DL_SKYWALKING) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif
H
report  
heyanlong 已提交
906
	if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
907 908 909
        if (sky_close == 1) {
            return SUCCESS;
        }
H
report  
heyanlong 已提交
910 911 912 913
		sky_increment_id++;
		if (sky_increment_id >= 9999) {
			sky_increment_id = 0;
		}
H
memory  
heyanlong 已提交
914
        request_init();
H
report  
heyanlong 已提交
915
	}
916 917 918 919 920 921 922 923 924
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(skywalking)
{
H
heyanlong 已提交
925

H
report  
heyanlong 已提交
926
	if(SKYWALKING_G(enable)){
H
memory  
heyanlong 已提交
927 928 929
        if (sky_close == 1) {
            return SUCCESS;
        }
S
songzhian 已提交
930
		sky_flush_all();
H
memory  
heyanlong 已提交
931
        zval_dtor(&SKYWALKING_G(context));
H
heyanlong 已提交
932
        zval_dtor(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
933
        zval_dtor(&SKYWALKING_G(curl_header_send));
H
memory  
heyanlong 已提交
934
        zval_dtor(&SKYWALKING_G(UpstreamSegment));
935 936 937 938 939 940 941 942 943
	}
	return SUCCESS;
}
/* }}} */

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

945
	php_info_print_table_start();
H
docs  
heyanlong 已提交
946 947 948 949 950
    if (sky_close) {
        php_info_print_table_header(2, "skywalking Support", "disabled (register fail)");
    } else {
        php_info_print_table_header(2, "skywalking Support", "enabled");
    }
951 952 953 954 955 956 957 958

    size_t instance_size = 0;
    instance_size = snprintf(NULL, 0, "%d", application_instance);
    char *instance_id = (char *)emalloc(instance_size + 1);
    bzero(instance_id, instance_size + 1);
    snprintf(instance_id, instance_size+1, "%d", application_instance);
    php_info_print_table_header(2, "Instance ID", instance_id);

959 960 961
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
962
    efree(instance_id);
963 964 965
}
/* }}} */

H
memory  
heyanlong 已提交
966 967 968 969 970 971 972 973 974 975 976 977
/* {{{ PHP_MINFO_FUNCTION
 */
PHP_GINIT_FUNCTION(skywalking)
{
    memset(skywalking_globals, 0, sizeof(*skywalking_globals));
}
/* }}} */

zend_module_dep skywalking_deps[] = {
        ZEND_MOD_REQUIRED("json")
        {NULL, NULL, NULL}
};
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

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

#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
 */