skywalking.c 80.4 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
/*
  +----------------------------------------------------------------------+
  | 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 */
H
heyanlong 已提交
29
#include <zend_interfaces.h>
30 31 32 33
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
H
heyanlong 已提交
34
#include "components.h"
35 36
#include "php_skywalking.h"
#include "ext/standard/url.h" /* for php_url */
H
heyanlong 已提交
37
#include "ext/standard/php_var.h"
H
mysql  
heyanlong 已提交
38
#include "ext/pdo/php_pdo_driver.h"
H
peer  
heyanlong 已提交
39
#include "ext/pcre/php_pcre.h"
40 41 42 43 44 45 46 47

#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 已提交
48 49 50 51 52 53 54 55 56
#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 已提交
57
#include <sys/un.h>
58

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

H
mysqlnd  
heyanlong 已提交
61 62 63 64
#ifdef MYSQLI_USE_MYSQLND
#include "ext/mysqli/php_mysqli_structs.h"
#endif

S
songzhian 已提交
65
/* If you declare any globals in php_skywalking.h uncomment this:
66 67 68 69 70
*/
ZEND_DECLARE_MODULE_GLOBALS(skywalking)

/* True global resources - no need for thread safety here */
static int le_skywalking;
H
3.2.6  
heyanlong 已提交
71 72 73 74
#if SKY_DEBUG
static int application_instance = 1;
static int application_id = 1;
static int cli_debug = 1;
75 76
static char service[512] = {'a'};
static char service_instance[512] = {'a', 'i'};
H
3.2.6  
heyanlong 已提交
77
#else
H
heyanlong 已提交
78 79
static int application_instance = 0;
static int application_id = 0;
H
3.2.6  
heyanlong 已提交
80
static int cli_debug = 0;
81 82
static char service[512] = {0};
static char service_instance[512] = {0};
H
3.2.6  
heyanlong 已提交
83
#endif
H
heyanlong 已提交
84
static char application_uuid[37] = {0};
S
songzhian 已提交
85 86
static int sky_increment_id = 0;

H
mysql  
heyanlong 已提交
87 88 89 90 91
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);

92 93 94 95
/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini*/
PHP_INI_BEGIN()
H
3.2.6  
heyanlong 已提交
96 97 98
#if SKY_DEBUG
	STD_PHP_INI_BOOLEAN("skywalking.enable",   	"1", PHP_INI_ALL, OnUpdateBool, enable, zend_skywalking_globals, skywalking_globals)
#else
H
heyanlong 已提交
99
	STD_PHP_INI_BOOLEAN("skywalking.enable",   	"0", PHP_INI_ALL, OnUpdateBool, enable, zend_skywalking_globals, skywalking_globals)
H
3.2.6  
heyanlong 已提交
100
#endif
101
	STD_PHP_INI_ENTRY("skywalking.version",   	"8", PHP_INI_ALL, OnUpdateLong, version, zend_skywalking_globals, skywalking_globals)
H
heyanlong 已提交
102
	STD_PHP_INI_ENTRY("skywalking.app_code", "hello_skywalking", PHP_INI_ALL, OnUpdateString, app_code, zend_skywalking_globals, skywalking_globals)
103
	STD_PHP_INI_ENTRY("skywalking.sock_path", "/tmp/sky-agent.sock", PHP_INI_ALL, OnUpdateString, sock_path, zend_skywalking_globals, skywalking_globals)
104 105 106 107
PHP_INI_END()

/* }}} */

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// declare args for skywalking_get_trace_info()
ZEND_BEGIN_ARG_INFO(arginfo_skywalking_get_trace_info, 0)
ZEND_END_ARG_INFO()

// declare function skywalking_get_trace_info()
PHP_FUNCTION(skywalking_get_trace_info)
{
    if (application_instance == 0) {
        zval empty;
        array_init(&empty);
        RETURN_ZVAL(&empty, 0, 1);
    }
    // return array
    RETURN_ZVAL(&SKYWALKING_G(UpstreamSegment), 1, 0)
}


125 126 127 128 129
/* {{{ skywalking_functions[]
 *
 * Every user visible function must have an entry in skywalking_functions[].
 */
const zend_function_entry skywalking_functions[] = {
130
    PHP_FE(skywalking_get_trace_info, arginfo_skywalking_get_trace_info)
131 132 133 134 135 136 137 138 139 140
	PHP_FE_END	/* Must be the last line in skywalking_functions[] */
};
/* }}} */



const zend_function_entry class_skywalking[] = {
	PHP_FE_END
};

H
peer  
heyanlong 已提交
141 142 143 144 145 146 147 148 149 150
static char *pcre_match(char *pattern, int len, char *subject) {
    pcre_cache_entry *cache;
    zval *result = (zval *) emalloc(sizeof(zval));
    bzero(result, sizeof(zval));
    zval *subpats = (zval *) emalloc(sizeof(zval));
    bzero(subpats, sizeof(zval));
    char *ret = NULL;

    zend_string *pattern_str = zend_string_init(pattern, len, 0);
    if ((cache = pcre_get_compiled_regex_cache(pattern_str)) != NULL) {
H
heyanlong 已提交
151
#if PHP_VERSION_ID < 70400
H
peer  
heyanlong 已提交
152
        php_pcre_match_impl(cache, subject, strlen(subject), result, subpats, 0, 0, 0, 0);
H
heyanlong 已提交
153 154 155 156 157
#else
        zend_string *subject_str = zend_string_init(subject, sizeof(subject) - 1, 0);
        php_pcre_match_impl(cache, subject_str, result, subpats, 0, 0, 0, 0);
        zend_string_free(subject_str);
#endif
H
peer  
heyanlong 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        zval *match = NULL;
        if (Z_LVAL_P(result) > 0 && Z_TYPE_P(subpats) == IS_ARRAY) {
            zval *value = zend_hash_index_find(Z_ARRVAL_P(subpats), 1);
            if (value != NULL) {
                match = value;
                if (Z_TYPE_P(match) == IS_STRING) {
                    ret = estrdup(Z_STRVAL_P(match));
                }
            }
        }
    }
    zend_string_free(pattern_str);
    efree(result);
    zval_dtor(subpats);
    efree(subpats);
    return ret;

}

H
heyanlong 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static char *sky_get_class_name(zval *obj) {
    if (Z_TYPE_P(obj) == IS_OBJECT) {
        zend_object *object = obj->value.obj;
        return ZSTR_VAL(object->ce->name);
    }
    return "";
}

static zval *sky_read_property(zval *obj, const char *property) {
    if (Z_TYPE_P(obj) == IS_OBJECT) {
        zend_object *object = obj->value.obj;
        return zend_read_property(object->ce, obj, property, strlen(property), 0, NULL);
    }
    return NULL;
}

w60514603's avatar
w60514603 已提交
193 194
static char *sky_redis_fnamewall(const char *function_name) {
    char *fnamewall = (char *) emalloc(strlen(function_name) + 3);
何延龙 已提交
195
    bzero(fnamewall, strlen(function_name) + 3);
w60514603's avatar
w60514603 已提交
196
    sprintf(fnamewall, "|%s|", function_name);
何延龙 已提交
197 198 199
    char *fnamewall_lower = zend_str_tolower_dup(fnamewall, strlen(fnamewall));
    efree(fnamewall);
    return fnamewall_lower;
w60514603's avatar
w60514603 已提交
200 201
}

w60514603's avatar
w60514603 已提交
202
static int sky_redis_opt_for_string_key(char *fnamewall) {
w60514603's avatar
w60514603 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216
    if (strstr(REDIS_KEY_STRING, fnamewall)
        || strstr(REDIS_KEY_KEY, fnamewall)
        || strstr(REDIS_KEY_HASH, fnamewall)
        || strstr(REDIS_KEY_LIST, fnamewall)
        || strstr(REDIS_KEY_SET, fnamewall)
        || strstr(REDIS_KEY_SORT, fnamewall)
        || strstr(REDIS_KEY_HLL, fnamewall)
        || strstr(REDIS_KEY_GEO, fnamewall)
    ) {
        return 1;
    }
    return 0;
}

K
kilingzhang 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static char *sky_memcached_fnamewall(const char *function_name) {
    char *fnamewall = (char *) emalloc(strlen(function_name) + 3);
    sprintf(fnamewall, "|%s|", function_name);
    fnamewall = zend_str_tolower_dup(fnamewall, strlen(fnamewall));
    return fnamewall;
}

static int sky_memcached_opt_for_string_key(char *fnamewall) {
    if (strstr(MEMCACHED_KEY_STRING, fnamewall)
        || strstr(MEMCACHED_KEY_STATS, fnamewall)
        || strstr(MEMCACHED_KEY_OTHERS, fnamewall)
            ) {
        return 1;
    }
    return 0;
}
S
songzhian 已提交
233

