skywalking.c 52.2 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 "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"
H
mysql  
heyanlong 已提交
37
#include "ext/pdo/php_pdo_driver.h"
38 39 40 41 42 43 44 45

#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 已提交
46 47 48 49 50 51 52 53 54
#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>

H
heyanlong 已提交
55
#include <sys/un.h>
56

H
heyanlong 已提交
57
#include "b64.h"
S
songzhian 已提交
58 59

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

/* True global resources - no need for thread safety here */
static int le_skywalking;
H
heyanlong 已提交
65 66
static int application_instance = 0;
static int application_id = 0;
S
songzhian 已提交
67 68
static int sky_close = 0;
static int sky_increment_id = 0;
H
heyanlong 已提交
69
static int cli_debug = 0;
70
static char *sock_path = "/tmp/sky_agent.sock";
S
songzhian 已提交
71

H
mysql  
heyanlong 已提交
72 73 74 75 76
static void (*ori_execute_ex)(zend_execute_data *execute_data);
static void (*ori_execute_internal)(zend_execute_data *execute_data, zval *return_value);
ZEND_API void sky_execute_ex(zend_execute_data *execute_data);
ZEND_API void sky_execute_internal(zend_execute_data *execute_data, zval *return_value);

77 78 79 80
/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini*/
PHP_INI_BEGIN()
H
heyanlong 已提交
81
	STD_PHP_INI_BOOLEAN("skywalking.enable",   	"0", PHP_INI_ALL, OnUpdateBool, enable, zend_skywalking_globals, skywalking_globals)
H
6.x  
heyanlong 已提交
82
	STD_PHP_INI_ENTRY("skywalking.version",   	"6", PHP_INI_ALL, OnUpdateLong, version, zend_skywalking_globals, skywalking_globals)
H
heyanlong 已提交
83
	STD_PHP_INI_ENTRY("skywalking.app_code", "hello_skywalking", PHP_INI_ALL, OnUpdateString, app_code, zend_skywalking_globals, skywalking_globals)
84
	STD_PHP_INI_ENTRY("skywalking.sock_path", "/tmp/sky_agent.sock", PHP_INI_ALL, OnUpdateString, sock_path, zend_skywalking_globals, skywalking_globals)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
PHP_INI_END()

/* }}} */

/* {{{ skywalking_functions[]
 *
 * Every user visible function must have an entry in skywalking_functions[].
 */
const zend_function_entry skywalking_functions[] = {
	PHP_FE_END	/* Must be the last line in skywalking_functions[] */
};
/* }}} */



const zend_function_entry class_skywalking[] = {
	PHP_FE_END
};

S
songzhian 已提交
104

H
mysql  
heyanlong 已提交
105
ZEND_API void sky_execute_ex(zend_execute_data *execute_data) {
H
predis  
heyanlong 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    zend_function *zf = execute_data->func;
    const char *class_name = (zf->common.scope != NULL && zf->common.scope->name != NULL) ? ZSTR_VAL(
            zf->common.scope->name) : NULL;
    const char *function_name = zf->common.function_name == NULL ? NULL : ZSTR_VAL(zf->common.function_name);

    char *operationName = NULL;
    if (class_name != NULL) {
        if (strcmp(class_name, "Predis\\Client") == 0 && strcmp(function_name, "__call") == 0) {
            // params
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
            if (arg_count) {
                zval *p = ZEND_CALL_ARG(execute_data, 1);

                if (Z_TYPE_P(p) == IS_STRING) {
                    operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
                    strcpy(operationName, class_name);
                    strcat(operationName, "->");
                    strcat(operationName, Z_STRVAL_P(p));
                }
            }
        }
    }

    if (operationName != NULL) {
        zval tags;
        array_init(&tags);

        if (strcmp(class_name, "Predis\\Client") == 0 && strcmp(function_name, "__call") == 0) {
H
predis  
heyanlong 已提交
135
            add_assoc_string(&tags, "db.type", "redis");
H
predis  
heyanlong 已提交
136 137
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
            zval *fname = ZEND_CALL_ARG(execute_data, 1);
138 139
	    int i;
            for (i = 1; i < arg_count + 1; ++i) {
H
predis  
heyanlong 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                if (i == 1) {
                    continue;
                }
                zval *pam = ZEND_CALL_ARG(execute_data, i);
                if (Z_TYPE_P(pam) == IS_ARRAY) {
                    zend_ulong num_key;
                    zval *entry;
                    ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(pam), num_key, entry)
                            {
                                char *fnamewall = (char *) emalloc(strlen(Z_STRVAL_P(fname)) + 3);
                                sprintf(fnamewall, "|%s|", Z_STRVAL_P(fname));
                                // first params
                                if (num_key == 0) {
                                    switch (Z_TYPE_P(entry)) {
                                        case IS_STRING:
                                            // string
                                            if (strstr(REDIS_KEY_STRING, fnamewall)) { // add tag key
                                                add_assoc_string(&tags, "redis.key", Z_STRVAL_P(entry));
                                            } else if (strstr(REDIS_OPERATION_STRING, fnamewall)) { // add tag operation
                                                add_assoc_string(&tags, "redis.operation", Z_STRVAL_P(entry));
                                            }
                                            break;
                                        case IS_ARRAY:

                                            break;
                                    }
                                }

                                efree(fnamewall);
                            }
                    ZEND_HASH_FOREACH_END();

                }
            }
        }

        zval temp;
        zval *spans = NULL;
        zval *span_id = NULL;
        zval *last_span = NULL;
        char *l_millisecond;
        long millisecond;
        array_init(&temp);
        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);

        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", 1);
        add_assoc_long(&temp, "componentId", COMPONENT_JEDIS);
        add_assoc_string(&temp, "operationName", operationName);
        add_assoc_string(&temp, "peer", "");
        efree(operationName);

        ori_execute_ex(execute_data);

        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);


        add_assoc_zval(&temp, "tags", &tags);
        add_assoc_long(&temp, "endTime", millisecond);
        add_assoc_long(&temp, "isError", 0);

        zend_hash_next_index_insert(Z_ARRVAL_P(spans), &temp);
    } else {
        ori_execute_ex(execute_data);
    }
