Skip to content

Commit 3faf0c9

Browse files
feat(NODE-5055): Add databaseName property to command monitoring events (#4586)
1 parent ec82ae9 commit 3faf0c9

7 files changed

+123
-11
lines changed

src/cmap/command_monitoring_events.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class CommandSucceededEvent {
9696
commandName: string;
9797
reply: unknown;
9898
serviceId?: ObjectId;
99+
databaseName: string;
99100
/** @internal */
100101
name = COMMAND_SUCCEEDED;
101102

@@ -127,6 +128,7 @@ export class CommandSucceededEvent {
127128
this.duration = calculateDurationInMs(started);
128129
this.reply = maybeRedact(commandName, cmd, extractReply(reply));
129130
this.serverConnectionId = serverConnectionId;
131+
this.databaseName = command.databaseName;
130132
}
131133

132134
/* @internal */
@@ -154,6 +156,7 @@ export class CommandFailedEvent {
154156
commandName: string;
155157
failure: Error;
156158
serviceId?: ObjectId;
159+
databaseName: string;
157160
/** @internal */
158161
name = COMMAND_FAILED;
159162

@@ -186,6 +189,7 @@ export class CommandFailedEvent {
186189
this.duration = calculateDurationInMs(started);
187190
this.failure = maybeRedact(commandName, cmd, error) as Error;
188191
this.serverConnectionId = serverConnectionId;
192+
this.databaseName = command.databaseName;
189193
}
190194

191195
/* @internal */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"description": "expectedCommandEvent-commandFailedEvent-databaseName-type",
3+
"schemaVersion": "1.15",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
}
10+
],
11+
"tests": [
12+
{
13+
"description": "foo",
14+
"operations": [],
15+
"expectEvents": [
16+
{
17+
"client": "client0",
18+
"events": [
19+
{
20+
"commandFailedEvent": {
21+
"databaseName": 0
22+
}
23+
}
24+
]
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
description: "expectedCommandEvent-commandFailedEvent-databaseName-type"
2+
3+
schemaVersion: "1.15"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 "client0"
8+
9+
tests:
10+
- description: "foo"
11+
operations: []
12+
expectEvents:
13+
- client: *client0
14+
events:
15+
- commandFailedEvent:
16+
databaseName: 0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"description": "expectedCommandEvent-commandSucceededEvent-databaseName-type",
3+
"schemaVersion": "1.15",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
}
10+
],
11+
"tests": [
12+
{
13+
"description": "foo",
14+
"operations": [],
15+
"expectEvents": [
16+
{
17+
"client": "client0",
18+
"events": [
19+
{
20+
"commandSucceededEvent": {
21+
"databaseName": 0
22+
}
23+
}
24+
]
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
description: "expectedCommandEvent-commandSucceededEvent-databaseName-type"
2+
3+
schemaVersion: "1.15"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 "client0"
8+
9+
tests:
10+
- description: "foo"
11+
operations: []
12+
expectEvents:
13+
- client: *client0
14+
events:
15+
- commandSucceededEvent:
16+
databaseName: 0

test/tools/unified-spec-runner/match.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,37 +478,53 @@ function compareCommandStartedEvents(
478478

479479
function compareCommandSucceededEvents(
480480
actual: CommandSucceededEvent,
481-
expected: ExpectedCommandEvent['commandSucceededEvent'],
481+
expected: NonNullable<ExpectedCommandEvent['commandSucceededEvent']>,
482482
entities: EntitiesMap,
483483
prefix: string
484484
) {
485-
if (expected!.reply) {
486-
resultCheck(actual.reply as Document, expected!.reply, entities, [prefix]);
485+
if (expected.reply) {
486+
resultCheck(actual.reply as Document, expected.reply, entities, [prefix]);
487487
}
488-
if (expected!.commandName) {
488+
if (expected.commandName) {
489489
expect(
490-
expected!.commandName,
491-
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
490+
expected.commandName,
491+
`expected ${prefix}.commandName to equal ${expected.commandName} but received ${
492492
actual.commandName
493493
}`
494494
).to.equal(actual.commandName);
495495
}
496+
if (expected.databaseName) {
497+
expect(
498+
expected.databaseName,
499+
`expected ${prefix}.databaseName to equal ${expected.databaseName} but received ${
500+
actual.databaseName
501+
}`
502+
).to.equal(actual.databaseName);
503+
}
496504
}
497505

498506
function compareCommandFailedEvents(
499507
actual: CommandFailedEvent,
500-
expected: ExpectedCommandEvent['commandFailedEvent'],
501-
entities: EntitiesMap,
508+
expected: NonNullable<ExpectedCommandEvent['commandFailedEvent']>,
509+
_entities: EntitiesMap,
502510
prefix: string
503511
) {
504-
if (expected!.commandName) {
512+
if (expected.commandName) {
505513
expect(
506-
expected!.commandName,
507-
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
514+
expected.commandName,
515+
`expected ${prefix}.commandName to equal ${expected.commandName} but received ${
508516
actual.commandName
509517
}`
510518
).to.equal(actual.commandName);
511519
}
520+
if (expected.databaseName) {
521+
expect(
522+
expected.databaseName,
523+
`expected ${prefix}.databaseName to equal ${expected.databaseName} but received ${
524+
actual.databaseName
525+
}`
526+
).to.equal(actual.databaseName);
527+
}
512528
}
513529

514530
function expectInstanceOf<T extends new (...args: any[]) => any>(

test/tools/unified-spec-runner/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,12 @@ export interface ExpectedCommandEvent {
312312
commandSucceededEvent?: {
313313
reply?: Document;
314314
commandName?: string;
315+
databaseName?: string;
315316
hasServerConnectionId?: boolean;
316317
};
317318
commandFailedEvent?: {
318319
commandName?: string;
320+
databaseName?: string;
319321
hasServerConnectionId?: boolean;
320322
};
321323
}

0 commit comments

Comments
 (0)