H
mysql  
heyanlong 已提交
234
ZEND_API void sky_execute_ex(zend_execute_data *execute_data) {
235
    if (application_instance == 0) {
w60514603's avatar
w60514603 已提交
236 237 238
        ori_execute_ex(execute_data);
        return;
    }
H
predis  
heyanlong 已提交
239 240 241 242 243 244 245

    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;
H
heyanlong 已提交
246
    char *peer = NULL;
w60514603's avatar
w60514603 已提交
247
    int componentId = 0;
H
predis  
heyanlong 已提交
248
    if (class_name != NULL) {
H
heyanlong 已提交
249
        if (strcmp(class_name, "Predis\\Client") == 0 && strcmp(function_name, "executeCommand") == 0) {
H
predis  
heyanlong 已提交
250 251 252 253 254
            // params
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
            if (arg_count) {
                zval *p = ZEND_CALL_ARG(execute_data, 1);

H
heyanlong 已提交
255 256 257 258 259
                zval *id = (zval *) emalloc(sizeof(zval));
                zend_call_method(p, Z_OBJCE_P(p), NULL, ZEND_STRL("getid"), id, 0, NULL, NULL);

                if (Z_TYPE_P(id) == IS_STRING) {
                    operationName = (char *) emalloc(strlen(class_name) + strlen(Z_STRVAL_P(id)) + 3);
w60514603's avatar
w60514603 已提交
260
                    componentId = COMPONENT_JEDIS;
H
predis  
heyanlong 已提交
261 262
                    strcpy(operationName, class_name);
                    strcat(operationName, "->");
H
heyanlong 已提交
263
                    strcat(operationName, Z_STRVAL_P(id));
H
predis  
heyanlong 已提交
264
                }
H
heyanlong 已提交
265
                efree(id);
H
predis  
heyanlong 已提交
266
            }
w60514603's avatar
w60514603 已提交
267 268 269 270 271 272 273
        } else if (strcmp(class_name, "Grpc\\BaseStub") == 0) {
            if (strcmp(function_name, "_simpleRequest") == 0
                || strcmp(function_name, "_clientStreamRequest") == 0
                || strcmp(function_name, "_serverStreamRequest") == 0
                || strcmp(function_name, "_bidiRequest") == 0
            ) {
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
274 275 276 277 278
                if (SKYWALKING_G(version) == 5) {
                    componentId = COMPONENT_GRPC;
                } else {
                    componentId = COMPONENT_RPC;
                }
w60514603's avatar
w60514603 已提交
279 280 281 282
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
H
predis  
heyanlong 已提交
283 284 285 286 287 288 289
        }
    }

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

H
heyanlong 已提交
290
        if (strcmp(class_name, "Predis\\Client") == 0 && strcmp(function_name, "executeCommand") == 0) {
H
predis  
heyanlong 已提交
291
            add_assoc_string(&tags, "db.type", "redis");
H
heyanlong 已提交
292 293 294 295 296 297
            zval *p = ZEND_CALL_ARG(execute_data, 1);
            zval *id = (zval *) emalloc(sizeof(zval));
            zval *arguments = (zval *) emalloc(sizeof(zval));
            zend_call_method(p, Z_OBJCE_P(p), NULL, ZEND_STRL("getid"), id, 0, NULL, NULL);
            zend_call_method(p, Z_OBJCE_P(p), NULL, ZEND_STRL("getarguments"), arguments, 0, NULL, NULL);

H
heyanlong 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311
            // peer
            zval *connection = sky_read_property(&(execute_data->This),"connection");
            if (connection != NULL && Z_TYPE_P(connection) == IS_OBJECT && strcmp(sky_get_class_name(connection), "Predis\\Connection\\StreamConnection") == 0) {
                zval *parameters = sky_read_property(connection, "parameters");
                if (parameters != NULL && Z_TYPE_P(parameters) == IS_OBJECT && strcmp(sky_get_class_name(parameters), "Predis\\Connection\\Parameters") == 0) {
                    zval *parameters_arr = sky_read_property(parameters, "parameters");
                    if (Z_TYPE_P(parameters_arr) == IS_ARRAY) {
                        zval *predis_host = zend_hash_str_find(Z_ARRVAL_P(parameters_arr), "host", sizeof("host") - 1);
                        zval *predis_port = zend_hash_str_find(Z_ARRVAL_P(parameters_arr), "port", sizeof("port") - 1);

                        if (Z_TYPE_P(predis_host) == IS_STRING && Z_TYPE_P(predis_port) == IS_LONG) {
                            const char *host = ZSTR_VAL(Z_STR_P(predis_host));
                            peer = (char *) emalloc(strlen(host) + 10);
                            bzero(peer, strlen(host) + 10);
H
heyanlong 已提交
312
                            sprintf(peer, "%s:%" PRId3264, host, Z_LVAL_P(predis_port));
H
heyanlong 已提交
313 314 315 316 317 318
                        }
                    }
                }
            }
            // peer end

H
heyanlong 已提交
319 320
            if (Z_TYPE_P(arguments) == IS_ARRAY) {
                zend_ulong num_key;
w60514603's avatar
w60514603 已提交
321
                zval *entry, str_entry;
H
heyanlong 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
                smart_str command = {0};
                smart_str_appends(&command, Z_STRVAL_P(id));
                smart_str_appends(&command, " ");
                ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(arguments), num_key, entry)
                        {
                            switch (Z_TYPE_P(entry)) {
                                case IS_STRING:
                                    smart_str_appends(&command, Z_STRVAL_P(entry));
                                    smart_str_appends(&command, " ");
                                    break;
                                case IS_ARRAY:
                                    break;
                                default:
w60514603's avatar
w60514603 已提交
335 336 337
                                    ZVAL_COPY(&str_entry, entry);
                                    convert_to_string(&str_entry);
                                    smart_str_appends(&command, Z_STRVAL_P(&str_entry));
w60514603's avatar
w60514603 已提交
338
                                    smart_str_appends(&command, " ");
H
heyanlong 已提交
339
                                    break;
H
predis  
heyanlong 已提交
340
                            }
H
heyanlong 已提交
341 342 343 344 345 346 347 348
                        }
                ZEND_HASH_FOREACH_END();

                // store command to tags
                if (command.s) {
                    smart_str_0(&command);
                    add_assoc_string(&tags, "redis.command", ZSTR_VAL(command.s));
                    smart_str_free(&command);
H
predis  
heyanlong 已提交
349 350
                }
            }
H
heyanlong 已提交
351 352 353 354
            zval_ptr_dtor(id);
            zval_ptr_dtor(arguments);
            efree(id);
            efree(arguments);
w60514603's avatar
w60514603 已提交
355 356 357 358 359 360
        } else if (strcmp(class_name, "Grpc\\BaseStub") == 0) {
            add_assoc_string(&tags, "rpc.type", "grpc");
            zval *p = ZEND_CALL_ARG(execute_data, 1);
            if (Z_TYPE_P(p) == IS_STRING) {
                add_assoc_string(&tags, "rpc.method", Z_STRVAL_P(p));
            }
H
predis  
heyanlong 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        }

        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);
w60514603's avatar
w60514603 已提交
382
        add_assoc_long(&temp, "componentId", componentId);
H
predis  
heyanlong 已提交
383
        add_assoc_string(&temp, "operationName", operationName);
H
heyanlong 已提交
384
        add_assoc_string(&temp, "peer", peer == NULL ? "" : peer);
H
predis  
heyanlong 已提交
385
        efree(operationName);
H
heyanlong 已提交
386 387 388
        if (peer != NULL) {
            efree(peer);
        }
H
predis  
heyanlong 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

        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 已提交
405 406 407 408
}

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

409
    if (application_instance == 0) {
H
mysql  
heyanlong 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422
        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);

423
    int is_procedural_mysqli = 0; // "Procedural style" or "Object oriented style" ?
H
mysql  
heyanlong 已提交
424
    char *operationName = NULL;
H
heyanlong 已提交
425
    char *peer = NULL;
H
mysql  
heyanlong 已提交
426
    char *component = NULL;
427
    int componentId = COMPONENT_MYSQL_JDBC_DRIVER;
H
mysql  
heyanlong 已提交
428 429 430 431 432 433
    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 已提交
434
                component = (char *) emalloc(strlen("PDO") + 1);
H
mysql  
heyanlong 已提交
435
                strcpy(component, "PDO");
H
pdo  
heyanlong 已提交
436 437 438 439 440 441 442 443 444 445
                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 已提交
446 447 448 449
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
450
        } else if (strcmp(class_name, "mysqli") == 0) {
451 452 453 454 455 456 457 458
            if (strcmp(function_name, "query") == 0) {
                component = (char *) emalloc(strlen("mysqli") + 1);
                strcpy(component, "mysqli");
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
459
        } else if (strcmp(class_name, "Yar_Client") == 0) {
460
            if (strcmp(function_name, "__call") == 0) {
H
6.x rpc  
heyanlong 已提交
461 462 463 464 465 466
                if (SKYWALKING_G(version) == 5) {
                    componentId = COMPONENT_GRPC;
                } else {
                    componentId = COMPONENT_RPC;
                }

467 468 469 470 471 472 473 474 475 476 477 478 479
                component = (char *) emalloc(strlen("Yar_Client") + 1);
                strcpy(component, "Yar_Client");
                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(Z_STRVAL_P(p)) + 3);
                        strcpy(operationName, class_name);
                        strcat(operationName, "->");
                        strcat(operationName, Z_STRVAL_P(p));
                    }
                }
            }
