|
66 | 66 | /* Our stuffz */ |
67 | 67 | #include "php_phongo.h" |
68 | 68 | #include "php_bson.h" |
| 69 | +#include "src/phongo_apm.h" |
69 | 70 | #include "src/BSON/functions.h" |
70 | 71 | #include "src/MongoDB/Monitoring/functions.h" |
71 | 72 |
|
@@ -2330,181 +2331,6 @@ static bool php_phongo_apply_driver_options_to_uri(mongoc_uri_t* uri, zval* zopt |
2330 | 2331 | } |
2331 | 2332 | #endif |
2332 | 2333 |
|
2333 | | -/* APM callbacks */ |
2334 | | -static void php_phongo_dispatch_handlers(const char* name, zval* z_event) |
2335 | | -{ |
2336 | | - zval* value; |
2337 | | - |
2338 | | - ZEND_HASH_FOREACH_VAL_IND(MONGODB_G(subscribers), value) |
2339 | | - { |
2340 | | - if (EG(exception)) { |
2341 | | - break; |
2342 | | - } |
2343 | | - /* We can't use the zend_call_method_with_1_params macro here, as it |
2344 | | - * does a sizeof() on the name argument, which does only work with |
2345 | | - * constant names, but not with parameterized ones as it does |
2346 | | - * "sizeof(char*)" in that case. */ |
2347 | | - zend_call_method(PHONGO_COMPAT_OBJ_P(value), NULL, NULL, name, strlen(name), NULL, 1, z_event, NULL); |
2348 | | - } |
2349 | | - ZEND_HASH_FOREACH_END(); |
2350 | | -} |
2351 | | - |
2352 | | -/* Search for a Manager associated with the given client in the request-scoped |
2353 | | - * registry. If any Manager is found, copy it into the output parameter |
2354 | | - * (incrementing its ref-count) and return true; otherwise, set the output |
2355 | | - * parameter to undefined and return false. */ |
2356 | | -static bool php_phongo_copy_manager_for_client(mongoc_client_t* client, zval* out) |
2357 | | -{ |
2358 | | - php_phongo_manager_t* manager; |
2359 | | - |
2360 | | - if (!MONGODB_G(managers) || zend_hash_num_elements(MONGODB_G(managers)) == 0) { |
2361 | | - return false; |
2362 | | - } |
2363 | | - |
2364 | | - ZEND_HASH_FOREACH_PTR(MONGODB_G(managers), manager) |
2365 | | - { |
2366 | | - if (manager->client == client) { |
2367 | | - ZVAL_OBJ(out, &manager->std); |
2368 | | - Z_ADDREF_P(out); |
2369 | | - |
2370 | | - return true; |
2371 | | - } |
2372 | | - } |
2373 | | - ZEND_HASH_FOREACH_END(); |
2374 | | - |
2375 | | - ZVAL_UNDEF(out); |
2376 | | - |
2377 | | - return false; |
2378 | | -} |
2379 | | - |
2380 | | -static void php_phongo_command_started(const mongoc_apm_command_started_t* event) |
2381 | | -{ |
2382 | | - php_phongo_commandstartedevent_t* p_event; |
2383 | | - zval z_event; |
2384 | | - |
2385 | | - /* Return early if there are no APM subscribers to notify */ |
2386 | | - if (!MONGODB_G(subscribers) || zend_hash_num_elements(MONGODB_G(subscribers)) == 0) { |
2387 | | - return; |
2388 | | - } |
2389 | | - |
2390 | | - object_init_ex(&z_event, php_phongo_commandstartedevent_ce); |
2391 | | - p_event = Z_COMMANDSTARTEDEVENT_OBJ_P(&z_event); |
2392 | | - |
2393 | | - p_event->command_name = estrdup(mongoc_apm_command_started_get_command_name(event)); |
2394 | | - p_event->server_id = mongoc_apm_command_started_get_server_id(event); |
2395 | | - p_event->operation_id = mongoc_apm_command_started_get_operation_id(event); |
2396 | | - p_event->request_id = mongoc_apm_command_started_get_request_id(event); |
2397 | | - p_event->command = bson_copy(mongoc_apm_command_started_get_command(event)); |
2398 | | - p_event->database_name = estrdup(mongoc_apm_command_started_get_database_name(event)); |
2399 | | - |
2400 | | - if (!php_phongo_copy_manager_for_client(mongoc_apm_command_started_get_context(event), &p_event->manager)) { |
2401 | | - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Found no Manager for client in APM event context"); |
2402 | | - zval_ptr_dtor(&z_event); |
2403 | | - |
2404 | | - return; |
2405 | | - } |
2406 | | - |
2407 | | - php_phongo_dispatch_handlers("commandStarted", &z_event); |
2408 | | - zval_ptr_dtor(&z_event); |
2409 | | -} |
2410 | | - |
2411 | | -static void php_phongo_command_succeeded(const mongoc_apm_command_succeeded_t* event) |
2412 | | -{ |
2413 | | - php_phongo_commandsucceededevent_t* p_event; |
2414 | | - zval z_event; |
2415 | | - |
2416 | | - /* Return early if there are no APM subscribers to notify */ |
2417 | | - if (!MONGODB_G(subscribers) || zend_hash_num_elements(MONGODB_G(subscribers)) == 0) { |
2418 | | - return; |
2419 | | - } |
2420 | | - |
2421 | | - object_init_ex(&z_event, php_phongo_commandsucceededevent_ce); |
2422 | | - p_event = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(&z_event); |
2423 | | - |
2424 | | - p_event->command_name = estrdup(mongoc_apm_command_succeeded_get_command_name(event)); |
2425 | | - p_event->server_id = mongoc_apm_command_succeeded_get_server_id(event); |
2426 | | - p_event->operation_id = mongoc_apm_command_succeeded_get_operation_id(event); |
2427 | | - p_event->request_id = mongoc_apm_command_succeeded_get_request_id(event); |
2428 | | - p_event->duration_micros = mongoc_apm_command_succeeded_get_duration(event); |
2429 | | - p_event->reply = bson_copy(mongoc_apm_command_succeeded_get_reply(event)); |
2430 | | - |
2431 | | - if (!php_phongo_copy_manager_for_client(mongoc_apm_command_succeeded_get_context(event), &p_event->manager)) { |
2432 | | - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Found no Manager for client in APM event context"); |
2433 | | - zval_ptr_dtor(&z_event); |
2434 | | - |
2435 | | - return; |
2436 | | - } |
2437 | | - |
2438 | | - php_phongo_dispatch_handlers("commandSucceeded", &z_event); |
2439 | | - zval_ptr_dtor(&z_event); |
2440 | | -} |
2441 | | - |
2442 | | -static void php_phongo_command_failed(const mongoc_apm_command_failed_t* event) |
2443 | | -{ |
2444 | | - php_phongo_commandfailedevent_t* p_event; |
2445 | | - zval z_event; |
2446 | | - bson_error_t tmp_error = { 0 }; |
2447 | | - zend_class_entry* default_exception_ce; |
2448 | | - |
2449 | | - default_exception_ce = zend_exception_get_default(); |
2450 | | - |
2451 | | - /* Return early if there are no APM subscribers to notify */ |
2452 | | - if (!MONGODB_G(subscribers) || zend_hash_num_elements(MONGODB_G(subscribers)) == 0) { |
2453 | | - return; |
2454 | | - } |
2455 | | - |
2456 | | - object_init_ex(&z_event, php_phongo_commandfailedevent_ce); |
2457 | | - p_event = Z_COMMANDFAILEDEVENT_OBJ_P(&z_event); |
2458 | | - |
2459 | | - p_event->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event)); |
2460 | | - p_event->server_id = mongoc_apm_command_failed_get_server_id(event); |
2461 | | - p_event->operation_id = mongoc_apm_command_failed_get_operation_id(event); |
2462 | | - p_event->request_id = mongoc_apm_command_failed_get_request_id(event); |
2463 | | - p_event->duration_micros = mongoc_apm_command_failed_get_duration(event); |
2464 | | - p_event->reply = bson_copy(mongoc_apm_command_failed_get_reply(event)); |
2465 | | - |
2466 | | - if (!php_phongo_copy_manager_for_client(mongoc_apm_command_failed_get_context(event), &p_event->manager)) { |
2467 | | - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Found no Manager for client in APM event context"); |
2468 | | - zval_ptr_dtor(&z_event); |
2469 | | - |
2470 | | - return; |
2471 | | - } |
2472 | | - |
2473 | | - /* We need to process and convert the error right here, otherwise |
2474 | | - * debug_info will turn into a recursive loop, and with the wrong trace |
2475 | | - * locations */ |
2476 | | - mongoc_apm_command_failed_get_error(event, &tmp_error); |
2477 | | - |
2478 | | - object_init_ex(&p_event->z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code)); |
2479 | | - zend_update_property_string(default_exception_ce, PHONGO_COMPAT_OBJ_P(&p_event->z_error), ZEND_STRL("message"), tmp_error.message); |
2480 | | - zend_update_property_long(default_exception_ce, PHONGO_COMPAT_OBJ_P(&p_event->z_error), ZEND_STRL("code"), tmp_error.code); |
2481 | | - |
2482 | | - php_phongo_dispatch_handlers("commandFailed", &z_event); |
2483 | | - zval_ptr_dtor(&z_event); |
2484 | | -} |
2485 | | - |
2486 | | -/* Sets the callbacks for APM */ |
2487 | | -bool php_phongo_set_monitoring_callbacks(mongoc_client_t* client) |
2488 | | -{ |
2489 | | - bool retval; |
2490 | | - |
2491 | | - mongoc_apm_callbacks_t* callbacks = mongoc_apm_callbacks_new(); |
2492 | | - |
2493 | | - mongoc_apm_set_command_started_cb(callbacks, php_phongo_command_started); |
2494 | | - mongoc_apm_set_command_succeeded_cb(callbacks, php_phongo_command_succeeded); |
2495 | | - mongoc_apm_set_command_failed_cb(callbacks, php_phongo_command_failed); |
2496 | | - |
2497 | | - retval = mongoc_client_set_apm_callbacks(client, callbacks, client); |
2498 | | - |
2499 | | - if (!retval) { |
2500 | | - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to set APM callbacks"); |
2501 | | - } |
2502 | | - |
2503 | | - mongoc_apm_callbacks_destroy(callbacks); |
2504 | | - |
2505 | | - return retval; |
2506 | | -} |
2507 | | - |
2508 | 2334 | static zval* php_phongo_manager_prepare_manager_for_hash(zval* driverOptions, bool* free) |
2509 | 2335 | { |
2510 | 2336 | php_phongo_manager_t* manager; |
@@ -3485,7 +3311,10 @@ void phongo_manager_init(php_phongo_manager_t* manager, const char* uri_string, |
3485 | 3311 | goto cleanup; |
3486 | 3312 | } |
3487 | 3313 |
|
3488 | | - php_phongo_set_monitoring_callbacks(manager->client); |
| 3314 | + if (!phongo_apm_set_callbacks(manager->client)) { |
| 3315 | + /* Exception should already have been thrown */ |
| 3316 | + goto cleanup; |
| 3317 | + } |
3489 | 3318 |
|
3490 | 3319 | MONGOC_DEBUG("Created client with hash: %s", manager->client_hash); |
3491 | 3320 |
|
|
0 commit comments