H
mysql  
heyanlong 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

ZEND_API void sky_execute_internal(zend_execute_data *execute_data, zval *return_value) {

    if (sky_close == 1) {
        if (ori_execute_internal) {
            ori_execute_internal(execute_data, return_value);
        } else {
            execute_internal(execute_data, return_value);
        }
        return;
    }

    zend_function *zf = execute_data->func;
    const char *class_name = (zf->common.scope != NULL && zf->common.scope->name != NULL) ? ZSTR_VAL(
            zf->common.scope->name) : NULL;
    const char *function_name = zf->common.function_name == NULL ? NULL : ZSTR_VAL(zf->common.function_name);

    char *operationName = NULL;
    char *component = NULL;
    if (class_name != NULL) {
        if (strcmp(class_name, "PDO") == 0) {
            if (strcmp(function_name, "exec") == 0
                || strcmp(function_name, "query") == 0
                || strcmp(function_name, "prepare") == 0
                || strcmp(function_name, "commit") == 0) {
H
pdo  
heyanlong 已提交
241
                component = (char *) emalloc(strlen("PDO") + 1);
H
mysql  
heyanlong 已提交
242
                strcpy(component, "PDO");
H
pdo  
heyanlong 已提交
243 244 245 246 247 248 249 250 251 252
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
        } else if (strcmp(class_name, "PDOStatement") == 0) {
            if (strcmp(function_name, "execute") == 0) {
                component = (char *) emalloc(strlen("PDOStatement") + 1);
                strcpy(component, "PDOStatement");
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
H
mysql  
heyanlong 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
        }
    } else if (function_name != NULL) {
    }

    if (operationName != NULL) {

        zval tags;
        array_init(&tags);

H
pdo  
heyanlong 已提交
266 267 268 269 270 271 272 273 274 275
        if (strcmp(class_name, "PDO") == 0) {

            // params
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
            if (arg_count) {
                zval *p = ZEND_CALL_ARG(execute_data, 1);
                //db.statement
                switch (Z_TYPE_P(p)) {
                    case IS_STRING:
                        add_assoc_string(&tags, "db.statement", Z_STRVAL_P(p));
H
predis  
heyanlong 已提交
276
                        break;
H
pdo  
heyanlong 已提交
277 278

                }
H
mysql  
heyanlong 已提交
279 280 281 282 283 284
            }

            char db_type[64] = {0};
            pdo_dbh_t *dbh = Z_PDO_DBH_P(&(execute_data->This));
            if (dbh != NULL) {
                if (dbh->driver != NULL && dbh->driver->driver_name != NULL) {
H
pdo  
heyanlong 已提交
285
                    memcpy(db_type, (char *) dbh->driver->driver_name, dbh->driver->driver_name_len);
H
mysql  
heyanlong 已提交
286 287 288 289
                    add_assoc_string(&tags, "db.type", db_type);
                }

                if (dbh->data_source != NULL && db_type[0] != '\0') {
H
pdo  
heyanlong 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                    add_assoc_string(&tags, "db.data_source", (char *) dbh->data_source);
                }
            }
        } else if (strcmp(class_name, "PDOStatement") == 0) {
            char db_type[64] = {0};
            pdo_stmt_t *stmt = (pdo_stmt_t *) Z_PDO_STMT_P(&(execute_data->This));
            if (stmt != NULL) {
                add_assoc_string(&tags, "db.statement", stmt->query_string);

                if (stmt->dbh != NULL && stmt->dbh->driver->driver_name != NULL) {
                    memcpy(db_type, (char *) stmt->dbh->driver->driver_name, stmt->dbh->driver->driver_name_len);
                    add_assoc_string(&tags, "db.type", db_type);
                }

                if (db_type[0] != '\0' && stmt->dbh != NULL && stmt->dbh->data_source != NULL) {
                    add_assoc_string(&tags, "db.data_source", (char *) stmt->dbh->data_source);
H
mysql  
heyanlong 已提交
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 351 352 353 354 355 356 357 358 359
                }
            }
        }

        zval temp;
        zval *spans = NULL;
        zval *span_id = NULL;
        zval *last_span = NULL;
        char *l_millisecond;
        long millisecond;
        array_init(&temp);
        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);

        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", 1);
//        add_assoc_string(&temp, "component", component);
        add_assoc_long(&temp, "componentId", COMPONENT_MYSQL_JDBC_DRIVER);
        add_assoc_string(&temp, "operationName", operationName);
        add_assoc_string(&temp, "peer", "");
        efree(component);
        efree(operationName);

        if (ori_execute_internal) {
            ori_execute_internal(execute_data, return_value);
        } else {
            execute_internal(execute_data, return_value);
        }

        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);


        add_assoc_zval(&temp, "tags", &tags);
        add_assoc_long(&temp, "endTime", millisecond);
        add_assoc_long(&temp, "isError", 0);

        zend_hash_next_index_insert(Z_ARRVAL_P(spans), &temp);
    } else {
        if (ori_execute_internal) {
            ori_execute_internal(execute_data, return_value);
        } else {
            execute_internal(execute_data, return_value);
        }
    }
}
S
songzhian 已提交
360 361