w60514603's avatar
w60514603 已提交
480 481
        } else if (strcmp(class_name, "Redis") == 0 || strcmp(class_name, "RedisCluster") == 0) {
            char *fnamewall = sky_redis_fnamewall(function_name);
w60514603's avatar
w60514603 已提交
482
            if (sky_redis_opt_for_string_key(fnamewall) == 1) {
w60514603's avatar
w60514603 已提交
483 484 485 486 487 488 489 490 491
                componentId = COMPONENT_JEDIS;
                component = (char *) emalloc(strlen("Redis") + 1);
                strcpy(component, "Redis");
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
            efree(fnamewall);
K
kilingzhang 已提交
492 493 494 495 496 497 498 499 500 501 502 503
        } else if (strcmp(class_name, "Memcached") == 0) {
            char *fnamewall = sky_memcached_fnamewall(function_name);
            if (sky_memcached_opt_for_string_key(fnamewall) == 1) {
                componentId = COMPONENT_XMEMCACHED;
                component = (char *) emalloc(strlen("memcached") + 1);
                strcpy(component, "memcached");
                operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
                strcpy(operationName, class_name);
                strcat(operationName, "->");
                strcat(operationName, function_name);
            }
            efree(fnamewall);
H
mysql  
heyanlong 已提交
504 505
        }
    } else if (function_name != NULL) {
506 507 508 509 510 511 512 513 514 515
        if (strcmp(function_name, "mysqli_query") == 0) {
            class_name = "mysqli";
            function_name = "query";
            component = (char *) emalloc(strlen(class_name) + 1);
            strcpy(component, class_name);
            operationName = (char *) emalloc(strlen(class_name) + strlen(function_name) + 3);
            strcpy(operationName, class_name);
            strcat(operationName, "->");
            strcat(operationName, function_name);

516
            is_procedural_mysqli = 1;
517
        }
H
mysql  
heyanlong 已提交
518 519 520 521 522 523 524
    }

    if (operationName != NULL) {

        zval tags;
        array_init(&tags);

H
pdo  
heyanlong 已提交
525 526 527 528 529 530 531 532 533 534
        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 已提交
535
                        break;
H
pdo  
heyanlong 已提交
536 537

                }
H
mysql  
heyanlong 已提交
538 539 540 541 542 543
            }

            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 已提交
544
                    memcpy(db_type, (char *) dbh->driver->driver_name, dbh->driver->driver_name_len);
H
mysql  
heyanlong 已提交
545 546 547 548
                    add_assoc_string(&tags, "db.type", db_type);
                }

                if (dbh->data_source != NULL && db_type[0] != '\0') {
H
pdo  
heyanlong 已提交
549
                    add_assoc_string(&tags, "db.data_source", (char *) dbh->data_source);
H
peer  
heyanlong 已提交
550 551 552 553 554 555 556 557 558
                    char *host = pcre_match("(host=([^;\\s]+))", sizeof("(host=([^;\\s]+))")-1, (char *) dbh->data_source);
                    char *port = pcre_match("(port=([^;\\s]+))", sizeof("(port=([^;\\s]+))")-1, (char *) dbh->data_source);
                    if (host != NULL && port != NULL) {
                        peer = (char *) emalloc(strlen(host) + 10);
                        bzero(peer, strlen(host) + 10);
                        sprintf(peer, "%s:%s", host, port);
                    }
                    if (host != NULL) efree(host);
                    if (port != NULL) efree(port);
H
pdo  
heyanlong 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
                }
            }
        } 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
peer  
heyanlong 已提交
574 575 576 577 578 579 580 581 582
                    char *host = pcre_match("(host=([^;\\s]+))", sizeof("(host=([^;\\s]+))")-1, (char *) stmt->dbh->data_source);
                    char *port = pcre_match("(port=([^;\\s]+))", sizeof("(port=([^;\\s]+))")-1, (char *) stmt->dbh->data_source);
                    if (host != NULL && port != NULL) {
                        peer = (char *) emalloc(strlen(host) + 10);
                        bzero(peer, strlen(host) + 10);
                        sprintf(peer, "%s:%s", host, port);
                    }
                    if (host != NULL) efree(host);
                    if (port != NULL) efree(port);
H
mysql  
heyanlong 已提交
583 584
                }
            }
H
heyanlong 已提交
585 586
        } else if (strcmp(class_name, "mysqli") == 0) {
            if (strcmp(function_name, "query") == 0) {
H
mysqlnd  
heyanlong 已提交
587
#ifdef MYSQLI_USE_MYSQLND
H
fix bug  
heyanlong 已提交
588 589 590 591 592 593 594
                mysqli_object *mysqli = NULL;
                if(is_procedural_mysqli) {
                    mysqli = (mysqli_object *) Z_MYSQLI_P(ZEND_CALL_ARG(execute_data, 1));
                } else {
                    mysqli = (mysqli_object *) Z_MYSQLI_P(&(execute_data->This));
                }

H
heyanlong 已提交
595 596 597 598
                MYSQLI_RESOURCE *my_res = (MYSQLI_RESOURCE *) mysqli->ptr;
                if (my_res && my_res->ptr) {
                    MY_MYSQL *mysql = (MY_MYSQL *) my_res->ptr;
                    if (mysql->mysql) {
H
fix #96  
heyanlong 已提交
599
#if PHP_VERSION_ID >= 70100
H
peer  
heyanlong 已提交
600 601 602 603
                        char *host = mysql->mysql->data->hostname.s;
#else
                        char *host = mysql->mysql->data->host;
#endif
w60514603's avatar
w60514603 已提交
604 605
                        char port[6];
                        sprintf(port, "%d", mysql->mysql->data->port);
H
peer  
heyanlong 已提交
606
                        add_assoc_string(&tags, "db.host", host);
w60514603's avatar
w60514603 已提交
607
                        add_assoc_string(&tags, "db.port", port);
H
peer  
heyanlong 已提交
608 609 610
                        peer = (char *) emalloc(strlen(host) + 10);
                        bzero(peer, strlen(host) + 10);
                        sprintf(peer, "%s:%d", host, mysql->mysql->data->port);
H
heyanlong 已提交
611 612
                    }
                }
H
mysqlnd  
heyanlong 已提交
613
#endif
H
heyanlong 已提交
614
                add_assoc_string(&tags, "db.type", "mysql");
615 616 617 618 619 620 621 622 623 624
                // params
                uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
                if (arg_count) {
                    zval *p = is_procedural_mysqli ? ZEND_CALL_ARG(execute_data, 2) : 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));
                            break;

625 626
                    }
                }
627
            }
628
        } else if (strcmp(class_name, "Yar_Client") == 0) {
629 630 631 632 633 634 635 636 637 638 639 640 641
            if (strcmp(function_name, "__call") == 0) {
                zval rv, _uri;
                ZVAL_STRING(&_uri, "_uri");
                zval *yar_uri = Z_OBJ_HT(EX(This))->read_property(&EX(This), &_uri, BP_VAR_R, 0, &rv);
                add_assoc_string(&tags, "yar.uri", Z_STRVAL_P(yar_uri));
                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) {
                        add_assoc_string(&tags, "yar.method", Z_STRVAL_P(p));
                    }
                }
            }
w60514603's avatar
w60514603 已提交
642 643 644 645
        } else if (strcmp(class_name, "Redis") == 0 || strcmp(class_name, "RedisCluster") == 0) {
            add_assoc_string(&tags, "db.type", "redis");
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);

何延龙 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
            if (strcmp(class_name, "Redis") == 0) {
                // find peer
                zval *this = &(execute_data->This);
                zval host;
                zval port;
                zend_call_method(this, Z_OBJCE_P(this), NULL, ZEND_STRL("gethost"), &host, 0, NULL, NULL);
                zend_call_method(this, Z_OBJCE_P(this), NULL, ZEND_STRL("getport"), &port, 0, NULL, NULL);


                if (!Z_ISUNDEF(host) && !Z_ISUNDEF(port) && Z_TYPE(host) == IS_STRING && Z_TYPE(port) == IS_LONG) {
                    const char *h = ZSTR_VAL(Z_STR(host));
                    peer = (char *) emalloc(strlen(h) + 10);
                    bzero(peer, strlen(h) + 10);
                    sprintf(peer, "%s:%" PRId3264, h, Z_LVAL(port));
                }

                if (!Z_ISUNDEF(host)) {
                    zval_ptr_dtor(&host);
                }

                if (!Z_ISUNDEF(port)) {
                    zval_ptr_dtor(&port);
                }
            }

w60514603's avatar
w60514603 已提交
671
            smart_str command = {0};
何延龙 已提交
672 673
            char *fname = zend_str_tolower_dup((char *) function_name, strlen((char *) function_name));
            smart_str_appends(&command, fname);
w60514603's avatar
w60514603 已提交
674
            smart_str_appends(&command, " ");
何延龙 已提交
675
            efree(fname);
w60514603's avatar
w60514603 已提交
676 677 678 679

            int is_string_command = 1;
            int i;
            for (i = 1; i < arg_count + 1; ++i) {
w60514603's avatar
w60514603 已提交
680
                zval str_p;
w60514603's avatar
w60514603 已提交
681 682 683 684 685
                zval *p = ZEND_CALL_ARG(execute_data, i);
                if (Z_TYPE_P(p) == IS_ARRAY) {
                    is_string_command = 0;
                    break;
                }
w60514603's avatar
w60514603 已提交
686 687 688 689

                ZVAL_COPY(&str_p, p);
                if (Z_TYPE_P(&str_p) != IS_STRING) {
                    convert_to_string(&str_p);
w60514603's avatar
w60514603 已提交
690 691
                }
                if (i == 1) {
w60514603's avatar
w60514603 已提交
692
                    add_assoc_string(&tags, "redis.key", Z_STRVAL_P(&str_p));
w60514603's avatar
w60514603 已提交
693
                }
何延龙 已提交
694 695
                char *tmp = zend_str_tolower_dup(Z_STRVAL_P(&str_p), Z_STRLEN_P(&str_p));
                smart_str_appends(&command, tmp);
w60514603's avatar
w60514603 已提交
696
                smart_str_appends(&command, " ");
何延龙 已提交
697
                efree(tmp);
w60514603's avatar
w60514603 已提交
698 699 700 701 702
            }
            // store command to tags
            if (command.s) {
                smart_str_0(&command);
                if (is_string_command) {
何延龙 已提交
703 704 705
                    zend_string *trim_s = php_trim(command.s, NULL, 0, 3);
                    add_assoc_string(&tags, "redis.command", ZSTR_VAL(trim_s));
                    zend_string_free(trim_s);
w60514603's avatar
w60514603 已提交
706 707 708
                }
                smart_str_free(&command);
            }
