Skip to content

refactor(NODE-5170): update UnifiedMongoClient to make use of MongoDBLogWritable interface #3634

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
Apr 19, 2023
Merged
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
41 changes: 16 additions & 25 deletions test/tools/unified-spec-runner/entities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { expect } from 'chai';
import { EventEmitter } from 'events';
import { Writable } from 'stream';

import {
AbstractCursor,
Expand All @@ -26,6 +25,7 @@ import {
Document,
GridFSBucket,
HostAddress,
Log,
MongoClient,
MongoCredentials,
ReadConcern,
Expand Down Expand Up @@ -107,33 +107,12 @@ function getClient(address) {
return new MongoClient(`mongodb://${address}`, getEnvironmentalOptions());
}

// TODO(NODE-4813): Remove this class in favour of a simple object with a write method
/* TODO(NODE-4813): Ensure that the object that we replace this with has logic to convert the
* collected log into the format require by the unified spec runner
* (see ExpectedLogMessage type in schema.ts) */
export class UnifiedLogCollector extends Writable {
collectedLogs: LogMessage[] = [];

constructor() {
super({ objectMode: true });
}

_write(
log: LogMessage,
_: string,
callback: (e: Error | null, l: LogMessage | undefined) => void
) {
this.collectedLogs.push(log);
callback(null, log);
}
}

export class UnifiedMongoClient extends MongoClient {
commandEvents: CommandEvent[] = [];
cmapEvents: CmapEvent[] = [];
sdamEvents: SdamEvent[] = [];
failPoints: Document[] = [];
logCollector: UnifiedLogCollector;
logCollector: { buffer: LogMessage[]; write: (log: Log) => void };

ignoredEvents: string[];
observedCommandEvents: ('commandStarted' | 'commandSucceeded' | 'commandFailed')[];
Expand Down Expand Up @@ -186,7 +165,18 @@ export class UnifiedMongoClient extends MongoClient {
} as const;

constructor(uri: string, description: ClientEntity) {
const logCollector = new UnifiedLogCollector();
const logCollector: { buffer: LogMessage[]; write: (log: Log) => void } = {
buffer: [],
write(log: Log): void {
const transformedLog = {
level: log.s,
component: log.c,
data: { ...log, s: undefined, c: undefined }
};

this.buffer.push(transformedLog);
}
};
const componentSeverities = {
MONGODB_LOG_ALL: 'off'
};
Expand All @@ -202,6 +192,7 @@ export class UnifiedMongoClient extends MongoClient {
[Symbol.for('@@mdb.skipPingOnConnect')]: true,
[Symbol.for('@@mdb.enableMongoLogger')]: true,
[Symbol.for('@@mdb.internalMongoLoggerConfig')]: componentSeverities,
// @ts-expect-error TODO(NODE-4849): Remove this once we have support for mongodbLogPath
mongodbLogPath: logCollector,
...getEnvironmentalOptions(),
...(description.serverApi ? { serverApi: description.serverApi } : {})
Expand Down Expand Up @@ -296,7 +287,7 @@ export class UnifiedMongoClient extends MongoClient {
}

get collectedLogs(): LogMessage[] {
return this.logCollector.collectedLogs;
return this.logCollector.buffer;
}
}

Expand Down