Skip to content

Commit dc3973d

Browse files
authored
PHPC-1899: getServerConnectionId for command monitoring events (#1316)
1 parent 98727ae commit dc3973d

16 files changed

+382
-23
lines changed

src/LIBMONGOC_VERSION_CURRENT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.11.1-20220328+git12b697c3da
1+
1.11.1-20220427+gitba73833f18

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ PHP_METHOD(CommandFailedEvent, getServiceId)
188188
phongo_objectid_init(return_value, &intern->service_id);
189189
} /* }}} */
190190

191+
/* {{{ proto int|null CommandFailedEvent::getServerConnectionId()
192+
Returns the event's server connection ID */
193+
PHP_METHOD(CommandFailedEvent, getServerConnectionId)
194+
{
195+
php_phongo_commandfailedevent_t* intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis());
196+
197+
PHONGO_PARSE_PARAMETERS_NONE();
198+
199+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
200+
if (intern->server_connection_id == -1) {
201+
RETURN_NULL();
202+
}
203+
204+
RETURN_LONG(intern->server_connection_id);
205+
} /* }}} */
206+
191207
/**
192208
* Event thrown when a command has failed to execute.
193209
*
@@ -209,6 +225,7 @@ static zend_function_entry php_phongo_commandfailedevent_me[] = {
209225
PHP_ME(CommandFailedEvent, getRequestId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
210226
PHP_ME(CommandFailedEvent, getServer, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
211227
PHP_ME(CommandFailedEvent, getServiceId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
228+
PHP_ME(CommandFailedEvent, getServerConnectionId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
212229
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
213230
PHP_FE_END
214231
/* clang-format on */
@@ -301,6 +318,13 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj
301318
ADD_ASSOC_NULL_EX(&retval, "serviceId");
302319
}
303320

321+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
322+
if (intern->server_connection_id == -1) {
323+
ADD_ASSOC_NULL_EX(&retval, "serverConnectionId");
324+
} else {
325+
ADD_ASSOC_LONG_EX(&retval, "serverConnectionId", intern->server_connection_id);
326+
}
327+
304328
done:
305329
return Z_ARRVAL(retval);
306330
} /* }}} */

src/MongoDB/Monitoring/CommandStartedEvent.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ PHP_METHOD(CommandStartedEvent, getServiceId)
169169
phongo_objectid_init(return_value, &intern->service_id);
170170
} /* }}} */
171171

172+
/* {{{ proto int|null CommandStartedEvent::getServerConnectionId()
173+
Returns the event's server connection ID */
174+
PHP_METHOD(CommandStartedEvent, getServerConnectionId)
175+
{
176+
php_phongo_commandstartedevent_t* intern = Z_COMMANDSTARTEDEVENT_OBJ_P(getThis());
177+
178+
PHONGO_PARSE_PARAMETERS_NONE();
179+
180+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
181+
if (intern->server_connection_id == -1) {
182+
RETURN_NULL();
183+
}
184+
185+
RETURN_LONG(intern->server_connection_id);
186+
} /* }}} */
187+
172188
/**
173189
* Event thrown when a command has started to execute.
174190
*
@@ -189,6 +205,7 @@ static zend_function_entry php_phongo_commandstartedevent_me[] = {
189205
PHP_ME(CommandStartedEvent, getRequestId, ai_CommandStartedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
190206
PHP_ME(CommandStartedEvent, getServer, ai_CommandStartedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
191207
PHP_ME(CommandStartedEvent, getServiceId, ai_CommandStartedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
208+
PHP_ME(CommandStartedEvent, getServerConnectionId, ai_CommandStartedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
192209
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandStartedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
193210
PHP_FE_END
194211
/* clang-format on */
@@ -278,6 +295,13 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(phongo_compat_ob
278295
ADD_ASSOC_NULL_EX(&retval, "serviceId");
279296
}
280297