K
kilingzhang 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
        } else if (strcmp(class_name, "Memcached") == 0) {

            add_assoc_string(&tags, "db.type", "memcached");
            uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);

            smart_str command = {0};
            smart_str_appends(&command, zend_str_tolower_dup((char *) function_name, strlen((char *) function_name)));
            smart_str_appends(&command, " ");

            int i;
            for (i = 1; i < arg_count + 1; ++i) {
                char *str = NULL;
                zval str_p;
                zval *p = ZEND_CALL_ARG(execute_data, i);
                if (Z_TYPE_P(p) == IS_ARRAY) {
                    str = sky_json_encode(p);
                }

                ZVAL_COPY(&str_p, p);
                if (Z_TYPE_P(&str_p) != IS_ARRAY && Z_TYPE_P(&str_p) != IS_STRING) {
                    convert_to_string(&str_p);
                }

                if (str == NULL) {
                    str = Z_STRVAL_P(&str_p);
                }

                if (i == 1) {
                    add_assoc_string(&tags, "memcached.key", str);
                }
                smart_str_appends(&command, zend_str_tolower_dup(str, strlen(str)));
                smart_str_appends(&command, " ");
            }
            // store command to tags
            if (command.s) {
                smart_str_0(&command);
                add_assoc_string(&tags, "memcached.command", ZSTR_VAL(php_trim(command.s, NULL, 0, 3)));
                smart_str_free(&command);
            }
H
mysql  
heyanlong 已提交
748 749
        }

K
kilingzhang 已提交
750

H
mysql  
heyanlong 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        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);
771
        add_assoc_long(&temp, "componentId", componentId);
H
mysql  
heyanlong 已提交
772
        add_assoc_string(&temp, "operationName", operationName);
H
heyanlong 已提交
773
        add_assoc_string(&temp, "peer", peer == NULL ? "" : peer);
H
mysql  
heyanlong 已提交
774 775
        efree(component);
        efree(operationName);
H
heyanlong 已提交
776 777 778
        if (peer != NULL) {
            efree(peer);
        }
H
mysql  
heyanlong 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

        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 已提交
804 805


