diff --git a/config.m4 b/config.m4 index fd05022f40d087ae06109764714a9cdce47f0439..7c7ccc66f641e39f11fa9bdab679207c5a94d2dc 100644 --- a/config.m4 +++ b/config.m4 @@ -1,6 +1,9 @@ PHP_ARG_ENABLE(skywalking, whether to enable skywalking support, [ --enable-skywalking Enable skywalking support]) +PHP_ARG_ENABLE(mysqli, whether to enable mysqli support, +[ --enable-mysqli Enable mysqli support], no, no) + if test "$PHP_SKYWALKING" != "no"; then PHP_NEW_EXTENSION(skywalking, \ skywalking.c \ @@ -8,3 +11,7 @@ if test "$PHP_SKYWALKING" != "no"; then encode.c \ , $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) fi + +if test "$PHP_MYSQLI" != "no"; then + AC_DEFINE(SKYWALKING_ENABLED_MYSQLI, 1, [Enable skywalking support]) +fi diff --git a/skywalking.c b/skywalking.c index 194ec5aea1428583b1554c18b324ec8de1625d91..76da7a8d9a98362ca1d2250ac2bfb949674bf392 100644 --- a/skywalking.c +++ b/skywalking.c @@ -249,6 +249,10 @@ ZEND_API void sky_execute_internal(zend_execute_data *execute_data, zval *return zf->common.scope->name) : NULL; const char *function_name = zf->common.function_name == NULL ? NULL : ZSTR_VAL(zf->common.function_name); +#ifdef SKYWALKING_ENABLED_MYSQLI + int is_mysqli_function = 0; +#endif + char *operationName = NULL; char *component = NULL; if (class_name != NULL) { @@ -274,7 +278,33 @@ ZEND_API void sky_execute_internal(zend_execute_data *execute_data, zval *return strcat(operationName, function_name); } } +#ifdef SKYWALKING_ENABLED_MYSQLI + if (strcmp(class_name, "mysqli") == 0) { + 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); + } + } +#endif } else if (function_name != NULL) { +#ifdef SKYWALKING_ENABLED_MYSQLI + 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); + + is_mysqli_function = 1; + } +#endif } if (operationName != NULL) { @@ -324,8 +354,27 @@ ZEND_API void sky_execute_internal(zend_execute_data *execute_data, zval *return add_assoc_string(&tags, "db.data_source", (char *) stmt->dbh->data_source); } } + } else if (function_name != NULL) { + #ifdef SKYWALKING_ENABLED_MYSQLI + if (strcmp(class_name, "mysqli") == 0 && strcmp(function_name, "query") == 0) { + add_assoc_string(&tags, "db.type", "mysqli"); + // params + uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data); + if (arg_count) { + zval *p = is_mysqli_function ? 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; + + } + } + } + #endif } + zval temp; zval *spans = NULL; zval *span_id = NULL; @@ -1304,9 +1353,11 @@ PHP_MINIT_FUNCTION (skywalking) { return SUCCESS; } + // 用户自定义函数执行器(php脚本定义的类、函数) ori_execute_ex = zend_execute_ex; zend_execute_ex = sky_execute_ex; + // 内部函数执行器(c语言定义的类、函数) ori_execute_internal = zend_execute_internal; zend_execute_internal = sky_execute_internal;