298+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
299+
if (intern->server_connection_id == -1) {
300+
ADD_ASSOC_NULL_EX(&retval, "serverConnectionId");
301+
} else {
302+
ADD_ASSOC_LONG_EX(&retval, "serverConnectionId", intern->server_connection_id);
303+
}
304+
281305
done:
282306
return Z_ARRVAL(retval);
283307
} /* }}} */

src/MongoDB/Monitoring/CommandSucceededEvent.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ PHP_METHOD(CommandSucceededEvent, getServiceId)
169169
phongo_objectid_init(return_value, &intern->service_id);
170170
} /* }}} */
171171

172+
/* {{{ proto int|null CommandSucceededEvent::getServerConnectionId()
173+
Returns the event's server connection ID */
174+
PHP_METHOD(CommandSucceededEvent, getServerConnectionId)
175+
{
176+
php_phongo_commandsucceededevent_t* intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
177+
178+
PHONGO_PARSE_PARAMETERS_NONE();
179+
180+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
181+
if (intern->server_connection_id == -1) {
182+
RETURN_NULL();
183+
}
184+
185+
RETURN_LONG(intern->server_connection_id);
186+
} /* }}} */
187+
172188
/**
173189
* Event thrown when a command has succeeded to execute.
174190
*
@@ -189,6 +205,7 @@ static zend_function_entry php_phongo_commandsucceededevent_me[] = {
189205
PHP_ME(CommandSucceededEvent, getRequestId, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
190206
PHP_ME(CommandSucceededEvent, getServer, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
191207
PHP_ME(CommandSucceededEvent, getServiceId, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
208+
PHP_ME(CommandSucceededEvent, getServerConnectionId, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
192209
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
193210
PHP_FE_END
194211
/* clang-format on */
@@ -274,6 +291,13 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_
274291
ADD_ASSOC_NULL_EX(&retval, "serviceId");
275292
}
276293

294+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
295+
if (intern->server_connection_id == -1) {
296+
ADD_ASSOC_NULL_EX(&retval, "serverConnectionId");
297+
} else {
298+
ADD_ASSOC_LONG_EX(&retval, "serverConnectionId", intern->server_connection_id);
299+
}
300+
277301
done:
278302
return Z_ARRVAL(retval);
279303
} /* }}} */

src/libmongoc

Submodule libmongoc updated 52 files