806 807 808 809 810 811
/* 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 已提交
812
void sky_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS)
813
{
814
    if(application_instance == 0) {
H
heyanlong 已提交
815 816 817 818
        orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

819 820 821 822 823
	zval		*zid;
	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zid) == FAILURE) {
		return;
	}

H
heyanlong 已提交
824
	int is_send = 1;
H
heyanlong 已提交
825

H
report  
heyanlong 已提交
826 827 828 829 830
    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 已提交
831 832 833
    zval_dtor(&function_name);
    zval_dtor(&params[0]);

H
report  
heyanlong 已提交
834
    zval *z_url = zend_hash_str_find(Z_ARRVAL(curlInfo),  ZEND_STRL("url"));
H
heyanlong 已提交
835
    char *url_str = Z_STRVAL_P(z_url);
H
heyanlong 已提交
836 837
    if(strlen(url_str) <= 0) {
        zval_dtor(&curlInfo);
H
heyanlong 已提交
838
        is_send = 0;
H
heyanlong 已提交
839
    }
H
heyanlong 已提交
840
    php_url *url_info = NULL;
H
heyanlong 已提交
841 842 843 844 845 846
    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 已提交
847 848 849
        }
    }

G
goerzh 已提交
850
    char *sw = NULL;
H
heyanlong 已提交
851 852 853
    zval *spans = NULL;
    zval *last_span = NULL;
    zval *span_id = NULL;
H
heyanlong 已提交
854
    char *peer = NULL;
H
heyanlong 已提交
855
    char *operation_name = NULL;
H
heyanlong 已提交
856 857
    char *full_url = NULL;

w60514603's avatar
w60514603 已提交
858

H
heyanlong 已提交
859
    if (is_send == 1) {
w60514603's avatar
w60514603 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873

// for php7.3.0+
#if PHP_VERSION_ID >= 70300
        char *php_url_scheme = ZSTR_VAL(url_info->scheme);
        char *php_url_host = ZSTR_VAL(url_info->host);
        char *php_url_path = ZSTR_VAL(url_info->path);
        char *php_url_query = ZSTR_VAL(url_info->query);
#else
        char *php_url_scheme = url_info->scheme;
        char *php_url_host = url_info->host;
        char *php_url_path = url_info->path;
        char *php_url_query = url_info->query;
#endif

H
heyanlong 已提交
874 875 876 877
        int peer_port = 0;
        if (url_info->port) {
            peer_port = url_info->port;
        } else {
w60514603's avatar
w60514603 已提交
878
            if (strcasecmp("http", php_url_scheme) == 0) {
H
heyanlong 已提交
879
                peer_port = 80;
H
heyanlong 已提交
880
            } else {
H
heyanlong 已提交
881
                peer_port = 443;
H
heyanlong 已提交
882 883
            }
        }
H
heyanlong 已提交
884

G
goerzh 已提交
885 886
        if (url_info->query) {
            if (url_info->path == NULL) {
H
heyanlong 已提交
887 888
                spprintf(&operation_name, 0, "%s", "/");
                spprintf(&full_url, 0, "%s?%s", "/", php_url_query);
G
goerzh 已提交
889
            } else {
H
heyanlong 已提交
890 891
                spprintf(&operation_name, 0, "%s", php_url_path);
                spprintf(&full_url, 0, "%s?%s", php_url_path, php_url_query);
G
goerzh 已提交
892 893 894
            }
        } else {
            if (url_info->path == NULL) {
H
heyanlong 已提交
895 896
                spprintf(&operation_name, 0, "%s", "/");
                spprintf(&full_url, 0, "%s", "/");
G
goerzh 已提交
897
            } else {
H
heyanlong 已提交
898 899
                spprintf(&operation_name, 0, "%s", php_url_path);
                spprintf(&full_url, 0, "%s", php_url_path);
G
goerzh 已提交
900 901
            }
        }
G
goerzh 已提交
902

H
heyanlong 已提交
903 904 905
        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);
906
        if (SKYWALKING_G(version) == 5) { // skywalking 5.x
H
heyanlong 已提交
907
            spprintf(&peer, 0, "%s://%s:%d", php_url_scheme, php_url_host, peer_port);
908
            sw = generate_sw3(Z_LVAL_P(span_id) + 1, peer, operation_name);
何延龙 已提交
909
        } else if (SKYWALKING_G(version) == 6 || SKYWALKING_G(version) == 7) { // skywalking 6.x
H
heyanlong 已提交
910
            spprintf(&peer, 0, "%s:%d", php_url_host, peer_port);
911
            sw = generate_sw6(Z_LVAL_P(span_id) + 1, peer);
912 913
        } else if (SKYWALKING_G(version) == 8) {
            spprintf(&peer, 0, "%s:%d", php_url_host, peer_port);
H
heyanlong 已提交
914
            sw = generate_sw8(Z_LVAL_P(span_id) + 1, peer);
G
goerzh 已提交
915
        }
H
report  
heyanlong 已提交
916 917
    }

H
heyanlong 已提交
918

G
goerzh 已提交
919
    if (sw != NULL) {
H
heyanlong 已提交
920 921 922 923 924 925 926 927 928
        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 已提交
929
        }
H
heyanlong 已提交
930

G
goerzh 已提交
931
        add_next_index_string(option, sw);
H
heyanlong 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        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 已提交
948
        }
H
heyanlong 已提交
949 950 951
        zval_dtor(&argv[0]);
        zval_dtor(&argv[1]);
        zval_dtor(&argv[2]);
G
goerzh 已提交
952
        efree(sw);
H
report  
heyanlong 已提交
953
    }
H
heyanlong 已提交
954 955 956 957

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

H
heyanlong 已提交
960
        array_init(&temp);
H
heyanlong 已提交
961

H
heyanlong 已提交
962 963 964 965 966 967 968 969 970
        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 已提交
971
    }
H
heyanlong 已提交
972 973


H
heyanlong 已提交
974
	orig_curl_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
975

H
heyanlong 已提交
976 977 978 979 980 981 982 983
    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 已提交
984

H
heyanlong 已提交
985 986 987 988
        zval *z_http_code;
        l_millisecond = get_millisecond();
        millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
        efree(l_millisecond);
H
heyanlong 已提交
989

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

H
heyanlong 已提交
992
        add_assoc_long(&temp, "endTime", millisecond);
H
heyanlong 已提交
993 994


G
goerzh 已提交
995
        add_assoc_string(&temp, "operationName", operation_name);
996
        add_assoc_string(&temp, "peer", peer);
H
heyanlong 已提交
997 998 999 1000 1001
        zval tags;
        array_init(&tags);
        add_assoc_string(&tags, "url", full_url);

        add_assoc_zval(&temp, "tags", &tags);
H
heyanlong 已提交
1002 1003
        efree(peer);
        efree(operation_name);
H
heyanlong 已提交
1004
        efree(full_url);
H
heyanlong 已提交
1005

H
heyanlong 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
        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);
    }
1020
}
H
heyanlong 已提交
1021 1022

void sky_curl_setopt_handler(INTERNAL_FUNCTION_PARAMETERS) {
1023
    if(application_instance == 0) {
H
heyanlong 已提交
1024 1025 1026 1027
        orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
1028 1029 1030 1031 1032 1033 1034
    zval *zid, *zvalue;
    zend_long options;

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

H
heyanlong 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    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 已提交
1045
        } else {
H
heyanlong 已提交
1046
            orig_curl_setopt(INTERNAL_FUNCTION_PARAM_PASSTHRU);
H
heyanlong 已提交
1047 1048 1049
        }
    }
}
H
heyanlong 已提交
1050 1051

void sky_curl_setopt_array_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
1052

1053
    if(application_instance == 0) {
H
heyanlong 已提交
1054 1055 1056 1057
        orig_curl_setopt_array(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
1058 1059 1060
    zval *zid, *arr, *entry;
    zend_ulong option;
    zend_string *string_key;
H
heyanlong 已提交
1061 1062 1063 1064 1065 1066

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

H
heyanlong 已提交
1067 1068 1069 1070 1071 1072
    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 已提交
1073
    }
H
heyanlong 已提交
1074 1075 1076 1077

    orig_curl_setopt_array(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}

H
heyanlong 已提交
1078
void sky_curl_close_handler(INTERNAL_FUNCTION_PARAMETERS) {
H
heyanlong 已提交
1079

1080
    if(application_instance == 0) {
H
heyanlong 已提交
1081 1082 1083 1084
        orig_curl_close(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        return;
    }

H
heyanlong 已提交
1085 1086 1087 1088 1089 1090
    zval *zid;

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

H
heyanlong 已提交
1091 1092 1093
    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 已提交
1094 1095 1096 1097
    }

    orig_curl_close(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
1098 1099 1100 1101 1102
/* {{{ 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 已提交
1103
	skywalking_globals->app_code = NULL;
H
heyanlong 已提交
1104
	skywalking_globals->enable = 0;
H
6.x  
heyanlong 已提交
1105
	skywalking_globals->version = 6;
H
heyanlong 已提交
1106
	skywalking_globals->sock_path = "/var/run/sky-agent.sock";
1107 1108 1109 1110 1111 1112 1113
}



static char *sky_json_encode(zval *parameter){

	smart_str buf = {0};
H
report  
heyanlong 已提交
1114
	zend_long options = 64;
H
6.x  
heyanlong 已提交
1115
#if PHP_VERSION_ID >= 70100
H
heyanlong 已提交
1116
	if (php_json_encode(&buf, parameter, (int)options) != SUCCESS) {
1117 1118 1119 1120 1121 1122 1123
		smart_str_free(&buf);
		return NULL;
	}
#else
	php_json_encode(&buf, parameter, (int)options);
#endif
	smart_str_0(&buf);
H
heyanlong 已提交
1124 1125 1126 1127 1128 1129 1130
	if(buf.s != NULL) {
        char *bufs = emalloc(strlen(ZSTR_VAL(buf.s)) + 1);
        strcpy(bufs, ZSTR_VAL(buf.s));
        smart_str_free(&buf);
        return bufs;
	}
    return NULL;
1131 1132
}

S
songzhian 已提交
1133

H
memory  
heyanlong 已提交
1134
static void write_log(char *text) {
H
heyanlong 已提交
1135
    if (application_instance != 0) {
H
heyanlong 已提交
1136
        // to stream
H
heyanlong 已提交
1137
        if(text == NULL || strlen(text) <= 0) {
H
heyanlong 已提交
1138 1139
            return;
        }
H
heyanlong 已提交
1140 1141 1142

        struct sockaddr_un un;
        un.sun_family = AF_UNIX;
1143
        strcpy(un.sun_path, SKYWALKING_G(sock_path));
H
heyanlong 已提交
1144
        int fd;
H
heyanlong 已提交
1145 1146
        char *message = (char*) emalloc(strlen(text) + 10);
        bzero(message, strlen(text) + 10);
H
heyanlong 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

        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) {
                sprintf(message, "1%s\n", text);
                write(fd, message, strlen(message));
H
heyanlong 已提交
1159 1160
            } else {
                php_error_docref(NULL, E_WARNING, "[skywalking] failed to connect the sock.");
H
heyanlong 已提交
1161 1162
            }
            close(fd);
H
heyanlong 已提交
1163 1164
        } else {
            php_error_docref(NULL, E_WARNING, "[skywalking] failed to open the sock.");
H
heyanlong 已提交
1165
        }
H
heyanlong 已提交
1166
        efree(message);
H
heyanlong 已提交
1167
        efree(text);
H
report  
heyanlong 已提交
1168
    }
1169 1170 1171 1172

}


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

H
heyanlong 已提交
1175 1176 1177 1178 1179 1180 1181
    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 已提交
1182
    ssize_t sw3_l = 0;
H
heyanlong 已提交
1183 1184
    sw3_l = snprintf(NULL, 0, "sw3: %s|%" PRId3264 "|%d|%" PRId3264 "|#%s|#%s|#%s|%s", Z_STRVAL_P(traceId), span_id,
                     application_instance, Z_LVAL_P(entryApplicationInstance), peer_host,
H
heyanlong 已提交
1185 1186 1187
                     Z_STRVAL_P(entryOperationName), operation_name, Z_STRVAL_P(distributedTraceId));
    char *sw3 = (char*)emalloc(sw3_l + 1);
    bzero(sw3, sw3_l + 1);
H
heyanlong 已提交
1188 1189
    snprintf(sw3, sw3_l + 1, "sw3: %s|%" PRId3264 "|%d|%" PRId3264 "|#%s|#%s|#%s|%s", Z_STRVAL_P(traceId), span_id,
             application_instance, Z_LVAL_P(entryApplicationInstance), peer_host,
H
heyanlong 已提交
1190 1191
             Z_STRVAL_P(entryOperationName), operation_name, Z_STRVAL_P(distributedTraceId));
    return sw3;
H
heyanlong 已提交
1192 1193
}

1194
static char *generate_sw6(zend_long span_id, char *peer_host) {
G
goerzh 已提交
1195 1196 1197 1198 1199
    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);
1200 1201
    zval *parentEndpointName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentEndpoint",
                                                  sizeof("currentEndpoint") - 1);
G
goerzh 已提交
1202 1203
    zval *distributedTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "distributedTraceId",
                                                  sizeof("distributedTraceId") - 1);
H
6.x  
heyanlong 已提交
1204 1205 1206 1207 1208
    zval distributedTraceIdEncode;
    zval traceSegmentIdEncode;
    zval peerHostEncode;
    zval entryEndpointNameEncode;
    zval parentEndpointNameEncode;
1209
    const char digits[] = "0123456789";
H
6.x  
heyanlong 已提交
1210

H
fix 502  
heyanlong 已提交
1211
    char *sharpPeer;
1212 1213 1214 1215 1216
    if (strspn(peer_host, digits) == strlen(peer_host)) {
        spprintf(&sharpPeer, 0, "%s", peer_host);
    } else {
        spprintf(&sharpPeer, 0, "#%s", peer_host);
    }
H
fix 502  
heyanlong 已提交
1217 1218

    char *sharpEntryEndpointName;
1219 1220 1221 1222 1223
    if (strspn(Z_STRVAL_P(entryOperationName), digits) == Z_STRLEN_P(entryOperationName)) {
        spprintf(&sharpEntryEndpointName, 0, "%s", Z_STRVAL_P(entryOperationName));
    } else {
        spprintf(&sharpEntryEndpointName, 0, "#%s", Z_STRVAL_P(entryOperationName));
    }
H
fix 502  
heyanlong 已提交
1224 1225

    char *sharpParentEndpointName;
1226 1227 1228 1229 1230
    if (strspn(Z_STRVAL_P(parentEndpointName), digits) == Z_STRLEN_P(parentEndpointName)) {
        spprintf(&sharpParentEndpointName, 0, "%s", Z_STRVAL_P(parentEndpointName));
    } else {
        spprintf(&sharpParentEndpointName, 0, "#%s", Z_STRVAL_P(parentEndpointName));
    }
H
6.x  
heyanlong 已提交
1231 1232 1233 1234 1235 1236

    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 已提交
1237 1238

    ssize_t sw6_l = 0;
H
heyanlong 已提交
1239 1240
    sw6_l = snprintf(NULL, 0, "sw6: 1-%s-%s-%" PRId3264 "-%d-%" PRId3264 "-%s-%s-%s", Z_STRVAL(distributedTraceIdEncode),
                     Z_STRVAL(traceSegmentIdEncode), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
H
6.x  
heyanlong 已提交
1241 1242
                     Z_STRVAL(peerHostEncode), Z_STRVAL(entryEndpointNameEncode),
                     Z_STRVAL(parentEndpointNameEncode));
G
goerzh 已提交
1243

H
6.x  
heyanlong 已提交
1244
    char *sw6 = (char *) emalloc(sw6_l + 1);
G
goerzh 已提交
1245
    bzero(sw6, sw6_l + 1);
H
heyanlong 已提交
1246 1247
    snprintf(sw6, sw6_l + 1, "sw6: 1-%s-%s-%" PRId3264 "-%d-%" PRId3264 "-%s-%s-%s", Z_STRVAL(distributedTraceIdEncode),
             Z_STRVAL(traceSegmentIdEncode), span_id, application_instance, Z_LVAL_P(entryApplicationInstance),
H
6.x  
heyanlong 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
             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 已提交
1259 1260 1261
    return sw6;
}

H
heyanlong 已提交
1262
static char *generate_sw8(zend_long span_id, char *peer_host) {
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "traceId",sizeof("traceId") - 1);
    zval *currentTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1);
    zval *currentEndpoint = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentEndpoint", sizeof("currentEndpoint") - 1);

    zval traceIdEncode;
    zval currentTraceIdEncode;
    zval serviceEncode;
    zval serviceInstanceEncode;
    zval parentEndpointEncode;
    zval targetAddressEncode;

    zval_b64_encode(&traceIdEncode, Z_STRVAL_P(traceId));
    zval_b64_encode(&currentTraceIdEncode, Z_STRVAL_P(currentTraceId));
    zval_b64_encode(&serviceEncode, service);
    zval_b64_encode(&serviceInstanceEncode, service_instance);
    zval_b64_encode(&parentEndpointEncode, Z_STRVAL_P(currentEndpoint));
H
heyanlong 已提交
1279
    zval_b64_encode(&targetAddressEncode, peer_host);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

    ssize_t sw6_l = 0;
    sw6_l = snprintf(NULL, 0, "sw8: 1-%s-%s-%" PRId3264 "-%s-%s-%s-%s",
            Z_STRVAL(traceIdEncode),
            Z_STRVAL(currentTraceIdEncode),
            span_id,
            Z_STRVAL(serviceEncode),
            Z_STRVAL(serviceInstanceEncode),
            Z_STRVAL(parentEndpointEncode),
            Z_STRVAL(targetAddressEncode));

    char *sw6 = (char *) emalloc(sw6_l + 1);
    bzero(sw6, sw6_l + 1);
    snprintf(sw6, sw6_l + 1, "sw8: 1-%s-%s-%" PRId3264 "-%s-%s-%s-%s",
            Z_STRVAL(traceIdEncode),
            Z_STRVAL(currentTraceIdEncode),
            span_id,
            Z_STRVAL(serviceEncode),
            Z_STRVAL(serviceInstanceEncode),
            Z_STRVAL(parentEndpointEncode),
            Z_STRVAL(targetAddressEncode));

    zval_dtor(&traceIdEncode);
    zval_dtor(&currentTraceIdEncode);
    zval_dtor(&serviceEncode);
    zval_dtor(&serviceInstanceEncode);
    zval_dtor(&parentEndpointEncode);
    zval_dtor(&targetAddressEncode);
    return sw6;
}

H
heyanlong 已提交
1311 1312 1313 1314
static zend_string *trim_sharp(zval *tmp) {
    return php_trim(Z_STR_P(tmp), "#", sizeof("#") - 1, 1);
}

G
goerzh 已提交
1315
static void zval_b64_encode(zval *out, char *in) {
H
6.x  
heyanlong 已提交
1316 1317
    char *enc = b64_encode((const unsigned char*)in, strlen(in));
    ZVAL_STRING(out, enc);
G
goerzh 已提交
1318 1319 1320 1321
    free(enc);
}

static void zval_b64_decode(zval *out, char *in) {
H
6.x  
heyanlong 已提交
1322 1323
    unsigned char *dec = b64_decode((const char *) in, strlen(in));
    ZVAL_STRING(out, (char *) dec);
G
goerzh 已提交
1324 1325 1326
    free(dec);
}

H
report  
heyanlong 已提交
1327 1328 1329 1330 1331 1332
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 已提交
1333

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

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

H
report  
heyanlong 已提交
1338
    add_assoc_string(&SKYWALKING_G(context), "currentTraceId", makeTraceId);
H
heyanlong 已提交
1339
    add_assoc_long(&SKYWALKING_G(context), "isChild", 0);
H
heyanlong 已提交
1340

H
report  
heyanlong 已提交
1341 1342
    // parent
    zval *carrier = NULL;
H
heyanlong 已提交
1343
    zval *sw;
G
goerzh 已提交
1344

H
report  
heyanlong 已提交
1345
    zend_bool jit_initialization = PG(auto_globals_jit);
H
heyanlong 已提交
1346

H
report  
heyanlong 已提交
1347 1348 1349 1350 1351
    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 已提交
1352
    carrier = zend_hash_str_find(&EG(symbol_table), ZEND_STRL("_SERVER"));
G
goerzh 已提交
1353

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

H
heyanlong 已提交
1357 1358
        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 已提交
1359 1360 1361 1362

            zval temp;
            array_init(&temp);

H
heyanlong 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
            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 已提交
1388
            }
H
heyanlong 已提交
1389 1390 1391
        } else {
            add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance);
            add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance);
H
heyanlong 已提交
1392 1393
            char *uri = get_page_request_uri();
            add_assoc_string(&SKYWALKING_G(context), "entryOperationName", (uri == NULL) ? "" : uri);
H
heyanlong 已提交
1394
            add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId);
H
heyanlong 已提交
1395 1396 1397
            if(uri != NULL) {
                efree(uri);
            }
H
heyanlong 已提交
1398
        }
何延龙 已提交
1399
    } else if (SKYWALKING_G(version) == 6 || SKYWALKING_G(version) == 7) {
H
heyanlong 已提交
1400 1401 1402
        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 已提交
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

            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 已提交
1428
                ZVAL_LONG(&child, 1)
H
6.x  
heyanlong 已提交
1429 1430
                zend_hash_str_update(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1, &child);

H
6.x  
heyanlong 已提交
1431 1432 1433 1434 1435 1436 1437 1438
                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 已提交
1439 1440 1441
                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));
1442
                add_assoc_string(&SKYWALKING_G(context), "networkAddress", Z_STRVAL(sw6_6decode));
H
6.x  
heyanlong 已提交
1443
                if (sw6_7 != NULL && sw6_8 != NULL) {
H
6.x  
heyanlong 已提交
1444 1445 1446 1447 1448 1449

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

1450 1451 1452
                    add_assoc_string(&SKYWALKING_G(context), "entryOperationName", Z_STRVAL(sw6_7decode));
                    add_assoc_string(&SKYWALKING_G(context), "parentOperationName", Z_STRVAL(sw6_8decode));

H
6.x  
heyanlong 已提交
1453 1454
                    zval_dtor(&sw6_7decode);
                    zval_dtor(&sw6_8decode);
H
6.x  
heyanlong 已提交
1455
                }
H
6.x  
heyanlong 已提交
1456 1457 1458 1459 1460
                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 已提交
1461
            }
H
6.x  
heyanlong 已提交
1462 1463 1464
        } else {
            add_assoc_long(&SKYWALKING_G(context), "parentApplicationInstance", application_instance);
            add_assoc_long(&SKYWALKING_G(context), "entryApplicationInstance", application_instance);
H
heyanlong 已提交
1465
            char *uri = get_page_request_uri();
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
            char *path = NULL;
            if (uri != NULL) {
                path = (char *)emalloc(strlen(uri) + 5);
                bzero(path, strlen(uri) + 5);

                int i;
                for(i = 0; i < strlen(uri); i++) {
                    if (uri[i] == '?') {
                        break;
                    }
                    path[i] = uri[i];
                }
                path[i] = '\0';
            }

            add_assoc_string(&SKYWALKING_G(context), "entryOperationName", (path == NULL) ? "" : path);
            if (path != NULL) {
                efree(path);
            }

H
6.x  
heyanlong 已提交
1486
            add_assoc_string(&SKYWALKING_G(context), "distributedTraceId", makeTraceId);
H
heyanlong 已提交
1487 1488 1489
            if(uri != NULL) {
                efree(uri);
            }
G
goerzh 已提交
1490
        }
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
    } else if (SKYWALKING_G(version) == 8) {
        sw = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_SW8", sizeof("HTTP_SW8") - 1);
//        zval *sw;
//        array_init(&sw);
//        ZVAL_STRING(sw, "1-My40LjU=-MS4yLjM=-4-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA=");
        if (sw != NULL && Z_TYPE_P(sw) == IS_STRING && Z_STRLEN_P(sw) > 10) {
            add_assoc_string(&SKYWALKING_G(context), "sw8", Z_STRVAL_P(sw));

            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 *sw8_0 = zend_hash_index_find(Z_ARRVAL(temp), 0);
                zval *sw8_1 = zend_hash_index_find(Z_ARRVAL(temp), 1); // Trace Id base64
                zval *sw8_2 = zend_hash_index_find(Z_ARRVAL(temp), 2); // Parent trace segment Id
                zval *sw8_3 = zend_hash_index_find(Z_ARRVAL(temp), 3); // Parent span Id
                zval *sw8_4 = zend_hash_index_find(Z_ARRVAL(temp), 4); // Parent service
                zval *sw8_5 = zend_hash_index_find(Z_ARRVAL(temp), 5); // Parent service instance
                zval *sw8_6 = zend_hash_index_find(Z_ARRVAL(temp), 6); // Parent endpoint
                zval *sw8_7 = zend_hash_index_find(Z_ARRVAL(temp), 7); // Target address used at client side of this request

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

                zval sw8_1decode;
                zval sw8_2decode;
                zval sw8_4decode;
                zval sw8_5decode;
                zval sw8_6decode;
                zval sw8_7decode;
                zval_b64_decode(&sw8_1decode, Z_STRVAL_P(sw8_1));
                zval_b64_decode(&sw8_2decode, Z_STRVAL_P(sw8_2));
                zval_b64_decode(&sw8_4decode, Z_STRVAL_P(sw8_4));
                zval_b64_decode(&sw8_5decode, Z_STRVAL_P(sw8_5));
                zval_b64_decode(&sw8_6decode, Z_STRVAL_P(sw8_6));
                zval_b64_decode(&sw8_7decode, Z_STRVAL_P(sw8_7));

                add_assoc_string(&SKYWALKING_G(context), "traceId", Z_STRVAL(sw8_1decode));
                add_assoc_string(&SKYWALKING_G(context), "parentTraceSegmentId", Z_STRVAL(sw8_2decode));
                add_assoc_long(&SKYWALKING_G(context), "parentSpanId", zend_atol(Z_STRVAL_P(sw8_3), sizeof(Z_STRVAL_P(sw8_3)) - 1));
                add_assoc_string(&SKYWALKING_G(context), "parentService", Z_STRVAL(sw8_4decode));
                add_assoc_string(&SKYWALKING_G(context), "parentServiceInstance", Z_STRVAL(sw8_5decode));
                add_assoc_string(&SKYWALKING_G(context), "parentEndpoint", Z_STRVAL(sw8_6decode));
                add_assoc_string(&SKYWALKING_G(context), "targetAddress", Z_STRVAL(sw8_7decode));

                zval_dtor(&sw8_1decode);
                zval_dtor(&sw8_2decode);
                zval_dtor(&sw8_4decode);
                zval_dtor(&sw8_5decode);
                zval_dtor(&sw8_6decode);
                zval_dtor(&sw8_7decode);
            }
        } else {
            add_assoc_string(&SKYWALKING_G(context), "parentService", service);
            add_assoc_string(&SKYWALKING_G(context), "parentServiceInstance", service_instance);
            char *uri = get_page_request_uri();
            char *path = NULL;
            if (uri != NULL) {
                path = (char *)emalloc(strlen(uri) + 5);
                bzero(path, strlen(uri) + 5);

                int i;
                for(i = 0; i < strlen(uri); i++) {
                    if (uri[i] == '?') {
                        break;
                    }
                    path[i] = uri[i];
                }
                path[i] = '\0';
            }

            add_assoc_string(&SKYWALKING_G(context), "parentEndpoint", (path == NULL) ? "" : path);
            if (path != NULL) {
                efree(path);
            }

            add_assoc_string(&SKYWALKING_G(context), "traceId", makeTraceId);
            if(uri != NULL) {
                efree(uri);
            }
        }
G
goerzh 已提交
1576 1577
    }

H
memory  
heyanlong 已提交
1578
    efree(makeTraceId);
1579 1580
}

H
report  
heyanlong 已提交
1581 1582 1583 1584 1585 1586 1587

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

1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
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 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
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
heyanlong 已提交
1625 1626 1627 1628 1629 1630 1631
    if (uri.s != NULL) {
        char *uris = emalloc(strlen(ZSTR_VAL(uri.s)) + 1);
        strcpy(uris, ZSTR_VAL(uri.s));
        smart_str_free(&uri);
        return uris;
    }
    return NULL;
H
heyanlong 已提交
1632
}
1633

G
goerzh 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
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"));

H
heyanlong 已提交
1651
    request_host = zend_hash_str_find(Z_ARRVAL_P(carrier), "HTTP_HOST", sizeof("HTTP_HOST") - 1);
G
goerzh 已提交
1652
    request_port = zend_hash_str_find(Z_ARRVAL_P(carrier), "SERVER_PORT", sizeof("SERVER_PORT") - 1);
H
heyanlong 已提交
1653 1654 1655
    if (request_host == NULL) {
        request_host = zend_hash_str_find(Z_ARRVAL_P(carrier), "SERVER_ADDR", sizeof("SERVER_ADDR") - 1);
    }
G
goerzh 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665

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

1666

S
songzhian 已提交
1667
/**
H
report  
heyanlong 已提交
1668
 * ip
H
heyanlong 已提交
1669
 *
S
songzhian 已提交
1670 1671 1672 1673 1674
 * @since 2017年11月23日
 * @copyright
 * @return return_type
//  */
static char* _get_current_machine_ip(){
G
goerzh 已提交
1675 1676 1677
    char *ip;
    ip = (char *) emalloc(sizeof(char) * 100);
    bzero(ip, 100);
S
songzhian 已提交
1678

G
goerzh 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
    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 已提交
1692 1693 1694 1695

    return ip;
}

