Skip to content

Commit 757e84e

Browse files
authored
CSHARP-4520: Add databaseName property to Command Events (#1132)
1 parent 6b74cd9 commit 757e84e

File tree

12 files changed

+97
-24
lines changed

12 files changed

+97
-24
lines changed

specifications/command-logging-and-monitoring/tests/logging/command.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"component": "command",
9494
"data": {
9595
"message": "Command succeeded",
96+
"databaseName": "logging-tests",
9697
"commandName": "ping",
9798
"reply": {
9899
"$$type": "string"
@@ -177,6 +178,7 @@
177178
"component": "command",
178179
"data": {
179180
"message": "Command failed",
181+
"databaseName": "logging-tests",
180182
"commandName": "find",
181183
"failure": {
182184
"$$exists": true

specifications/command-logging-and-monitoring/tests/logging/command.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ tests:
5252
component: command
5353
data:
5454
message: "Command succeeded"
55+
databaseName: *databaseName
5556
commandName: *commandName
5657
reply: { $$type: string }
5758
requestId: { $$type: [int, long] }
5859
serverHost: { $$type: string }
5960
serverPort: { $$type: [int, long] }
6061
durationMS: { $$type: [double, int, long] }
61-
62+
6263
- description: "A failed command"
6364
operations:
6465
- name: &commandName find
@@ -85,10 +86,10 @@ tests:
8586
component: command
8687
data:
8788
message: "Command failed"
89+
databaseName: *databaseName
8890
commandName: *commandName
8991
failure: { $$exists: true }
9092
requestId: { $$type: [int, long] }
9193
serverHost: { $$type: string }
9294
serverPort: { $$type: [int, long] }
9395
durationMS: { $$type: [double, int, long] }
94-

specifications/command-logging-and-monitoring/tests/monitoring/find.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "find",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.15",
44
"createEntities": [
55
{
66
"client": {
@@ -103,7 +103,8 @@
103103
]
104104
}
105105
},
106-
"commandName": "find"
106+
"commandName": "find",
107+
"databaseName": "command-monitoring-tests"
107108
}
108109
}
109110
]
@@ -198,7 +199,8 @@
198199
]
199200
}
200201
},
201-
"commandName": "find"
202+
"commandName": "find",
203+
"databaseName": "command-monitoring-tests"
202204
}
203205
}
204206
]
@@ -262,7 +264,8 @@
262264
]
263265
}
264266
},
265-
"commandName": "find"
267+
"commandName": "find",
268+
"databaseName": "command-monitoring-tests"
266269
}
267270
}
268271
]
@@ -338,7 +341,8 @@
338341
]
339342
}
340343
},
341-
"commandName": "find"
344+
"commandName": "find",
345+
"databaseName": "command-monitoring-tests"
342346
}
343347
},
344348
{
@@ -376,7 +380,8 @@
376380
]
377381
}
378382
},
379-
"commandName": "getMore"
383+
"commandName": "getMore",
384+
"databaseName": "command-monitoring-tests"
380385
}
381386
}
382387
]
@@ -464,7 +469,8 @@
464469
]
465470
}
466471
},
467-
"commandName": "find"
472+
"commandName": "find",
473+
"databaseName": "command-monitoring-tests"
468474
}
469475
},
470476
{
@@ -498,7 +504,8 @@
498504
]
499505
}
500506
},
501-
"commandName": "getMore"
507+
"commandName": "getMore",
508+
"databaseName": "command-monitoring-tests"
502509
}
503510
}
504511
]
@@ -539,7 +546,8 @@
539546
},
540547
{
541548
"commandFailedEvent": {
542-
"commandName": "find"
549+
"commandName": "find",
550+
"databaseName": "command-monitoring-tests"
543551
}
544552
}
545553
]

specifications/command-logging-and-monitoring/tests/monitoring/find.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
description: "find"
22

3-
schemaVersion: "1.1"
3+
schemaVersion: "1.15"
44

55
createEntities:
66
- client:
@@ -56,6 +56,7 @@ tests:
5656
firstBatch:
5757
- { _id: 1, x: 11 }
5858
commandName: find
59+
databaseName: *databaseName
5960

6061
- description: "A successful find with options"
6162
operations:
@@ -98,6 +99,7 @@ tests:
9899
- { x: 33 }
99100
- { x: 22 }
100101
commandName: find
102+
databaseName: *databaseName
101103

102104
- description: "A successful find with showRecordId and returnKey"
103105
operations:
@@ -131,6 +133,7 @@ tests:
131133
- { _id: 4 }
132134
- { _id: 5 }
133135
commandName: find
136+
databaseName: *databaseName
134137