src/phongo_apm.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,14 @@ static void phongo_apm_command_started(const mongoc_apm_command_started_t* event
143143
object_init_ex(&z_event, php_phongo_commandstartedevent_ce);
144144
p_event = Z_COMMANDSTARTEDEVENT_OBJ_P(&z_event);
145145

146-
p_event->command_name = estrdup(mongoc_apm_command_started_get_command_name(event));
147-
p_event->server_id = mongoc_apm_command_started_get_server_id(event);
148-
p_event->operation_id = mongoc_apm_command_started_get_operation_id(event);
149-
p_event->request_id = mongoc_apm_command_started_get_request_id(event);
150-
p_event->command = bson_copy(mongoc_apm_command_started_get_command(event));
151-
p_event->database_name = estrdup(mongoc_apm_command_started_get_database_name(event));
152-
p_event->has_service_id = mongoc_apm_command_started_get_service_id(event) != NULL;
146+
p_event->command_name = estrdup(mongoc_apm_command_started_get_command_name(event));
147+
p_event->server_id = mongoc_apm_command_started_get_server_id(event);
148+
p_event->operation_id = mongoc_apm_command_started_get_operation_id(event);
149+
p_event->request_id = mongoc_apm_command_started_get_request_id(event);
150+
p_event->command = bson_copy(mongoc_apm_command_started_get_command(event));
151+
p_event->database_name = estrdup(mongoc_apm_command_started_get_database_name(event));
152+
p_event->server_connection_id = mongoc_apm_command_started_get_server_connection_id(event);
153+
p_event->has_service_id = mongoc_apm_command_started_get_service_id(event) != NULL;
153154

154155
if (p_event->has_service_id) {
155156
bson_oid_copy(mongoc_apm_command_started_get_service_id(event), &p_event->service_id);
@@ -188,13 +189,14 @@ static void phongo_apm_command_succeeded(const mongoc_apm_command_succeeded_t* e
188189
object_init_ex(&z_event, php_phongo_commandsucceededevent_ce);
189190
p_event = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(&z_event);
190191

191-
p_event->command_name = estrdup(mongoc_apm_command_succeeded_get_command_name(event));
192-
p_event->server_id = mongoc_apm_command_succeeded_get_server_id(event);
193-
p_event->operation_id = mongoc_apm_command_succeeded_get_operation_id(event);
194-
p_event->request_id = mongoc_apm_command_succeeded_get_request_id(event);
195-
p_event->duration_micros = mongoc_apm_command_succeeded_get_duration(event);
196-
p_event->reply = bson_copy(mongoc_apm_command_succeeded_get_reply(event));
197-
p_event->has_service_id = mongoc_apm_command_succeeded_get_service_id(event) != NULL;
192+
p_event->command_name = estrdup(mongoc_apm_command_succeeded_get_command_name(event));
193+
p_event->server_id = mongoc_apm_command_succeeded_get_server_id(event);
194+
p_event->operation_id = mongoc_apm_command_succeeded_get_operation_id(event);
195+
p_event->request_id = mongoc_apm_command_succeeded_get_request_id(event);
196+
p_event->duration_micros = mongoc_apm_command_succeeded_get_duration(event);
197+
p_event->reply = bson_copy(mongoc_apm_command_succeeded_get_reply(event));
198+
p_event->server_connection_id = mongoc_apm_command_succeeded_get_server_connection_id(event);
199+
p_event->has_service_id = mongoc_apm_command_succeeded_get_service_id(event) != NULL;
198200

199201
if (p_event->has_service_id) {
200202
bson_oid_copy(mongoc_apm_command_succeeded_get_service_id(event), &p_event->service_id);
@@ -234,13 +236,14 @@ static void phongo_apm_command_failed(const mongoc_apm_command_failed_t* event)
234236
object_init_ex(&z_event, php_phongo_commandfailedevent_ce);
235237
p_event = Z_COMMANDFAILEDEVENT_OBJ_P(&z_event);
236238

237-
p_event->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event));
238-
p_event->server_id = mongoc_apm_command_failed_get_server_id(event);
239-
p_event->operation_id = mongoc_apm_command_failed_get_operation_id(event);
240-
p_event->request_id = mongoc_apm_command_failed_get_request_id(event);
241-
p_event->duration_micros = mongoc_apm_command_failed_get_duration(event);
242-
p_event->reply = bson_copy(mongoc_apm_command_failed_get_reply(event));
243-
p_event->has_service_id = mongoc_apm_command_failed_get_service_id(event) != NULL;
239+
p_event->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event));
240+
p_event->server_id = mongoc_apm_command_failed_get_server_id(event);
241+
p_event->operation_id = mongoc_apm_command_failed_get_operation_id(event);
242+
p_event->request_id = mongoc_apm_command_failed_get_request_id(event);
243+
p_event->duration_micros = mongoc_apm_command_failed_get_duration(event);
244+
p_event->reply = bson_copy(mongoc_apm_command_failed_get_reply(event));
245+
p_event->server_connection_id = mongoc_apm_command_failed_get_server_connection_id(event);
246+
p_event->has_service_id = mongoc_apm_command_failed_get_service_id(event) != NULL;
244247