H
heyanlong 已提交
1696
static void request_init() {
S
songzhian 已提交
1697

H
heyanlong 已提交
1698
    array_init(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
1699
    array_init(&SKYWALKING_G(curl_header_send));
H
heyanlong 已提交
1700 1701
    array_init(&SKYWALKING_G(context));
	array_init(&SKYWALKING_G(UpstreamSegment));
S
songzhian 已提交
1702

H
report  
heyanlong 已提交
1703
    generate_context();
S
songzhian 已提交
1704

H
report  
heyanlong 已提交
1705
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_instance", application_instance);
H
heyanlong 已提交
1706
    add_assoc_stringl(&SKYWALKING_G(UpstreamSegment), "uuid", application_uuid, strlen(application_uuid));
H
heyanlong 已提交
1707
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "pid", getppid());
H
report  
heyanlong 已提交
1708
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_id", application_id);
H
heyanlong 已提交
1709
    add_assoc_long(&SKYWALKING_G(UpstreamSegment), "version", SKYWALKING_G(version));
H
heyanlong 已提交
1710
	SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "segment");
H
report  
heyanlong 已提交
1711 1712
	SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "globalTraceIds");

1713 1714 1715
    add_assoc_stringl(&SKYWALKING_G(UpstreamSegment), "service", service, strlen(service));
    add_assoc_stringl(&SKYWALKING_G(UpstreamSegment), "serviceInstance", service_instance, strlen(service_instance));

