Skip to content

Commit 8910c63

Browse files
add logging
1 parent 7800067 commit 8910c63

File tree

4 files changed

+117
-7
lines changed

4 files changed

+117
-7
lines changed

.evergreen/config.in.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ functions:
107107
binary: bash
108108
args:
109109
- .evergreen/run-tests.sh
110+
- command: s3.put
111+
params:
112+
aws_key: ${aws_key}
113+
aws_secret: ${aws_secret}
114+
local_file: src/connection-logs.txt
115+
optional: true
116+
# Upload the coverage report for all tasks in a single build to the same directory.
117+
# TODO NODE-4707 - change upload directory to ${UPLOAD_BUCKET}
118+
# This change will require changing the `download and merge coverage` func as well
119+
remote_file: mongo-node-driver/${revision}/${version_id}/${build_variant}/${task_name}/connection-logs.txt
120+
bucket: mciuploads
121+
permissions: public-read
122+
content_type: text/plain
123+
display_name: "Connection Logs"
110124

111125
"run serverless tests":
112126
- command: timeout.update
@@ -127,6 +141,20 @@ functions:
127141
add_expansions_to_env: true
128142
args:
129143
- .evergreen/run-serverless-tests.sh
144+
- command: s3.put
145+
params:
146+
aws_key: ${aws_key}
147+
aws_secret: ${aws_secret}
148+
local_file: src/connection-logs.txt
149+
optional: true
150+
# Upload the coverage report for all tasks in a single build to the same directory.
151+
# TODO NODE-4707 - change upload directory to ${UPLOAD_BUCKET}
152+
# This change will require changing the `download and merge coverage` func as well
153+
remote_file: mongo-node-driver/${revision}/${version_id}/${build_variant}/${task_name}/connection-logs.txt
154+
bucket: mciuploads
155+
permissions: public-read
156+
content_type: text/plain
157+
display_name: "Connection Logs"
130158

131159
"start-load-balancer":
132160
- command: shell.exec

.evergreen/config.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ functions:
8080
binary: bash
8181
args:
8282
- .evergreen/run-tests.sh
83+
- command: s3.put
84+
params:
85+
aws_key: ${aws_key}
86+
aws_secret: ${aws_secret}
87+
local_file: src/connection-logs.txt
88+
optional: true
89+
remote_file: mongo-node-driver/${revision}/${version_id}/${build_variant}/${task_name}/connection-logs.txt
90+
bucket: mciuploads
91+
permissions: public-read
92+
content_type: text/plain
93+
display_name: Connection Logs
8394
run serverless tests:
8495
- command: timeout.update
8596
params:
@@ -99,6 +110,17 @@ functions:
99110
add_expansions_to_env: true
100111
args:
101112
- .evergreen/run-serverless-tests.sh
113+
- command: s3.put
114+
params:
115+
aws_key: ${aws_key}
116+
aws_secret: ${aws_secret}
117+
local_file: src/connection-logs.txt
118+
optional: true
119+
remote_file: mongo-node-driver/${revision}/${version_id}/${build_variant}/${task_name}/connection-logs.txt
120+
bucket: mciuploads
121+
permissions: public-read
122+
content_type: text/plain
123+
display_name: Connection Logs
102124
start-load-balancer:
103125
- command: shell.exec
104126
params:

src/cmap/connection.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createWriteStream } from 'fs';
12
import { type Readable, Transform, type TransformCallback } from 'stream';
23
import { clearTimeout, setTimeout } from 'timers';
34

@@ -6,6 +7,7 @@ import {
67
deserialize,
78
type DeserializeOptions,
89
type Document,
10+
EJSON,
911
type ObjectId
1012
} from '../bson';
1113
import { type AutoEncrypter } from '../client-side-encryption/auto_encrypter';
@@ -179,6 +181,43 @@ function streamIdentifier(stream: Stream, options: ConnectionOptions): string {
179181
return uuidV4().toString('hex');
180182
}
181183

184+
export const logger = createWriteStream('connection-logs.txt');
185+
export const write = (payload: Document) => {
186+
payload.timestamp = new Date();
187+
payload.hostname = process.env.HOSTNAME;
188+
const log = EJSON.stringify(payload);
189+
logger.write(log);
190+
logger.write('\n');
191+
};
192+
193+
const writeEvent =
194+
(event: string) =>
195+
<T extends Document>(payload: T) => {
196+
(payload as T & { event: string }).event = event;
197+
write(payload);
198+
};
199+
200+
export const writeStarted = writeEvent('commandStarted')<{
201+
requestId: number;
202+
connectionId: number | '<monitor>';
203+
}>;
204+
205+
export const readStarted = writeEvent('readStarted')<{
206+
requestId: number;
207+
connectionId: number | '<monitor>';
208+
}>;
209+
210+
export const readSucceeded = writeEvent('readSucceeded')<{
211+
requestId: number;
212+
connectionId: number | '<monitor>';
213+
}>;
214+
215+
export const readFailed = writeEvent('readFailed')<{
216+
requestId: number;
217+
connectionId: number | '<monitor>';
218+
error: Error;
219+
}>;
220+
182221
/** @internal */
183222
export class Connection extends TypedEventEmitter<ConnectionEvents> {
184223
public id: number | '<monitor>';
@@ -451,7 +490,13 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
451490
this.socketTimeoutMS;
452491
this.socket.setTimeout(timeout);
453492

493+
const payload = {
494+
connectionId: this.id,
495+
requestId: message.requestId
496+
};
454497
try {
498+
writeStarted(payload);
499+
455500
await this.writeCommand(message, {
456501
agreedCompressor: this.description.compressor ?? 'none',
457502
zlibCompressionLevel: this.description.zlibCompressionLevel,
@@ -476,16 +521,28 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
476521
);
477522
}
478523

479-
for await (const response of this.readMany(options)) {
480-
this.socket.setTimeout(0);
481-
const bson = response.parse();
524+
readStarted(payload);
525+
try {
526+
for await (const response of this.readMany(options)) {
527+
readSucceeded(payload);
528+
this.socket.setTimeout(0);
529+
const bson = response.parse();
482530

483-
const document = (responseType ?? MongoDBResponse).make(bson);
531+
const document = (responseType ?? MongoDBResponse).make(bson);
484532

485-
yield document;
486-
this.throwIfAborted();
533+
yield document;
487534

488-
this.socket.setTimeout(timeout);
535+
readStarted(payload);
536+
this.throwIfAborted();
537+
538+
this.socket.setTimeout(timeout);
539+
}
540+
} catch (error) {
541+
readFailed({
542+
...payload,
543+
error
544+
});
545+
throw error;
489546
}
490547
} finally {
491548
this.socket.setTimeout(0);

test/tools/runner/hooks/configuration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { NodeVersionFilter } from '../filters/node_version_filter';
2222
import { OSFilter } from '../filters/os_filter';
2323
import { ServerlessFilter } from '../filters/serverless_filter';
2424
import { type Filter } from '../filters/filter';
25+
import { spawnSync } from 'child_process';
2526

2627
// Default our tests to have auth enabled
2728
// A better solution will be tackled in NODE-3714
@@ -46,6 +47,8 @@ const loadBalanced = SINGLE_MONGOS_LB_URI && MULTI_MONGOS_LB_URI;
4647
const filters: Filter[] = [];
4748

4849
let initializedFilters = false;
50+
51+
process.env.HOSTNAME = spawnSync('hostname', { encoding: 'utf-8' }).stdout;
4952
async function initializeFilters(client): Promise<Record<string, any>> {
5053
if (initializedFilters) {
5154
return {};

0 commit comments

Comments
 (0)