362 363 364 365 366 367
/* 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 已提交
368
void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS)
369
{
H
heyanlong 已提交
370 371 372 373 374
    if(sky_close == 1) {
        orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

375 376 377 378 379
	zval		*zid;
	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
		return;
	}

H
heyanlong 已提交
380
	int is_send = 1;
H
heyanlong 已提交
381

H
report  
heyanlong 已提交
382 383 384 385 386
    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 已提交
387 388 389
    zval_dtor(&function_name);
    zval_dtor(&params[0]);

H
report  
heyanlong 已提交
390
    zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo),  ZEND_STRL("url"));
H
heyanlong 已提交
391
    char *url_str = Z_STRVAL_P(z_url);
H
heyanlong 已提交
392 393
    if(strlen(url_str) <= 0) {
        zval_dtor(&curlInfo);
H
heyanlong 已提交
394
        is_send = 0;
H
heyanlong 已提交
395
    }
H
heyanlong 已提交
396
    php_url *url_info = NULL;
H
heyanlong 已提交
397 398 399 400 401 402
    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 已提交
403 404 405
        }
    }

G
goerzh 已提交
406
    char *sw = NULL;
H
heyanlong 已提交
407 408 409
    zval *spans = NULL;
    zval *last_span = NULL;
    zval *span_id = NULL;
H
heyanlong 已提交
410
    char *peer = NULL;
H
heyanlong 已提交
411 412
    ssize_t operation_name_l = 0;
    char *operation_name = NULL;
H
heyanlong 已提交
413 414 415
    ssize_t full_url_l = 0;
    char *full_url = NULL;

H
heyanlong 已提交
416 417 418 419 420 421
    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 已提交
422
                peer_port = 80;
H
heyanlong 已提交
423
            } else {
H
heyanlong 已提交
424
                peer_port = 443;
H
heyanlong 已提交
425 426
            }
        }
H
heyanlong 已提交
427

428
        peer = (char *) emalloc(strlen(url_info->scheme) + 3 + strlen(url_info->host) + 7);
H
strlen  
heyanlong 已提交
429
        bzero(peer, strlen(url_info->scheme) + 3 + strlen(url_info->host) + 7);
G
goerzh 已提交
430

G
goerzh 已提交
431 432
        if (url_info->query) {
            if (url_info->path == NULL) {
H
heyanlong 已提交
433
                operation_name_l = snprintf(NULL, 0, "%s", "/");
G
goerzh 已提交
434 435
                operation_name = (char *) emalloc(operation_name_l + 1);
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
436 437 438 439 440 441
                sprintf(operation_name, "%s", "/");

                full_url_l = snprintf(NULL, 0, "%s?%s", "/", url_info->query);
                full_url = (char *) emalloc(full_url_l + 1);
                bzero(full_url, full_url_l + 1);
                sprintf(full_url, "%s?%s", "/", url_info->query);
G
goerzh 已提交
442
            } else {
H
heyanlong 已提交
443
                operation_name_l = snprintf(NULL, 0, "%s", url_info->path);
G
goerzh 已提交
444 445
                operation_name = (char *) emalloc(operation_name_l + 1);
                bzero(operation_name, operation_name_l + 1);
H
heyanlong 已提交
446 447 448 449 450 451
                sprintf(operation_name, "%s", url_info->path);

                full_url_l = snprintf(NULL, 0, "%s?%s", url_info->path, url_info->query);
                full_url = (char *) emalloc(full_url_l + 1);
                bzero(full_url, full_url_l + 1);
                sprintf(full_url, "%s?%s", url_info->path, url_info->query);
G
goerzh 已提交
452 453 454 455 456 457 458
            }
        } else {
            if (url_info->path == NULL) {
                operation_name_l = snprintf(NULL, 0, "%s", "/");
                operation_name = (char *) emalloc(operation_name_l + 1);
                bzero(operation_name, operation_name_l + 1);
                sprintf(operation_name, "%s", "/");
H
heyanlong 已提交
459 460 461 462 463

                full_url_l = snprintf(NULL, 0, "%s", "/");
                full_url = (char *) emalloc(full_url_l + 1);
                bzero(full_url, full_url_l + 1);
                sprintf(full_url, "%s", "/");
G
goerzh 已提交
464
            } else {
G
goerzh 已提交
465
                operation_name_l = snprintf(NULL, 0, "%s", url_info->path);
G
goerzh 已提交
466 467
                operation_name = (char *) emalloc(operation_name_l + 1);
                bzero(operation_name, operation_name_l + 1);
G
goerzh 已提交
468
                sprintf(operation_name, "%s", url_info->path);
H
heyanlong 已提交
469 470 471 472 473

                full_url_l = snprintf(NULL, 0, "%s", url_info->path);
                full_url = (char *) emalloc(full_url_l + 1);
                bzero(full_url, full_url_l + 1);
                sprintf(full_url, "%s", url_info->path);
G
goerzh 已提交
474 475
            }
        }
G
goerzh 已提交
476

H
heyanlong 已提交
477 478 479
        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);
480
        if (SKYWALKING_G(version) == 5) { // skywalking 5.x
H
6.x  
heyanlong 已提交
481
            sprintf(peer, "%s://%s:%d", url_info->scheme, url_info->host, peer_port);
482 483
            sw = generate_sw3(Z_LVAL_P(span_id) + 1, peer, operation_name);
        } else if (SKYWALKING_G(version) == 6) { // skywalking 6.x
H
6.x  
heyanlong 已提交
484 485
            sprintf(peer, "%s:%d", url_info->host, peer_port);
            sw = generate_sw6(Z_LVAL_P(span_id) + 1, peer, operation_name);
G
goerzh 已提交
486
        }
H
report  
heyanlong 已提交
487 488
    }

H
heyanlong 已提交
489

G
goerzh 已提交
490
    if (sw != NULL) {
H
heyanlong 已提交
491 492 493 494 495 496 497 498 499
        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 已提交
500
        }
H
heyanlong 已提交
501

G
goerzh 已提交
502
        add_next_index_string(option, sw);
H
heyanlong 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
        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 已提交
519
        }
H
heyanlong 已提交
520 521 522
        zval_dtor(&argv[0]);
        zval_dtor(&argv[1]);
        zval_dtor(&argv[2]);
G
goerzh 已提交
523
        efree(sw);
H
report  
heyanlong 已提交
524
    }
H
heyanlong 已提交
525 526 527 528

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

H
heyanlong 已提交
531
        array_init(&temp);
H
heyanlong 已提交
532

H
heyanlong 已提交
533 534 535 536 537 538 539 540 541
        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 已提交
542
    }
H
heyanlong 已提交
543 544


H
heyanlong 已提交
545
	orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
546

H
heyanlong 已提交
547 548 549 550 551 552 553 554
    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 已提交
555

H
heyanlong 已提交
556 557 558 559
        zval *z_http_code;
        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);
H
heyanlong 已提交
560

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

H
heyanlong 已提交
563
        add_assoc_long(&temp, "endTime", millisecond);
H
heyanlong 已提交
564 565


G
goerzh 已提交
566
        add_assoc_string(&temp, "operationName", operation_name);
567
        add_assoc_string(&temp, "peer", peer);
H
heyanlong 已提交
568 569 570 571 572
        zval tags;
        array_init(&tags);
        add_assoc_string(&tags, "url", full_url);

        add_assoc_zval(&temp, "tags", &tags);
H
heyanlong 已提交
573 574
        efree(peer);
        efree(operation_name);
H
heyanlong 已提交
575
        efree(full_url);
H
heyanlong 已提交
576

H
heyanlong 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590
        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);
    }
591
}
H
heyanlong 已提交
592 593

void sky_curl_setopt_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
594 595 596 597 598
    if(sky_close == 1) {
        orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
599 600 601 602 603 604 605
    zval *zid, *zvalue;
    zend_long options;

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

H
heyanlong 已提交
606 607 608 609 610 611 612 613 614 615
    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 已提交
616
        } else {
H
heyanlong 已提交
617
            orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
618 619 620
        }
    }
}
H
heyanlong 已提交
621 622

void sky_curl_setopt_array_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
623 624 625 626 627 628

    if(sky_close == 1) {
        orig_curl_setopt_array(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
629 630 631
    zval *zid, *arr, *entry;
    zend_ulong option;
    zend_string *string_key;
H
heyanlong 已提交
632 633 634 635 636 637

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

H
heyanlong 已提交
638 639 640 641 642 643
    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 已提交
644
    }
H
heyanlong 已提交
645 646 647 648

    orig_curl_setopt_array(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}

H
heyanlong 已提交
649
void sky_curl_close_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
650 651 652 653 654 655

    if(sky_close == 1) {
        orig_curl_close(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
656 657 658 659 660 661
    zval *zid;

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

H
heyanlong 已提交
662 663 664
    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 已提交
665 666 667 668
    }

    orig_curl_close(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
669 670 671 672 673
/* {{{ 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 已提交
674
	skywalking_globals->app_code = NULL;
H
heyanlong 已提交
675
	skywalking_globals->enable = 0;
H
6.x  
heyanlong 已提交
676
	skywalking_globals->version = 6;
677
	skywalking_globals->sock_path = "/tmp/sky_agent.sock";
678 679 680 681 682 683 684
}



static char *sky_json_encode(zval *parameter){

	smart_str buf = {0};
H
report  
heyanlong 已提交
685
	zend_long options = 64;
686 687 688 689 690 691 692 693 694 695
#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 已提交
696 697 698
	char *bufs = ZSTR_VAL(buf.s);
	smart_str_free(&buf);
	return bufs;
699 700
}

S
songzhian 已提交
701

H
memory  
heyanlong 已提交
702
static void write_log(char *text) {
H
heyanlong 已提交
703
    if (application_instance != 0) {
H
heyanlong 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
        // to stream

        struct sockaddr_un un;
        un.sun_family = AF_UNIX;
        strcpy(un.sun_path, sock_path);
        int fd;
        char message[strlen(text) + 2];

        fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd >= 0) {
            struct timeval tv;
            tv.tv_sec = 0;
            tv.tv_usec = 100000;
            setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
            int conn = connect(fd, (struct sockaddr *) &un, sizeof(un));

            if (conn >= 0) {
                bzero(message, strlen(text) + 2);
                sprintf(message, "1%s\n", text);
                write(fd, message, strlen(message));
            }
            close(fd);
        }
H
report  
heyanlong 已提交
727
    }
728 729 730 731

}


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

H
heyanlong 已提交
734 735 736 737 738 739 740
    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 已提交
741 742 743 744 745 746 747 748 749 750
    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 已提交
751 752
}

G
goerzh 已提交
753 754 755 756 757 758 759 760
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);
H
6.x  
heyanlong 已提交
761 762 763 764 765 766 767 768
    zval distributedTraceIdEncode;
    zval traceSegmentIdEncode;
    zval peerHostEncode;
    zval entryEndpointNameEncode;
    zval parentEndpointNameEncode;

    char *sharpPeer = (char *) emalloc(sizeof(peer_host) + 1);
    sprintf(sharpPeer, "#%s", peer_host);
H
6.x  
heyanlong 已提交
769
    char *sharpEntryEndpointName = (char *) emalloc(sizeof(Z_STRVAL_P(entryOperationName)) + 1);
H
6.x  
heyanlong 已提交
770
    sprintf(sharpEntryEndpointName, "#%s", Z_STRVAL_P(entryOperationName));
H
6.x  
heyanlong 已提交
771
    char *sharpParentEndpointName = (char *) emalloc(sizeof(operation_name) + 1);
H
6.x  
heyanlong 已提交
772 773 774 775 776 777 778 779
    sprintf(sharpParentEndpointName, "#%s", operation_name);


    zval_b64_encode(&distributedTraceIdEncode, Z_STRVAL_P(distributedTraceId));
    zval_b64_encode(&traceSegmentIdEncode, Z_STRVAL_P(traceId));
    zval_b64_encode(&peerHostEncode, sharpPeer);
    zval_b64_encode(&entryEndpointNameEncode, sharpEntryEndpointName);
    zval_b64_encode(&parentEndpointNameEncode, sharpParentEndpointName);
G
goerzh 已提交
780 781

    ssize_t sw6_l = 0;
H
6.x  
heyanlong 已提交
782 783 784 785
    sw6_l = snprintf(NULL, 0, "sw6: 1-%s-%s-%d-%d-%d-%s-%s-%s", Z_STRVAL(distributedTraceIdEncode),
                     Z_STRVAL(traceSegmentIdEncode), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
                     Z_STRVAL(peerHostEncode), Z_STRVAL(entryEndpointNameEncode),
                     Z_STRVAL(parentEndpointNameEncode));
G
goerzh 已提交
786

H
6.x  
heyanlong 已提交
787
    char *sw6 = (char *) emalloc(sw6_l + 1);
G
goerzh 已提交
788
    bzero(sw6, sw6_l + 1);
H
6.x  
heyanlong 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801
    snprintf(sw6, sw6_l + 1, "sw6: 1-%s-%s-%d-%d-%d-%s-%s-%s", Z_STRVAL(distributedTraceIdEncode),
             Z_STRVAL(traceSegmentIdEncode), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
             Z_STRVAL(peerHostEncode), Z_STRVAL(entryEndpointNameEncode),
             Z_STRVAL(parentEndpointNameEncode));

    efree(sharpPeer);
    efree(sharpEntryEndpointName);
    efree(sharpParentEndpointName);
    zval_dtor(&distributedTraceIdEncode);
    zval_dtor(&traceSegmentIdEncode);
    zval_dtor(&peerHostEncode);
    zval_dtor(&entryEndpointNameEncode);
    zval_dtor(&parentEndpointNameEncode);
G
goerzh 已提交
802 803 804
    return sw6;
}

H
heyanlong 已提交
805 806 807 808
static zend_string *trim_sharp(zval *tmp) {
    return php_trim(Z_STR_P(tmp), "#", sizeof("#") - 1, 1);
}

G
goerzh 已提交
809
static void zval_b64_encode(zval *out, char *in) {
H
6.x  
heyanlong 已提交
810 811
    char *enc = b64_encode((const unsigned char*)in, strlen(in));
    ZVAL_STRING(out, enc);
G
goerzh 已提交
812 813 814 815
    free(enc);
}

static void zval_b64_decode(zval *out, char *in) {
H
6.x  
heyanlong 已提交
816 817
    unsigned char *dec = b64_decode((const char *) in, strlen(in));
    ZVAL_STRING(out, (char *) dec);
G
goerzh 已提交
818 819 820
    free(dec);
}

H
report  
heyanlong 已提交
821 822 823 824 825 826
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 已提交
827

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

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

H
report  
heyanlong 已提交
832
    add_assoc_string(&SKYWALKING_G(context), "currentTraceId", makeTraceId);
H
heyanlong 已提交
833
    add_assoc_long(&SKYWALKING_G(context), "isChild", 0);
H
heyanlong 已提交
834

H
report  
heyanlong 已提交
835 836
    // parent
    zval *carrier = NULL;
H
heyanlong 已提交
837
    zval *sw;
G
goerzh 已提交
838

H
report  
heyanlong 已提交
839
    zend_bool jit_initialization = PG(auto_globals_jit);
H
heyanlong 已提交
840

H
report  
heyanlong 已提交
841 842 843 844 845
    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 已提交
846
    carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"));
G
goerzh 已提交
847

H
heyanlong 已提交
848 849
    if(SKYWALKING_G(version) == 5) {
        sw = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW3", sizeof("HTTP_SW3") - 1);
G
goerzh 已提交
850

H
heyanlong 已提交
851 852
        if (sw != NULL && Z_TYPE_P(sw) == IS_STRING && Z_STRLEN_P(sw) > 10) {
            add_assoc_string(&SKYWALKING_G(context), "sw3", Z_STRVAL_P(sw));
G
goerzh 已提交
853 854 855 856

            zval temp;
            array_init(&temp);

H
heyanlong 已提交
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
            php_explode(zend_string_init(ZEND_STRL("|"), 0), Z_STR_P(sw), &temp, 10);

            if(zend_array_count(Z_ARRVAL_P(&temp)) >= 8) {
                zval *sw3_0 = zend_hash_index_find(Z_ARRVAL(temp), 0);
                zval *sw3_1 = zend_hash_index_find(Z_ARRVAL(temp), 1);
                zval *sw3_2 = zend_hash_index_find(Z_ARRVAL(temp), 2);
                zval *sw3_3 = zend_hash_index_find(Z_ARRVAL(temp), 3);
                zval *sw3_4 = zend_hash_index_find(Z_ARRVAL(temp), 4);
                zval *sw3_5 = zend_hash_index_find(Z_ARRVAL(temp), 5);
                zval *sw3_6 = zend_hash_index_find(Z_ARRVAL(temp), 6);
                zval *sw3_7 = zend_hash_index_find(Z_ARRVAL(temp), 7);

                zval child;
                array_init(&child);
                ZVAL_LONG(&child, 1);
                zend_hash_str_update(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1, &child);

                add_assoc_string(&SKYWALKING_G(context), "parentTraceSegmentId", Z_STRVAL_P(sw3_0));
                add_assoc_long(&SKYWALKING_G(context), "parentSpanId", zend_atol(Z_STRVAL_P(sw3_1), sizeof(Z_STRVAL_P(sw3_1)) - 1));
                add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", zend_atol(Z_STRVAL_P(sw3_2), sizeof(Z_STRVAL_P(sw3_2)) - 1));
                add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", zend_atol(Z_STRVAL_P(sw3_3), sizeof(Z_STRVAL_P(sw3_3)) - 1));
                add_assoc_str(&SKYWALKING_G(context), "networkAddress", trim_sharp(sw3_4));
                add_assoc_str(&SKYWALKING_G(context), "entryOperationName", trim_sharp(sw3_5));
                add_assoc_str(&SKYWALKING_G(context), "parentOperationName", trim_sharp(sw3_6));
                add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", Z_STRVAL_P(sw3_7));
G
goerzh 已提交
882
            }
H
heyanlong 已提交
883 884 885 886 887 888 889 890 891 892
        } else {
            add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance);
            add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance);
            add_assoc_string(&SKYWALKING_G(context), "entryOperationName", get_page_request_uri());
            add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId);
        }
    } else if (SKYWALKING_G(version) == 6) {
        sw = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW6", sizeof("HTTP_SW6") - 1);
        if (sw != NULL && Z_TYPE_P(sw) == IS_STRING && Z_STRLEN_P(sw) > 10) {
            add_assoc_string(&SKYWALKING_G(context), "sw6", Z_STRVAL_P(sw));
H
6.x  
heyanlong 已提交
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

            zval temp;
            array_init(&temp);

            php_explode(zend_string_init(ZEND_STRL("-"), 0), Z_STR_P(sw), &temp, 10);

            if(zend_array_count(Z_ARRVAL_P(&temp)) >= 7) {
                zval *sw6_0 = zend_hash_index_find(Z_ARRVAL(temp), 0);
                zval *sw6_1 = zend_hash_index_find(Z_ARRVAL(temp), 1); // Trace Id
                zval *sw6_2 = zend_hash_index_find(Z_ARRVAL(temp), 2); // Parent trace segment Id
                zval *sw6_3 = zend_hash_index_find(Z_ARRVAL(temp), 3); // Parent span Id
                zval *sw6_4 = zend_hash_index_find(Z_ARRVAL(temp), 4); // Parent service instance Id
                zval *sw6_5 = zend_hash_index_find(Z_ARRVAL(temp), 5); // Entrance service instance Id
                zval *sw6_6 = zend_hash_index_find(Z_ARRVAL(temp), 6); // Target address of this request

                zval *sw6_7 = NULL;
                zval *sw6_8 = NULL;
                if (zend_array_count(Z_ARRVAL_P(&temp)) >= 9) {
                    sw6_7 = zend_hash_index_find(Z_ARRVAL(temp), 7);
                    sw6_8 = zend_hash_index_find(Z_ARRVAL(temp), 8);
                }


                zval child;
                array_init(&child);
H
6.x  
heyanlong 已提交
918
                ZVAL_LONG(&child, 1)
H
6.x  
heyanlong 已提交
919 920
                zend_hash_str_update(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1, &child);

H
6.x  
heyanlong 已提交
921 922 923 924 925 926 927 928
                zval sw6_1decode;
                zval sw6_2decode;
                zval sw6_6decode;
                zval_b64_decode(&sw6_1decode, Z_STRVAL_P(sw6_1));
                zval_b64_decode(&sw6_2decode, Z_STRVAL_P(sw6_2));
                zval_b64_decode(&sw6_6decode, Z_STRVAL_P(sw6_6));

                add_assoc_string(&SKYWALKING_G(context), "parentTraceSegmentId", Z_STRVAL(sw6_2decode));
H
6.x  
heyanlong 已提交
929 930 931
                add_assoc_long(&SKYWALKING_G(context), "parentSpanId", zend_atol(Z_STRVAL_P(sw6_3), sizeof(Z_STRVAL_P(sw6_3)) - 1));
                add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", zend_atol(Z_STRVAL_P(sw6_4), sizeof(Z_STRVAL_P(sw6_4)) - 1));
                add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", zend_atol(Z_STRVAL_P(sw6_5), sizeof(Z_STRVAL_P(sw6_5)) - 1));
H
6.x  
heyanlong 已提交
932
                add_assoc_str(&SKYWALKING_G(context), "networkAddress", trim_sharp(&sw6_6decode));
H
6.x  
heyanlong 已提交
933
                if (sw6_7 != NULL && sw6_8 != NULL) {
H
6.x  
heyanlong 已提交
934 935 936 937 938 939 940 941 942 943

                    zval sw6_7decode;
                    zval sw6_8decode;
                    zval_b64_decode(&sw6_7decode, Z_STRVAL_P(sw6_7));
                    zval_b64_decode(&sw6_8decode, Z_STRVAL_P(sw6_8));

                    add_assoc_str(&SKYWALKING_G(context), "entryOperationName", trim_sharp(&sw6_7decode));
                    add_assoc_str(&SKYWALKING_G(context), "parentOperationName", trim_sharp(&sw6_8decode));
                    zval_dtor(&sw6_7decode);
                    zval_dtor(&sw6_8decode);
H
6.x  
heyanlong 已提交
944
                }
H
6.x  
heyanlong 已提交
945 946 947 948 949
                add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", Z_STRVAL(sw6_1decode));

                zval_dtor(&sw6_1decode);
                zval_dtor(&sw6_2decode);
                zval_dtor(&sw6_6decode);
H
6.x  
heyanlong 已提交
950
            }
H
6.x  
heyanlong 已提交
951 952 953 954 955
        } else {
            add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance);
            add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance);
            add_assoc_string(&SKYWALKING_G(context), "entryOperationName", get_page_request_uri());
            add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId);
G
goerzh 已提交
956 957 958
        }
    }

H
memory  
heyanlong 已提交
959
    efree(makeTraceId);
960 961
}

H
report  
heyanlong 已提交
962 963 964 965 966 967 968

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

969 970 971 972 973 974 975 976 977 978 979 980 981 982
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 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
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 已提交
1006 1007 1008
    char *uris = ZSTR_VAL(uri.s);
    smart_str_free(&uri);
    return uris;
H
heyanlong 已提交
1009
}
1010

G
goerzh 已提交
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 1036 1037 1038 1039
static char *get_page_request_peer() {
    zval *carrier = NULL;
    zval *request_host = NULL;
    zval *request_port = NULL;

    char *peer = NULL;
    size_t peer_l = 0;

    zend_bool jit_initialization = PG(auto_globals_jit);

    if (jit_initialization) {
        zend_string *server_str = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0);
        zend_is_auto_global(server_str);
        zend_string_release(server_str);
    }
    carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"));

    request_host = zend_hash_str_find(Z_ARRVAL_P(carrier), "SERVER_ADDR", sizeof("SERVER_ADDR") - 1);
    request_port = zend_hash_str_find(Z_ARRVAL_P(carrier), "SERVER_PORT", sizeof("SERVER_PORT") - 1);

    if (request_host != NULL && request_port != NULL) {
        peer_l = snprintf(NULL, 0, "%s:%s", Z_STRVAL_P(request_host), Z_STRVAL_P(request_port));
        peer = emalloc(peer_l + 1);
        snprintf(peer, peer_l + 1, "%s:%s", Z_STRVAL_P(request_host), Z_STRVAL_P(request_port));
    }

    return peer;
}

1040

S
songzhian 已提交
1041
/**
H
report  
heyanlong 已提交
1042
 * ip
S
songzhian 已提交
1043 1044 1045 1046 1047 1048
 * 
 * @since 2017年11月23日
 * @copyright
 * @return return_type
//  */
static char* _get_current_machine_ip(){
G
goerzh 已提交
1049 1050 1051
    char *ip;
    ip = (char *) emalloc(sizeof(char) * 100);
    bzero(ip, 100);
S
songzhian 已提交
1052

G
goerzh 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    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);
        if (hent == NULL) {
            strcpy(ip, "127.0.0.1");
        } else {
            ip = inet_ntoa(*(struct in_addr *) (hent->h_addr_list[0]));
        }
    }