135138
- description: "A successful find with a getMore"
136139
operations:
@@ -162,6 +165,7 @@ tests:
162165
- { _id: 2, x: 22 }
163166
- { _id: 3, x: 33 }
164167
commandName: find
168+
databaseName: *databaseName
165169
- commandStartedEvent:
166170
command:
167171
getMore: { $$type: [ int, long ] }
@@ -179,6 +183,7 @@ tests:
179183
- { _id: 4, x: 44 }
180184
- { _id: 5, x: 55 }
181185
commandName: getMore
186+
databaseName: *databaseName
182187

183188
- description: "A successful find event with a getmore and the server kills the cursor (<= 4.4)"
184189
runOnRequirements:
@@ -216,6 +221,7 @@ tests:
216221
- { _id: 2, x: 22 }
217222
- { _id: 3, x: 33 }
218223
commandName: find
224+
databaseName: *databaseName
219225
- commandStartedEvent:
220226
command:
221227
getMore: { $$type: [ int, long ] }
@@ -232,6 +238,7 @@ tests:
232238
nextBatch:
233239
- { _id: 4, x: 44 }
234240
commandName: getMore
241+
databaseName: *databaseName
235242

236243
- description: "A failed find event"
237244
operations:
@@ -252,3 +259,4 @@ tests:
252259
databaseName: *databaseName
253260
- commandFailedEvent:
254261
commandName: find
262+
databaseName: *databaseName