H
report  
heyanlong 已提交
1716
	zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "currentTraceId", sizeof("currentTraceId") - 1);
S
songzhian 已提交
1717

H
heyanlong 已提交
1718 1719 1720 1721
	zval traceSegmentObject;
	zval spans;
	array_init(&spans);
	array_init(&traceSegmentObject);
H
report  
heyanlong 已提交
1722 1723
	add_assoc_string(&traceSegmentObject, "traceSegmentId", Z_STRVAL_P(traceId));
	add_assoc_long(&traceSegmentObject, "isSizeLimited", 0);
S
songzhian 已提交
1724

H
heyanlong 已提交
1725
	zval temp;
H
heyanlong 已提交
1726 1727
    char *peer = NULL;
    char *uri = get_page_request_uri();
H
fix 502  
heyanlong 已提交
1728 1729 1730
    char *path = (char*)emalloc(strlen(uri) + 5);
    bzero(path, strlen(uri) + 5);

H
heyanlong 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739

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

H
heyanlong 已提交
1741
	array_init(&temp);
H
heyanlong 已提交
1742 1743 1744 1745 1746 1747 1748
    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 已提交
1749

H
heyanlong 已提交
1750 1751 1752 1753 1754 1755
    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 已提交
1756
    add_assoc_string(&temp, "operationName", path);
G
goerzh 已提交
1757
    add_assoc_string(&temp, "peer", (peer == NULL) ? "" : peer);
H
heyanlong 已提交
1758 1759
    add_assoc_long(&temp, "spanType", 0);
    add_assoc_long(&temp, "spanLayer", 3);
1760
    add_assoc_long(&temp, "componentId", COMPONENT_UNDERTOW);
S
songzhian 已提交
1761

1762
    // sw8 or sw6 for parent endpoint name
1763 1764 1765
    add_assoc_string(&SKYWALKING_G(context), "currentEndpoint", path);

    efree(path);
G
goerzh 已提交
1766 1767 1768
    if (peer != NULL) {
        efree(peer);
    }
H
heyanlong 已提交
1769 1770 1771
    if (uri != NULL) {
        efree(uri);
    }
G
goerzh 已提交
1772

H
heyanlong 已提交
1773 1774 1775 1776
    zval *isChild = zend_hash_str_find(Z_ARRVAL_P(&SKYWALKING_G(context)), "isChild", sizeof("isChild") - 1);
    // refs
    zval refs;
    array_init(&refs);
H
heyanlong 已提交
1777 1778 1779 1780 1781

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

H
heyanlong 已提交
1782
    add_assoc_string(&SKYWALKING_G(UpstreamSegment), "traceId", Z_STRVAL_P(traceId));
1783

H
heyanlong 已提交
1784
    if(Z_LVAL_P(isChild) == 1) {
H
memory  
heyanlong 已提交
1785 1786
        zval ref;
        array_init(&ref);
H
heyanlong 已提交
1787 1788 1789 1790 1791 1792
        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 已提交
1793
        zval *parentOperationName = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentOperationName", sizeof("parentOperationName") - 1);
H
heyanlong 已提交
1794
        zval *distributedTraceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "distributedTraceId", sizeof("distributedTraceId") - 1);
H
heyanlong 已提交
1795 1796 1797
        add_assoc_long(&ref, "type", 0);
        add_assoc_string(&ref, "parentTraceSegmentId", Z_STRVAL_P(parentTraceSegmentId));
        add_assoc_long(&ref, "parentSpanId", Z_LVAL_P(parentSpanId));
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820

        if (SKYWALKING_G(version) == 8) {
            zval *traceId = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "traceId", sizeof("traceId") - 1);
            zval *parentService = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentService", sizeof("parentService") - 1);
            zval *parentServiceInstance = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentServiceInstance", sizeof("parentServiceInstance") - 1);
            zval *parentEndpoint = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "parentEndpoint", sizeof("parentEndpoint") - 1);
            zval *targetAddress = zend_hash_str_find(Z_ARRVAL(SKYWALKING_G(context)), "targetAddress", sizeof("targetAddress") - 1);

            add_assoc_string(&ref, "traceId", Z_STRVAL_P(traceId));
            add_assoc_string(&ref, "parentService", Z_STRVAL_P(parentService));
            add_assoc_string(&ref, "parentServiceInstance", Z_STRVAL_P(parentServiceInstance));
            add_assoc_string(&ref, "parentEndpoint", Z_STRVAL_P(parentEndpoint));
            add_assoc_string(&ref, "targetAddress", Z_STRVAL_P(targetAddress));
            zend_hash_str_update(Z_ARRVAL(SKYWALKING_G(UpstreamSegment)), "traceId", sizeof("traceId") - 1, traceId);
        } else {
            add_assoc_long(&ref, "parentApplicationInstanceId", Z_LVAL_P(parentApplicationInstance));
            add_assoc_long(&ref, "entryApplicationInstanceId", Z_LVAL_P(entryApplicationInstance));
            add_assoc_string(&ref, "networkAddress", Z_STRVAL_P(networkAddress));
            add_assoc_string(&ref, "entryServiceName", Z_STRVAL_P(entryOperationName));
            add_assoc_string(&ref, "parentServiceName", Z_STRVAL_P(parentOperationName));
            ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(distributedTraceId));
        }