S
songzhian 已提交
1066 1067 1068 1069

    return ip;
}

H
heyanlong 已提交
1070
static void request_init() {
S
songzhian 已提交
1071

H
heyanlong 已提交
1072
    array_init(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
1073
    array_init(&SKYWALKING_G(curl_header_send));
H
heyanlong 已提交
1074 1075
    array_init(&SKYWALKING_G(context));
	array_init(&SKYWALKING_G(UpstreamSegment));
S
songzhian 已提交
1076

H
report  
heyanlong 已提交
1077
    generate_context();
S
songzhian 已提交
1078

H
report  
heyanlong 已提交
1079
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_instance", application_instance);
H
heyanlong 已提交
1080
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "pid", getppid());
H
report  
heyanlong 已提交
1081
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_id", application_id);
H
heyanlong 已提交
1082
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "version", SKYWALKING_G(version));
H
heyanlong 已提交
1083
	SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "segment");
H
report  
heyanlong 已提交
1084 1085 1086
	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 已提交
1087

H
heyanlong 已提交
1088 1089 1090 1091
	zval traceSegmentObject;
	zval spans;
	array_init(&spans);
	array_init(&traceSegmentObject);
H
report  
heyanlong 已提交
1092 1093
	add_assoc_string(&traceSegmentObject, "traceSegmentId", Z_STRVAL_P(traceId));
	add_assoc_long(&traceSegmentObject, "isSizeLimited", 0);
