Skip to content

feat(NODE-5055): Add databaseName property to command monitoring events #4586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cmap/command_monitoring_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class CommandSucceededEvent {
commandName: string;
reply: unknown;
serviceId?: ObjectId;
databaseName: string;
/** @internal */
name = COMMAND_SUCCEEDED;

Expand Down Expand Up @@ -127,6 +128,7 @@ export class CommandSucceededEvent {
this.duration = calculateDurationInMs(started);
this.reply = maybeRedact(commandName, cmd, extractReply(reply));
this.serverConnectionId = serverConnectionId;
this.databaseName = command.databaseName;
}

/* @internal */
Expand Down Expand Up @@ -154,6 +156,7 @@ export class CommandFailedEvent {
commandName: string;
failure: Error;
serviceId?: ObjectId;
databaseName: string;
/** @internal */
name = COMMAND_FAILED;

Expand Down Expand Up @@ -186,6 +189,7 @@ export class CommandFailedEvent {
this.duration = calculateDurationInMs(started);
this.failure = maybeRedact(commandName, cmd, error) as Error;
this.serverConnectionId = serverConnectionId;
this.databaseName = command.databaseName;
}

/* @internal */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"description": "expectedCommandEvent-commandFailedEvent-databaseName-type",
"schemaVersion": "1.15",
"createEntities": [
{
"client": {
"id": "client0"
}
}
],
"tests": [
{
"description": "foo",
"operations": [],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandFailedEvent": {
"databaseName": 0
}
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
description: "expectedCommandEvent-commandFailedEvent-databaseName-type"

schemaVersion: "1.15"

createEntities:
- client:
id: &client0 "client0"

tests:
- description: "foo"
operations: []
expectEvents:
- client: *client0
events:
- commandFailedEvent:
databaseName: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"description": "expectedCommandEvent-commandSucceededEvent-databaseName-type",
"schemaVersion": "1.15",
"createEntities": [
{
"client": {
"id": "client0"
}
}
],
"tests": [
{
"description": "foo",
"operations": [],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandSucceededEvent": {
"databaseName": 0
}
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
description: "expectedCommandEvent-commandSucceededEvent-databaseName-type"

schemaVersion: "1.15"

createEntities:
- client:
id: &client0 "client0"

tests:
- description: "foo"
operations: []
expectEvents:
- client: *client0
events:
- commandSucceededEvent:
databaseName: 0
38 changes: 27 additions & 11 deletions test/tools/unified-spec-runner/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,37 +478,53 @@ function compareCommandStartedEvents(

function compareCommandSucceededEvents(
actual: CommandSucceededEvent,
expected: ExpectedCommandEvent['commandSucceededEvent'],
expected: NonNullable<ExpectedCommandEvent['commandSucceededEvent']>,
entities: EntitiesMap,
prefix: string
) {
if (expected!.reply) {
resultCheck(actual.reply as Document, expected!.reply, entities, [prefix]);
if (expected.reply) {
resultCheck(actual.reply as Document, expected.reply, entities, [prefix]);
}
if (expected!.commandName) {
if (expected.commandName) {
expect(
expected!.commandName,
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
expected.commandName,
`expected ${prefix}.commandName to equal ${expected.commandName} but received ${
actual.commandName
}`
).to.equal(actual.commandName);
}
if (expected.databaseName) {
expect(
expected.databaseName,
`expected ${prefix}.databaseName to equal ${expected.databaseName} but received ${
actual.databaseName
}`
).to.equal(actual.databaseName);
}
}

function compareCommandFailedEvents(
actual: CommandFailedEvent,
expected: ExpectedCommandEvent['commandFailedEvent'],
entities: EntitiesMap,
expected: NonNullable<ExpectedCommandEvent['commandFailedEvent']>,
_entities: EntitiesMap,
prefix: string
) {
if (expected!.commandName) {
if (expected.commandName) {
expect(
expected!.commandName,
`expected ${prefix}.commandName to equal ${expected!.commandName} but received ${
expected.commandName,
`expected ${prefix}.commandName to equal ${expected.commandName} but received ${
actual.commandName
}`
).to.equal(actual.commandName);
}
if (expected.databaseName) {
expect(
expected.databaseName,
`expected ${prefix}.databaseName to equal ${expected.databaseName} but received ${
actual.databaseName
}`
).to.equal(actual.databaseName);
}
}

function expectInstanceOf<T extends new (...args: any[]) => any>(
Expand Down
2 changes: 2 additions & 0 deletions test/tools/unified-spec-runner/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,12 @@ export interface ExpectedCommandEvent {
commandSucceededEvent?: {
reply?: Document;
commandName?: string;
databaseName?: string;
hasServerConnectionId?: boolean;
};
commandFailedEvent?: {
commandName?: string;
databaseName?: string;
hasServerConnectionId?: boolean;
};
}
Expand Down