H
heyanlong 已提交
1821
        zend_hash_next_index_insert(Z_ARRVAL(refs), &ref);
H
heyanlong 已提交
1822 1823
    } else {
        ZVAL_STRING(&tmpGlobalTraceIds, Z_STRVAL_P(traceId));
H
heyanlong 已提交
1824 1825 1826
    }

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

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

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

H
report  
heyanlong 已提交
1833 1834
    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 已提交
1835 1836 1837
}


H
report  
heyanlong 已提交
1838
static void sky_flush_all() {
S
songzhian 已提交
1839
	char *l_millisecond = get_millisecond();
H
heyanlong 已提交
1840 1841 1842 1843
	long millisecond = zend_atol(l_millisecond, strlen(l_millisecond));
	efree(l_millisecond);

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

H
heyanlong 已提交
1845 1846
	add_assoc_long(span, "endTime", millisecond);
	if ((SG(sapi_headers).http_response_code >= 500)) {
H
heyanlong 已提交
1847
		add_assoc_long(span, "isError", 1);
H
heyanlong 已提交
1848
	} else {
H
heyanlong 已提交
1849
		add_assoc_long(span, "isError", 0);
H
heyanlong 已提交
1850
	}
S
songzhian 已提交
1851

H
heyanlong 已提交
1852 1853
	write_log(sky_json_encode(&SKYWALKING_G(UpstreamSegment)));
}
S
songzhian 已提交
1854

H
heyanlong 已提交
1855 1856 1857 1858 1859 1860
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 已提交
1861

H
heyanlong 已提交
1862 1863 1864 1865
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 已提交
1866 1867 1868
}


H
heyanlong 已提交
1869
static int sky_register() {
H
heyanlong 已提交
1870
    if (application_instance == 0) {
H
heyanlong 已提交
1871 1872
        struct sockaddr_un un;
        un.sun_family = AF_UNIX;
1873
        strcpy(un.sun_path, SKYWALKING_G(sock_path));
H
heyanlong 已提交
1874 1875 1876 1877 1878 1879
        int fd;
        char message[4096];
        char return_message[4096];

        fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd >= 0) {
H
heyanlong 已提交
1880 1881 1882
            struct timeval tv;
            tv.tv_sec = 0;
            tv.tv_usec = 100000;
H
heyanlong 已提交
1883
            setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv, sizeof tv);
H
heyanlong 已提交
1884 1885 1886 1887
            int conn = connect(fd, (struct sockaddr *) &un, sizeof(un));

            if (conn >= 0) {
                bzero(message, sizeof(message));
H
heyanlong 已提交
1888 1889
                sprintf(message, "0{\"app_code\":\"%s\",\"pid\":%d,\"version\":%d}\n", SKYWALKING_G(app_code),
                        getppid(), SKYWALKING_G(version));
H
heyanlong 已提交
1890 1891 1892 1893 1894
                write(fd, message, strlen(message));

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

H
heyanlong 已提交
1895
                char *ids[10] = {0};
H
heyanlong 已提交
1896 1897 1898 1899
                int i = 0;
                char *p = strtok(return_message, ",");
                while (p != NULL) {
                    ids[i++] = p;
H
heyanlong 已提交
1900
                    p = strtok(NULL, ",");
H
heyanlong 已提交
1901
                }
H
heyanlong 已提交
1902

H
heyanlong 已提交
1903
                if (ids[0] != NULL && ids[1] != NULL && ids[2] != NULL) {
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
                    if (SKYWALKING_G(version) == 6 || SKYWALKING_G(version) == 7) {
                        application_id = atoi(ids[0]);
                        application_instance = atoi(ids[1]);
                        strncpy(application_uuid, ids[2], sizeof application_uuid - 1);
                    } else if (SKYWALKING_G(version) == 8) {
                        application_id = 1;
                        application_instance = 1;
                        strncpy(service, ids[0], sizeof service - 1);
                        strncpy(service_instance, ids[1], sizeof service_instance - 1);
                        strncpy(application_uuid, ids[2], sizeof application_uuid - 1);
                    }
H
heyanlong 已提交
1915
                }
1916

H
heyanlong 已提交
1917 1918
            } else {
                php_error_docref(NULL, E_WARNING, "[skywalking] failed to connect the sock.");
H
heyanlong 已提交
1919 1920 1921
            }

            close(fd);
H
heyanlong 已提交
1922 1923
        } else {
            php_error_docref(NULL, E_WARNING, "[skywalking] failed to open the sock.");
H
heyanlong 已提交
1924 1925 1926 1927 1928
        }
    }
    return 0;
}

S
songzhian 已提交
1929

1930 1931
/* {{{ PHP_MINIT_FUNCTION
 */
H
heyanlong 已提交
1932
PHP_MINIT_FUNCTION (skywalking) {
H
docs  
heyanlong 已提交
1933
	ZEND_INIT_MODULE_GLOBALS(skywalking, php_skywalking_init_globals, NULL);
S
songzhian 已提交
1934
	//data_register_hashtable();
1935 1936 1937
	REGISTER_INI_ENTRIES();
	/* If you have INI entries, uncomment these lines
	*/
H
report  
heyanlong 已提交
1938
	if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
1939
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
H
heyanlong 已提交
1940
            return SUCCESS;
1941 1942
        }

1943
        // 用户自定义函数执行器(php脚本定义的类、函数)
H
predis  
heyanlong 已提交
1944 1945 1946
        ori_execute_ex = zend_execute_ex;
        zend_execute_ex = sky_execute_ex;

1947
        // 内部函数执行器(c语言定义的类、函数)
H
mysql  
heyanlong 已提交
1948 1949 1950
        ori_execute_internal = zend_execute_internal;
        zend_execute_internal = sky_execute_internal;

H
heyanlong 已提交
1951
		// bind curl
1952
		zend_function *old_function;
H
heyanlong 已提交
1953
		if ((old_function = zend_hash_str_find_ptr(CG(function_table), "curl_exec", sizeof("curl_exec") - 1)) != NULL) {
1954
			orig_curl_exec = old_function->internal_function.handler;
H
heyanlong 已提交
1955
			old_function->internal_function.handler = sky_curl_exec_handler;
1956
		}
H
heyanlong 已提交
1957 1958 1959 1960
        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 已提交
1961 1962 1963 1964
        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 已提交
1965 1966 1967 1968
        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;
        }
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
	}

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(skywalking)
{
	UNREGISTER_INI_ENTRIES();
H
memory  
heyanlong 已提交
1980
    return SUCCESS;
1981 1982 1983 1984
}
/* }}} */

/* Remove if there's nothing to do at request start */
S
songzhian 已提交
1985
/* {{{ PHP_RINIT_FUNCTION7
1986 1987 1988
 */
PHP_RINIT_FUNCTION(skywalking)
{
S
songzhian 已提交
1989

1990 1991 1992
#if defined(COMPILE_DL_SKYWALKING) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif
H
heyanlong 已提交
1993
    if (SKYWALKING_G(enable)) {
H
heyanlong 已提交
1994 1995 1996
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
            return SUCCESS;
        }
H
heyanlong 已提交
1997 1998
        sky_register();
        if (application_instance == 0) {
H
heyanlong 已提交
1999 2000
            return SUCCESS;
        }
H
heyanlong 已提交
2001 2002 2003 2004
        sky_increment_id++;
        if (sky_increment_id >= 9999) {
            sky_increment_id = 0;
        }
H
memory  
heyanlong 已提交
2005
        request_init();
H
heyanlong 已提交
2006 2007
    }
    return SUCCESS;
2008 2009 2010 2011 2012 2013 2014 2015
}
/* }}} */

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

H
report  
heyanlong 已提交
2017
	if(SKYWALKING_G(enable)){
H
heyanlong 已提交
2018 2019 2020
        if (strcasecmp("cli", sapi_module.name) == 0 && cli_debug == 0) {
            return SUCCESS;
        }
2021
        if (application_instance == 0) {
H
memory  
heyanlong 已提交
2022 2023
            return SUCCESS;
        }
S
songzhian 已提交
2024
		sky_flush_all();
H
memory  
heyanlong 已提交
2025
        zval_dtor(&SKYWALKING_G(context));
H
heyanlong 已提交
2026
        zval_dtor(&SKYWALKING_G(curl_header));
H
heyanlong 已提交
2027
        zval_dtor(&SKYWALKING_G(curl_header_send));
H
memory  
heyanlong 已提交
2028
        zval_dtor(&SKYWALKING_G(UpstreamSegment));
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
	}
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(skywalking)
{
	DISPLAY_INI_ENTRIES();
}
/* }}} */

H
memory  
heyanlong 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
/* {{{ PHP_MINFO_FUNCTION
 */
PHP_GINIT_FUNCTION(skywalking)
{
    memset(skywalking_globals, 0, sizeof(*skywalking_globals));
}
/* }}} */

zend_module_dep skywalking_deps[] = {
        ZEND_MOD_REQUIRED("json")
H
heyanlong 已提交
2052 2053 2054 2055
        ZEND_MOD_REQUIRED("pcre")
        ZEND_MOD_REQUIRED("standard")
        ZEND_MOD_REQUIRED("curl")
        ZEND_MOD_END
H
memory  
heyanlong 已提交
2056
};
2057 2058 2059 2060

/* {{{ skywalking_module_entry
 */
zend_module_entry skywalking_module_entry = {
H
heyanlong 已提交
2061 2062 2063
	STANDARD_MODULE_HEADER_EX,
	NULL,
	skywalking_deps,
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
	"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
 */