S
songzhian 已提交
1094

H
heyanlong 已提交
1095
	zval temp;
H
heyanlong 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    char *peer = NULL;
    char *uri = get_page_request_uri();
    char *path = (char*)emalloc(sizeof(char) * strlen(uri) + 5);

    int i;
    for(i = 0; i < strlen(uri); i++) {
        if (uri[i] == '?') {
            break;
        }
        path[i] = uri[i];
    }
    path[i] = '\0';
G
goerzh 已提交
1108

H
heyanlong 已提交
1109
	array_init(&temp);
H
heyanlong 已提交
1110 1111 1112 1113 1114 1115 1116
    peer = get_page_request_peer();

    zval tags;
    array_init(&tags);
    add_assoc_string(&tags, "url", (uri == NULL) ? "" : uri);

    add_assoc_zval(&temp, "tags", &tags);
S
songzhian 已提交
1117

H
heyanlong 已提交
1118 1119 1120 1121 1122 1123
    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);
H
heyanlong 已提交
1124 1125
    add_assoc_string(&temp, "operationName", path);
    efree(path);
G
goerzh 已提交
1126
    add_assoc_string(&temp, "peer", (peer == NULL) ? "" : peer);
H
heyanlong 已提交
1127 1128 1129
    add_assoc_long(&temp, "spanType", 0);
    add_assoc_long(&temp, "spanLayer", 3);
    add_assoc_long(&temp, "componentId", COMPONENT_HTTPCLIENT);