245248
if (p_event->has_service_id) {
246249
bson_oid_copy(mongoc_apm_command_failed_get_service_id(event), &p_event->service_id);

src/phongo_structs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ typedef struct {
265265
zval z_error;
266266
bool has_service_id;
267267
bson_oid_t service_id;
268+
int32_t server_connection_id;
268269
zend_object std;
269270
} php_phongo_commandfailedevent_t;
270271

@@ -278,6 +279,7 @@ typedef struct {
278279
char* database_name;
279280
bool has_service_id;
280281
bson_oid_t service_id;
282+
int32_t server_connection_id;
281283
zend_object std;
282284
} php_phongo_commandstartedevent_t;
283285

@@ -291,6 +293,7 @@ typedef struct {
291293
bson_t* reply;
292294
bool has_service_id;
293295
bson_oid_t service_id;
296+
int32_t server_connection_id;
294297
zend_object std;
295298
} php_phongo_commandsucceededevent_t;
296299

tests/apm/commandFailedEvent-debug-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ object(MongoDB\Driver\Monitoring\CommandFailedEvent)#%d (%d) {
6161
}
6262
["serviceId"]=>
6363
%r(NULL|object\(MongoDB\\BSON\\ObjectId\).*)%r
64+
["serverConnectionId"]=>
65+
%r(NULL|int\(\d+\))%r
6466
}
6567
OK: Got MongoDB\Driver\Exception\CommandException
6668
===DONE===
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
MongoDB\Driver\Monitoring\CommandFailedEvent includes serverConnectionId for 4.2+ server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '4.2'); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
class MySubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber
12+
{
13+
private $commandStartedServerConnectionId;
14+
15+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
16+
{
17+
printf("commandStarted: %s\n", $event->getCommandName());
18+
19+
$this->commandStartedServerConnectionId = $event->getServerConnectionId();
20+
var_dump($this->commandStartedServerConnectionId);
21+
}
22+
23+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
24+
{
25+
}
26+
27+
public function commandFailed( \MongoDB\Driver\Monitoring\CommandFailedEvent $event )
28+
{
29+
printf("commandFailed: %s\n", $event->getCommandName());
30+
printf("same serverConnectionId as last commandStarted: %s\n", $event->getServerConnectionId() == $this->commandStartedServerConnectionId ? 'yes' : 'no');
31+
var_dump($event->getServerConnectionId());
32+
}
33+
}
34+
35+
$manager = create_test_manager();
36+
$manager->addSubscriber(new MySubscriber);
37+
38+
$command = new MongoDB\Driver\Command([
39+
'aggregate' => COLLECTION_NAME,
40+
'pipeline' => [['$unsupported' => 1]],
41+
]);
42+
43+
throws(function() use ($manager, $command) {
44+
$manager->executeCommand(DATABASE_NAME, $command);
45+
}, MongoDB\Driver\Exception\CommandException::class);
46+
47+
?>
48+
--EXPECTF--
49+
commandStarted: aggregate
50+
int(%d)
51+
commandFailed: aggregate
52+
same serverConnectionId as last commandStarted: yes
53+
int(%d)
54+
OK: Got MongoDB\Driver\Exception\CommandException
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
MongoDB\Driver\Monitoring\CommandFailedEvent omits serverConnectionId for pre-4.2 server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('>=', '4.2'); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
class MySubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber
12+
{
13+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
14+
{
15+
printf("commandStarted: %s\n", $event->getCommandName());
16+
var_dump($event->getServerConnectionId());
17+
}
18+
19+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
20+
{
21+
}
22+
23+
public function commandFailed( \MongoDB\Driver\Monitoring\CommandFailedEvent $event )
24+
{
25+
printf("commandFailed: %s\n", $event->getCommandName());
26+
var_dump($event->getServerConnectionId());
27+
}
28+
}
29+
30+
$manager = create_test_manager();
31+
$manager->addSubscriber(new MySubscriber);
32+
33+
$command = new MongoDB\Driver\Command([
34+
'aggregate' => COLLECTION_NAME,
35+
'pipeline' => [['$unsupported' => 1]],
36+
]);
37+
38+
throws(function() use ($manager, $command) {
39+
$manager->executeCommand(DATABASE_NAME, $command);
40+
}, MongoDB\Driver\Exception\CommandException::class);
41+
42+
?>
43+
--EXPECTF--
44+
commandStarted: aggregate
45+
NULL
46+
commandFailed: aggregate
47+
NULL
48+
OK: Got MongoDB\Driver\Exception\CommandException

0 commit comments

Comments
 (0)