Skip to content

Commit 460de49

Browse files
CDRIVER-4572 Log database name in command_failed events (#1530)
Log database name in APM events and sync spec tests --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 7cbea30 commit 460de49

13 files changed

+683
-9
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
:man_page: mongoc_apm_command_failed_get_database_name
2+
3+
mongoc_apm_command_failed_get_database_name()
4+
=============================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
const char *
12+
mongoc_apm_command_failed_get_database_name (
13+
const mongoc_apm_command_failed_t *event);
14+
15+
Returns this event's database name. The data is only valid in the scope of the callback that receives this event; copy it if it will be accessed after the callback returns.
16+
17+
Parameters
18+
----------
19+
20+
* ``event``: A :symbol:`mongoc_apm_command_failed_t`.
21+
22+
Returns
23+
-------
24+
25+
A string that should not be modified or freed.
26+
27+
.. seealso::
28+
29+
| :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`
30+

src/libmongoc/doc/mongoc_apm_command_failed_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ An event notification sent when the driver fails to execute a MongoDB command.
2020
:maxdepth: 1
2121

2222
mongoc_apm_command_failed_get_command_name
23+
mongoc_apm_command_failed_get_database_name
2324
mongoc_apm_command_failed_get_context
2425
mongoc_apm_command_failed_get_duration
2526
mongoc_apm_command_failed_get_error
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
:man_page: mongoc_apm_command_succeeded_get_database_name
2+
3+
mongoc_apm_command_succeeded_get_database_name()
4+
================================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
const char *
12+
mongoc_apm_command_succeeded_get_database_name (
13+
const mongoc_apm_command_succeeded_t *event);
14+
15+
Returns this event's database name. The data is only valid in the scope of the callback that receives this event; copy it if it will be accessed after the callback returns.
16+
17+
Parameters
18+
----------
19+
20+
* ``event``: A :symbol:`mongoc_apm_command_succeeded_t`.
21+
22+
Returns
23+
-------
24+
25+
A string that should not be modified or freed.
26+
27+
.. seealso::
28+
29+
| :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`
30+

src/libmongoc/doc/mongoc_apm_command_succeeded_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ An event notification sent when the driver successfully executes a MongoDB comma
2424
:maxdepth: 1
2525

2626
mongoc_apm_command_succeeded_get_command_name
27+
mongoc_apm_command_succeeded_get_database_name
2728
mongoc_apm_command_succeeded_get_context
2829
mongoc_apm_command_succeeded_get_duration
2930
mongoc_apm_command_succeeded_get_host

src/libmongoc/src/mongoc/mongoc-apm-private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct _mongoc_apm_command_succeeded_t {
6565
bson_t *reply;
6666
bool reply_owned;
6767
const char *command_name;
68+
const char *database_name;
6869
int64_t request_id;
6970
int64_t operation_id;
7071
const mongoc_host_list_t *host;
@@ -77,6 +78,7 @@ struct _mongoc_apm_command_succeeded_t {
7778
struct _mongoc_apm_command_failed_t {
7879
int64_t duration;
7980
const char *command_name;
81+
const char *database_name;
8082
const bson_error_t *error;
8183
bson_t *reply;
8284
bool reply_owned;
@@ -181,6 +183,7 @@ mongoc_apm_command_succeeded_init (mongoc_apm_command_succeeded_t *event,
181183
int64_t duration,
182184
const bson_t *reply,
183185
const char *command_name,
186+
const char *database_name,
184187
int64_t request_id,
185188
int64_t operation_id,
186189
const mongoc_host_list_t *host,
@@ -197,6 +200,7 @@ void
197200
mongoc_apm_command_failed_init (mongoc_apm_command_failed_t *event,
198201
int64_t duration,
199202
const char *command_name,
203+
const char *database_name,
200204
const bson_error_t *error,
201205
const bson_t *reply,
202206
int64_t request_id,

src/libmongoc/src/mongoc/mongoc-apm.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ mongoc_apm_command_succeeded_init (mongoc_apm_command_succeeded_t *event,
200200
int64_t duration,
201201
const bson_t *reply,
202202
const char *command_name,
203+
const char *database_name,
203204
int64_t request_id,
204205
int64_t operation_id,
205206
const mongoc_host_list_t *host,
@@ -224,6 +225,7 @@ mongoc_apm_command_succeeded_init (mongoc_apm_command_succeeded_t *event,
224225

225226
event->duration = duration;
226227
event->command_name = command_name;
228+
event->database_name = database_name;
227229
event->request_id = request_id;
228230
event->operation_id = operation_id;
229231
event->host = host;
@@ -260,6 +262,7 @@ void
260262
mongoc_apm_command_failed_init (mongoc_apm_command_failed_t *event,
261263
int64_t duration,
262264
const char *command_name,
265+
const char *database_name,
263266
const bson_error_t *error,
264267
const bson_t *reply,
265268
int64_t request_id,
@@ -286,6 +289,7 @@ mongoc_apm_command_failed_init (mongoc_apm_command_failed_t *event,
286289

287290
event->duration = duration;
288291
event->command_name = command_name;
292+
event->database_name = database_name;
289293
event->error = error;
290294
event->request_id = request_id;
291295
event->operation_id = operation_id;
@@ -424,6 +428,11 @@ mongoc_apm_command_succeeded_get_command_name (const mongoc_apm_command_succeede
424428
return event->command_name;
425429
}
426430

431+
const char *
432+
mongoc_apm_command_succeeded_get_database_name (const mongoc_apm_command_succeeded_t *event)
433+
{
434+
return event->database_name;
435+
}
427436

428437
int64_t
429438
mongoc_apm_command_succeeded_get_request_id (const mongoc_apm_command_succeeded_t *event)
@@ -589,6 +598,13 @@ mongoc_apm_command_failed_get_context (const mongoc_apm_command_failed_t *event)
589598
}
590599

591600

601+
const char *
602+
mongoc_apm_command_failed_get_database_name (const mongoc_apm_command_failed_t *event)
603+
{
604+
return event->database_name;
605+
}
606+
607+
592608
/* server-changed event fields */
593609

594610
const mongoc_host_list_t *

src/libmongoc/src/mongoc/mongoc-apm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ MONGOC_EXPORT (const bson_t *)
108108
mongoc_apm_command_succeeded_get_reply (const mongoc_apm_command_succeeded_t *event);
109109
MONGOC_EXPORT (const char *)
110110
mongoc_apm_command_succeeded_get_command_name (const mongoc_apm_command_succeeded_t *event);
111+
MONGOC_EXPORT (const char *)
112+
mongoc_apm_command_succeeded_get_database_name (const mongoc_apm_command_succeeded_t *event);
111113
MONGOC_EXPORT (int64_t)
112114
mongoc_apm_command_succeeded_get_request_id (const mongoc_apm_command_succeeded_t *event);
113115
MONGOC_EXPORT (int64_t)
@@ -132,6 +134,8 @@ MONGOC_EXPORT (int64_t)
132134
mongoc_apm_command_failed_get_duration (const mongoc_apm_command_failed_t *event);
133135
MONGOC_EXPORT (const char *)
134136
mongoc_apm_command_failed_get_command_name (const mongoc_apm_command_failed_t *event);
137+
MONGOC_EXPORT (const char *)
138+
mongoc_apm_command_failed_get_database_name (const mongoc_apm_command_failed_t *event);
135139
/* retrieve the error by filling out the passed-in "error" struct */
136140
MONGOC_EXPORT (void)
137141
mongoc_apm_command_failed_get_error (const mongoc_apm_command_failed_t *event, bson_error_t *error);

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,8 @@ _mongoc_client_monitor_op_killcursors_succeeded (mongoc_cluster_t *cluster,
21362136
int64_t duration,
21372137
mongoc_server_stream_t *server_stream,
21382138
int64_t cursor_id,
2139-
int64_t operation_id)
2139+
int64_t operation_id,
2140+
const char *db)
21402141
{
21412142
mongoc_client_t *client;
21422143
bson_t doc;
@@ -2162,6 +2163,7 @@ _mongoc_client_monitor_op_killcursors_succeeded (mongoc_cluster_t *cluster,
21622163
duration,
21632164
&doc,
21642165
"killCursors",
2166+
db,
21652167
cluster->request_id,
21662168
operation_id,
21672169
&server_stream->sd->host,
@@ -2183,7 +2185,8 @@ _mongoc_client_monitor_op_killcursors_failed (mongoc_cluster_t *cluster,
21832185
int64_t duration,
21842186
mongoc_server_stream_t *server_stream,
21852187
const bson_error_t *error,
2186-
int64_t operation_id)
2188+
int64_t operation_id,
2189+
const char *db)
21872190
{
21882191
mongoc_client_t *client;
21892192
bson_t doc;
@@ -2204,6 +2207,7 @@ _mongoc_client_monitor_op_killcursors_failed (mongoc_cluster_t *cluster,
22042207
mongoc_apm_command_failed_init (&event,
22052208
duration,
22062209
"killCursors",
2210+
db,
22072211
error,
22082212
&doc,
22092213
cluster->request_id,
@@ -2264,10 +2268,10 @@ _mongoc_client_op_killcursors (mongoc_cluster_t *cluster,
22642268
if (has_ns) {
22652269
if (res) {
22662270
_mongoc_client_monitor_op_killcursors_succeeded (
2267-
cluster, bson_get_monotonic_time () - started, server_stream, cursor_id, operation_id);
2271+
cluster, bson_get_monotonic_time () - started, server_stream, cursor_id, operation_id, db);
22682272
} else {
22692273
_mongoc_client_monitor_op_killcursors_failed (
2270-
cluster, bson_get_monotonic_time () - started, server_stream, &error, operation_id);
2274+
cluster, bson_get_monotonic_time () - started, server_stream, &error, operation_id, db);
22712275
}
22722276
}
22732277

src/libmongoc/src/mongoc/mongoc-cluster.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ mongoc_cluster_run_command_monitored (mongoc_cluster_t *cluster, mongoc_cmd_t *c
555555
bson_get_monotonic_time () - started,
556556
cmd->is_acknowledged ? reply : &fake_reply,
557557
cmd->command_name,
558+
cmd->db_name,
558559
request_id,
559560
cmd->operation_id,
560561
&server_stream->sd->host,
@@ -572,6 +573,7 @@ mongoc_cluster_run_command_monitored (mongoc_cluster_t *cluster, mongoc_cmd_t *c
572573
mongoc_apm_command_failed_init (&failed_event,
573574
bson_get_monotonic_time () - started,
574575
cmd->command_name,
576+
cmd->db_name,
575577
error,
576578
reply,
577579
request_id,

src/libmongoc/src/mongoc/mongoc-cursor.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,15 @@ _mongoc_cursor_monitor_succeeded (mongoc_cursor_t *cursor,
725725
doc (kv ("id", int64 (mongoc_cursor_get_id (cursor))),
726726
kv ("ns", utf8_w_len (cursor->ns, cursor->nslen)),
727727
kv (first_batch ? "firstBatch" : "nextBatch", bsonArray (docs_array)))));
728+
char *db = bson_strndup (cursor->ns, cursor->dblen);
728729

729730
bson_destroy (&docs_array);
730731

731732
mongoc_apm_command_succeeded_init (&event,
732733
duration,
733734
&reply,
734735
cmd_name,
736+
db,
735737
client->cluster.request_id,
736738
cursor->operation_id,
737739
&stream->sd->host,
@@ -745,6 +747,7 @@ _mongoc_cursor_monitor_succeeded (mongoc_cursor_t *cursor,
745747

746748
mongoc_apm_command_succeeded_cleanup (&event);
747749
bson_destroy (&reply);
750+
bson_free (db);
748751

749752
EXIT;
750753
}
@@ -771,10 +774,12 @@ _mongoc_cursor_monitor_failed (mongoc_cursor_t *cursor,
771774
* {ok: 0}
772775
*/
773776
bsonBuildDecl (reply, kv ("ok", int32 (0)));
777+
char *db = bson_strndup (cursor->ns, cursor->dblen);
774778

775779
mongoc_apm_command_failed_init (&event,
776780
duration,
777781
cmd_name,
782+
db,
778783
&cursor->error,
779784
&reply,
780785
client->cluster.request_id,
@@ -790,6 +795,7 @@ _mongoc_cursor_monitor_failed (mongoc_cursor_t *cursor,
790795

791796
mongoc_apm_command_failed_cleanup (&event);
792797
bson_destroy (&reply);
798+
bson_free (db);
793799

794800
EXIT;
795801
}

0 commit comments

Comments
 (0)