S
songzhian 已提交
1130

G
goerzh 已提交
1131 1132 1133 1134
    if (peer != NULL) {
        efree(peer);
    }

H
heyanlong 已提交
1135 1136 1137 1138
    zval *isChild = zend_hash_str_find(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1);
    // refs
    zval refs;
    array_init(&refs);
H
heyanlong 已提交
1139 1140 1141 1142 1143

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

H
heyanlong 已提交
1144
    if(Z_LVAL_P(isChild) == 1) {
H
memory  
heyanlong 已提交
1145 1146
        zval ref;
        array_init(&ref);
H
heyanlong 已提交
1147 1148 1149 1150 1151 1152
        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 已提交
1153
        zval *parentOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentOperationName", sizeof("parentOperationName") - 1);
H
heyanlong 已提交
1154
        zval *distributedTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "distributedTraceId", sizeof("distributedTraceId") - 1);
H
heyanlong 已提交
1155 1156 1157 1158 1159 1160 1161
        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 已提交
1162
        add_assoc_string(&ref, "parentServiceName", Z_STRVAL_P(parentOperationName));
H
heyanlong 已提交
1163
        zend_hash_next_index_insert(Z_ARRVAL(refs), &ref);
H
heyanlong 已提交
1164 1165 1166
        ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(distributedTraceId));
    } else {
        ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(traceId));
