1- import * as assert from 'node:assert/strict' ;
1+ /* eslint-disable no-console */
2+ import util from 'node:util' ;
23
34import { MongoClient } from 'mongodb' ;
45
56// Creates the client that is cached for all requests, subscribes to
67// relevant events, and forces the connection pool to get populated.
78const mongoClient = new MongoClient ( process . env . MONGODB_URI , {
8- monitorCommands : true
9+ monitorCommands : true ,
10+ mongodbLogComponentSeverities : {
11+ command : 'trace' ,
12+ topology : 'trace' ,
13+ connection : 'trace' ,
14+ default : 'trace'
15+ } ,
16+ mongodbLogMaxDocumentLength : 10_000 ,
17+ mongodbLogPath : {
18+ write ( log ) {
19+ console . log (
20+ util . inspect ( log , { colors : false , breakLength : Infinity , compact : true , depth : Infinity } )
21+ ) ;
22+ }
23+ }
924} ) ;
1025
1126let openConnections = 0 ;
@@ -14,49 +29,38 @@ let totalHeartbeatDuration = 0;
1429let totalCommands = 0 ;
1530let totalCommandDuration = 0 ;
1631
17- mongoClient . on ( 'commandStarted' , ( event ) => {
18- console . log ( 'commandStarted' , event ) ;
19- } ) ;
20-
21- mongoClient . on ( 'commandSucceeded' , ( event ) => {
32+ mongoClient . on ( 'commandSucceeded' , event => {
2233 totalCommands ++ ;
2334 totalCommandDuration += event . duration ;
24- console . log ( 'commandSucceeded' , event ) ;
2535} ) ;
2636
27- mongoClient . on ( 'commandFailed' , ( event ) => {
37+ mongoClient . on ( 'commandFailed' , event => {
2838 totalCommands ++ ;
2939 totalCommandDuration += event . duration ;
30- console . log ( 'commandFailed' , event ) ;
3140} ) ;
3241
33- mongoClient . on ( 'serverHeartbeatStarted' , ( event ) => {
34- console . log ( 'serverHeartbeatStarted' , event ) ;
35- assert . strictEqual ( event . awaited , false ) ;
42+ mongoClient . on ( 'serverHeartbeatStarted' , event => {
43+ if ( event . awaited !== false ) console . log ( 'server hb started' , { awaited : event . awaited } ) ;
3644} ) ;
3745
38- mongoClient . on ( 'serverHeartbeatSucceeded' , ( event ) => {
46+ mongoClient . on ( 'serverHeartbeatSucceeded' , event => {
3947 heartbeatCount ++ ;
4048 totalHeartbeatDuration += event . duration ;
41- console . log ( 'serverHeartbeatSucceeded' , event ) ;
42- assert . strictEqual ( event . awaited , false ) ;
49+ if ( event . awaited !== false ) console . log ( 'server hb succeeded' , { awaited : event . awaited } ) ;
4350} ) ;
4451
45- mongoClient . on ( 'serverHeartbeatFailed' , ( event ) => {
52+ mongoClient . on ( 'serverHeartbeatFailed' , event => {
4653 heartbeatCount ++ ;
4754 totalHeartbeatDuration += event . duration ;
48- console . log ( 'serverHeartbeatFailed' , event ) ;
49- assert . strictEqual ( event . awaited , false ) ;
55+ if ( event . awaited !== false ) console . log ( 'server hb failed' , { awaited : event . awaited } ) ;
5056} ) ;
5157
52- mongoClient . on ( 'connectionCreated' , ( event ) => {
58+ mongoClient . on ( 'connectionCreated' , ( ) => {
5359 openConnections ++ ;
54- console . log ( 'connectionCreated' , event ) ;
5560} ) ;
5661
57- mongoClient . on ( 'connectionClosed' , ( event ) => {
62+ mongoClient . on ( 'connectionClosed' , ( ) => {
5863 openConnections -- ;
59- console . log ( 'connectionClosed' , event ) ;
6064} ) ;
6165
6266// Populate the connection pool.
@@ -65,8 +69,8 @@ await mongoClient.connect();
6569// Create the response to send back.
6670function createResponse ( ) {
6771 return {
68- averageCommandDuration : totalCommandDuration / totalCommands ,
69- averageHeartbeatDuration : totalHeartbeatDuration / heartbeatCount ,
72+ averageCommandDuration : totalCommands === 0 ? 0 : totalCommandDuration / totalCommands ,
73+ averageHeartbeatDuration : heartbeatCount === 0 ? 0 : totalHeartbeatDuration / heartbeatCount ,
7074 openConnections : openConnections ,
7175 heartbeatCount : heartbeatCount
7276 } ;
@@ -85,10 +89,10 @@ function reset() {
8589 * The handler function itself performs an insert/delete and returns the
8690 * id of the document in play.
8791 *
88- * @param { Object } event - API Gateway Lambda Proxy Input Format
89- * @returns { Object } object - API Gateway Lambda Proxy Output Format
92+ * @param event - API Gateway Lambda Proxy Input Format
93+ * @returns API Gateway Lambda Proxy Output Format
9094 */
91- export const lambdaHandler = async ( event ) => {
95+ export const lambdaHandler = async ( ) => {
9296 const db = mongoClient . db ( 'lambdaTest' ) ;
9397 const collection = db . collection ( 'test' ) ;
9498 const { insertedId } = await collection . insertOne ( { n : 1 } ) ;
0 commit comments