src/MongoDB.Driver.Core/Core/Connections/CommandEventHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public void AfterSending(IEnumerable<RequestMessage> messages, ConnectionId conn
117117
_eventLogger.LogAndPublish(new CommandSucceededEvent(
118118
state.CommandName,
119119
new BsonDocument("ok", 1),
120+
state.QueryNamespace.DatabaseNamespace,
120121
state.OperationId,
121122
message.RequestId,
122123
connectionId,
@@ -139,6 +140,7 @@ public void ErrorSending(IEnumerable<RequestMessage> messages, ConnectionId conn
139140
state.Stopwatch.Stop();
140141
_eventLogger.LogAndPublish(new CommandFailedEvent(
141142
state.CommandName,
143+
state.QueryNamespace.DatabaseNamespace,
142144
exception,
143145
state.OperationId,
144146
message.RequestId,
@@ -182,6 +184,7 @@ public void ErrorReceiving(int responseTo, ConnectionId connectionId, ObjectId?
182184

183185
_eventLogger.LogAndPublish(new CommandFailedEvent(
184186
state.CommandName,
187+
state.QueryNamespace.DatabaseNamespace,
185188
exception,
186189
state.OperationId,
187190
responseTo,
@@ -207,6 +210,7 @@ public void ConnectionFailed(ConnectionId connectionId, ObjectId? serviceId, Exc
207210
state.Stopwatch.Stop();
208211
_eventLogger.LogAndPublish(new CommandFailedEvent(
209212
state.CommandName,
213+
state.QueryNamespace.DatabaseNamespace,
210214
exception,
211215
state.OperationId,
212216
requestId,
@@ -313,6 +317,7 @@ private void ProcessCommandResponseMessage(CommandState state, CommandResponseMe
313317
_eventLogger.LogAndPublish(new CommandSucceededEvent(
314318
state.CommandName,
315319
reply,
320+
state.QueryNamespace.DatabaseNamespace,
316321
state.OperationId,
317322
message.ResponseTo,
318323
connectionId,
@@ -326,6 +331,7 @@ private void ProcessCommandResponseMessage(CommandState state, CommandResponseMe
326331
{
327332
_eventLogger.LogAndPublish(new CommandFailedEvent(
328333
state.CommandName,
334+
state.QueryNamespace.DatabaseNamespace,
329335
new MongoCommandException(
330336
connectionId,
331337
string.Format("{0} command failed", state.CommandName),
@@ -446,6 +452,7 @@ private void ProcessReplyMessage(CommandState state, ResponseMessage message, IB
446452
{
447453
_eventLogger.LogAndPublish(new CommandFailedEvent(
448454
state.CommandName,
455+
state.QueryNamespace.DatabaseNamespace,
449456
new MongoCommandException(
450457
connectionId,
451458
string.Format("{0} command failed", state.CommandName),
@@ -502,6 +509,7 @@ private void ProcessCommandReplyMessage(CommandState state, ReplyMessage<RawBson
502509
{
503510
_eventLogger.LogAndPublish(new CommandFailedEvent(
504511
state.CommandName,
512+
state.QueryNamespace.DatabaseNamespace,
505513
new MongoCommandException(
506514
connectionId,
507515
string.Format("{0} command failed", state.CommandName),
@@ -519,6 +527,7 @@ private void ProcessCommandReplyMessage(CommandState state, ReplyMessage<RawBson
519527
_eventLogger.LogAndPublish(new CommandSucceededEvent(
520528
state.CommandName,
521529
reply,
530+
state.QueryNamespace.DatabaseNamespace,
522531
state.OperationId,
523532
replyMessage.ResponseTo,
524533
connectionId,
@@ -555,6 +564,7 @@ private void ProcessQueryReplyMessage(CommandState state, ReplyMessage<RawBsonDo
555564
_eventLogger.LogAndPublish(new CommandSucceededEvent(
556565
state.CommandName,
557566
reply,
567+
state.QueryNamespace.DatabaseNamespace,
558568
state.OperationId,
559569
replyMessage.ResponseTo,
560570
connectionId,

src/MongoDB.Driver.Core/Core/Events/CommandFailedEvent.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct CommandFailedEvent : IEvent
2727
{
2828
private readonly string _commandName;
2929
private readonly ConnectionId _connectionId;
30+
private readonly DatabaseNamespace _databaseNamespace;
3031
private readonly TimeSpan _duration;
3132
private readonly Exception _exception;
3233
private readonly long? _operationId;
@@ -38,31 +39,34 @@ public struct CommandFailedEvent : IEvent
3839
/// Initializes a new instance of the <see cref="CommandFailedEvent" /> struct.
3940
/// </summary>
4041
/// <param name="commandName">Name of the command.</param>
42+
/// <param name="databaseNamespace">The database namespace.</param>
4143
/// <param name="exception">The exception.</param>
4244
/// <param name="operationId">The operation identifier.</param>
4345
/// <param name="requestId">The request identifier.</param>
4446
/// <param name="connectionId">The connection identifier.</param>
4547
/// <param name="duration">The duration.</param>
46-
public CommandFailedEvent(string commandName, Exception exception, long? operationId, int requestId, ConnectionId connectionId, TimeSpan duration)
47-
: this(commandName, exception, operationId, requestId, connectionId, serviceId: null, duration)
48+
public CommandFailedEvent(string commandName, DatabaseNamespace databaseNamespace, Exception exception, long? operationId, int requestId, ConnectionId connectionId, TimeSpan duration)
49+
: this(commandName, databaseNamespace, exception, operationId, requestId, connectionId, serviceId: null, duration)
4850
{
4951
}
5052

5153
/// <summary>
5254
/// Initializes a new instance of the <see cref="CommandFailedEvent" /> struct.
5355
/// </summary>
5456
/// <param name="commandName">Name of the command.</param>
57+
/// <param name="databaseNamespace">The database namespace.</param>
5558
/// <param name="exception">The exception.</param>
5659
/// <param name="operationId">The operation identifier.</param>
5760
/// <param name="requestId">The request identifier.</param>
5861
/// <param name="connectionId">The connection identifier.</param>
5962
/// <param name="serviceId">The service identifier.</param>
6063
/// <param name="duration">The duration.</param>
61-
public CommandFailedEvent(string commandName, Exception exception, long? operationId, int requestId, ConnectionId connectionId, ObjectId? serviceId, TimeSpan duration)
64+
public CommandFailedEvent(string commandName, DatabaseNamespace databaseNamespace, Exception exception, long? operationId, int requestId, ConnectionId connectionId, ObjectId? serviceId, TimeSpan duration)
6265
{
6366
_commandName = Ensure.IsNotNullOrEmpty(commandName, "commandName");
6467
_exception = Ensure.IsNotNull(exception, "exception");
6568
_connectionId = Ensure.IsNotNull(connectionId, "connectionId");
69+
_databaseNamespace = Ensure.IsNotNull(databaseNamespace, "databaseNamespace");
6670
_operationId = operationId;
6771
_requestId = requestId;
6872
_duration = duration;
@@ -86,6 +90,14 @@ public ConnectionId ConnectionId
8690
get { return _connectionId; }
8791
}
8892

93+
/// <summary>
94+
/// Gets the database namespace.
95+
/// </summary>
96+
public DatabaseNamespace DatabaseNamespace
97+
{
98+
get { return _databaseNamespace; }
99+
}
100+
89101
/// <summary>
90102
/// Gets the duration.
91103
/// </summary>

0 commit comments

Comments
 (0)