H
heyanlong 已提交
1167 1168 1169 1170

    }

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

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

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

H
report  
heyanlong 已提交
1177 1178
    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 已提交
1179 1180 1181
}


H
report  
heyanlong 已提交
1182
static void sky_flush_all() {
S
songzhian 已提交
1183
	char *l_millisecond = get_millisecond();
H
heyanlong 已提交
1184 1185 1186 1187
	long millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
	efree(l_millisecond);

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

H
heyanlong 已提交
1189 1190
	add_assoc_long(span, "endTime", millisecond);
	if ((SG(sapi_headers).http_response_code >= 500)) {
H
heyanlong 已提交
1191
		add_assoc_long(span, "isError", 1);
H
heyanlong 已提交
1192
	} else {
H
heyanlong 已提交
1193
		add_assoc_long(span, "isError", 0);
H
heyanlong 已提交
1194
	}
S
songzhian 已提交
1195

H
heyanlong 已提交
1196 1197
	write_log(sky_json_encode(&SKYWALKING_G(UpstreamSegment)));
}
S
songzhian 已提交
1198

H
heyanlong 已提交
1199 1200 1201 1202 1203 1204
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 已提交
1205

H
heyanlong 已提交
1206 1207 1208 1209
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 已提交
1210 1211 1212
}


H
heyanlong 已提交
1213
static int sky_register() {
H
heyanlong 已提交
1214
    if (application_instance == 0) {
H
heyanlong 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223
        struct sockaddr_un un;
        un.sun_family = AF_UNIX;
        strcpy(un.sun_path, sock_path);
        int fd;
        char message[4096];
        char return_message[4096];

        fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd >= 0) {
H
heyanlong 已提交
1224 1225 1226
            struct timeval tv;
            tv.tv_sec = 0;
            tv.tv_usec = 100000;
H
heyanlong 已提交
1227
            setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv, sizeof tv);
H
heyanlong 已提交
1228 1229 1230 1231
            int conn = connect(fd, (struct sockaddr *) &un, sizeof(un));

            if (conn >= 0) {
                bzero(message, sizeof(message));
H
heyanlong 已提交
1232 1233
                sprintf(message, "0{\"app_code\":\"%s\",\"pid\":%d,\"version\":%d}\n", SKYWALKING_G(app_code),
                        getppid(), SKYWALKING_G(version));
H
heyanlong 已提交
1234 1235 1236 1237 1238
                write(fd, message, strlen(message));

                bzero(return_message, sizeof(return_message));
                read(fd, return_message, sizeof(return_message));

H
heyanlong 已提交
1239
                char *ids[10] = {0};
H
heyanlong 已提交
1240 1241 1242 1243
                int i = 0;
                char *p = strtok(return_message, ",");
                while (p != NULL) {
                    ids[i++] = p;
H
heyanlong 已提交
1244
                    p = strtok(NULL, ",");
H
heyanlong 已提交
1245
                }
H
heyanlong 已提交
1246

H
heyanlong 已提交
1247 1248 1249 1250
                if (ids[0] != NULL && ids[1] != NULL) {
                    application_id = atoi(ids[0]);
                    application_instance = atoi(ids[1]);
                }
H
heyanlong 已提交
1251 1252 1253 1254 1255 1256 1257 1258
            }

            close(fd);
        }
    }
    return 0;
}

S
songzhian 已提交
1259

1260 1261
/* {{{ PHP_MINIT_FUNCTION
 */
H
heyanlong 已提交
1262
PHP_MINIT_FUNCTION (skywalking) {
H
docs  
heyanlong 已提交
1263
	ZEND_INIT_MODULE_GLOBALS(skywalking, php_skywalking_init_globals, NULL);
S
songzhian 已提交
1264
	//data_register_hashtable();
1265 1266 1267
	REGISTER_INI_ENTRIES();
	/* If you have INI entries, uncomment these lines
	*/
H
report  
heyanlong 已提交
1268
	if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
1269
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
H
heyanlong 已提交
1270
            return SUCCESS;
1271 1272
        }

H
predis  
heyanlong 已提交
1273 1274 1275
        ori_execute_ex = zend_execute_ex;
        zend_execute_ex = sky_execute_ex;

H
mysql  
heyanlong 已提交
1276 1277 1278
        ori_execute_internal = zend_execute_internal;
        zend_execute_internal = sky_execute_internal;

H
heyanlong 已提交
1279
		// bind curl
1280
		zend_function *old_function;
H
heyanlong 已提交
1281
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_exec", sizeof("curl_exec") - 1)) != NULL) {
1282
			orig_curl_exec = old_function->internal_function.handler;
H
heyanlong 已提交
1283
			old_function->internal_function.handler = sky_curl_exec_handler;
1284
		}
H
heyanlong 已提交
1285 1286 1287 1288
        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 已提交
1289 1290 1291 1292
        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 已提交
1293 1294 1295 1296
        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;
        }
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	}

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(skywalking)
{
	UNREGISTER_INI_ENTRIES();
H
memory  
heyanlong 已提交
1308
    return SUCCESS;
1309 1310 1311 1312
}
/* }}} */

/* Remove if there's nothing to do at request start */
S
songzhian 已提交
1313
/* {{{ PHP_RINIT_FUNCTION7
1314 1315 1316
 */
PHP_RINIT_FUNCTION(skywalking)
{
S
songzhian 已提交
1317

1318 1319 1320
#if defined(COMPILE_DL_SKYWALKING) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif
H
heyanlong 已提交
1321
    if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
1322 1323 1324
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
            return SUCCESS;
        }
H
heyanlong 已提交
1325 1326
        sky_register();
        if (application_instance == 0) {
H
heyanlong 已提交
1327
            sky_close = 1;
H
heyanlong 已提交
1328
            return SUCCESS;
H
heyanlong 已提交
1329 1330
        } else {
            sky_close = 0;
H
heyanlong 已提交
1331
        }
H
heyanlong 已提交
1332 1333 1334 1335
        sky_increment_id++;
        if (sky_increment_id >= 9999) {
            sky_increment_id = 0;
        }
H
memory  
heyanlong 已提交
1336
        request_init();
H
heyanlong 已提交
1337 1338
    }
    return SUCCESS;
1339 1340 1341 1342 1343 1344 1345 1346
}
/* }}} */

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

H
report  
heyanlong 已提交
1348
	if(SKYWALKING_G(enable)){
H
heyanlong 已提交
1349 1350 1351
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
            return SUCCESS;
        }
H
memory  
heyanlong 已提交
1352 1353 1354
        if (sky_close == 1) {
            return SUCCESS;
        }
S
songzhian 已提交
1355
		sky_flush_all();
H
memory  
heyanlong 已提交
1356
        zval_dtor(&SKYWALKING_G(context));
H
heyanlong 已提交
1357
        zval_dtor(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
1358
        zval_dtor(&SKYWALKING_G(curl_header_send));
H
memory  
heyanlong 已提交
1359
        zval_dtor(&SKYWALKING_G(UpstreamSegment));
1360 1361 1362 1363 1364 1365 1366 1367 1368
	}
	return SUCCESS;
}
/* }}} */

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

1370
	php_info_print_table_start();
H
phpinfo  
heyanlong 已提交
1371 1372
    if (SKYWALKING_G(enable)) {
        php_info_print_table_header(2, "SkyWalking Support", "enabled");
H
docs  
heyanlong 已提交
1373
    } else {
H
phpinfo  
heyanlong 已提交
1374
        php_info_print_table_header(2, "SkyWalking Support", "disabled");
H
docs  
heyanlong 已提交
1375
    }
1376

H
heyanlong 已提交
1377
    php_info_print_table_header(2, "SkyWalking Agent", "/tmp/sky_agent.sock");
1378

1379 1380 1381 1382 1383 1384
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
}
/* }}} */

H
memory  
heyanlong 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
/* {{{ 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}
};
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

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