|
| 1 | +import { MongoClient } from 'mongodb'; |
| 2 | + |
| 3 | +// Creates the client that is cached for all requests, subscribes to |
| 4 | +// relevant events, and forces the connection pool to get populated. |
| 5 | +const mongoClient = new MongoClient(process.env.MONGODB_URI, { |
| 6 | + monitorCommands: true |
| 7 | +}); |
| 8 | + |
| 9 | +let openConnections = 0; |
| 10 | +let heartbeatCount = 0; |
| 11 | +let totalHeartbeatDuration = 0; |
| 12 | +let totalCommands = 0; |
| 13 | +let totalCommandDuration = 0; |
| 14 | + |
| 15 | +mongoClient.on('commandStarted', (event) => { |
| 16 | + console.log('commandStarted', event); |
| 17 | +}); |
| 18 | + |
| 19 | +mongoClient.on('commandSucceeded', (event) => { |
| 20 | + totalCommands++; |
| 21 | + totalCommandDuration += event.duration; |
| 22 | + console.log('commandSucceeded', event); |
| 23 | +}); |
| 24 | + |
| 25 | +mongoClient.on('commandFailed', (event) => { |
| 26 | + totalCommands++; |
| 27 | + totalCommandDuration += event.duration; |
| 28 | + console.log('commandFailed', event); |
| 29 | +}); |
| 30 | + |
| 31 | +mongoClient.on('serverHeartbeatStarted', (event) => { |
| 32 | + console.log('serverHeartbeatStarted', event); |
| 33 | +}); |
| 34 | + |
| 35 | +mongoClient.on('serverHeartbeatSucceeded', (event) => { |
| 36 | + heartbeatCount++; |
| 37 | + totalHeartbeatDuration += event.duration; |
| 38 | + console.log('serverHeartbeatSucceeded', event); |
| 39 | +}); |
| 40 | + |
| 41 | +mongoClient.on('serverHeartbeatFailed', (event) => { |
| 42 | + heartbeatCount++; |
| 43 | + totalHeartbeatDuration += event.duration; |
| 44 | + console.log('serverHeartbeatFailed', event); |
| 45 | +}); |
| 46 | + |
| 47 | +mongoClient.on('connectionCreated', (event) => { |
| 48 | + openConnections++; |
| 49 | + console.log('connectionCreated', event); |
| 50 | +}); |
| 51 | + |
| 52 | +mongoClient.on('connectionClosed', (event) => { |
| 53 | + openConnections--; |
| 54 | + console.log('connectionClosed', event); |
| 55 | +}); |
| 56 | + |
| 57 | +// Populate the connection pool. |
| 58 | +await mongoClient.connect(); |
| 59 | + |
| 60 | +// Create the response to send back. |
| 61 | +function createResponse() { |
| 62 | + return { |
| 63 | + averageCommandDuration: totalCommandDuration / totalCommands, |
| 64 | + averageHeartbeatDuration: totalHeartbeatDuration / heartbeatCount, |
| 65 | + openConnections: openConnections, |
| 66 | + heartbeatCount: heartbeatCount |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +// Reset the numbers. |
| 71 | +function reset() { |
| 72 | + openConnections = 0; |
| 73 | + heartbeatCount = 0; |
| 74 | + totalHeartbeatDuration = 0; |
| 75 | + totalCommands = 0; |
| 76 | + totalCommandDuration = 0; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * The handler function itself performs an insert/delete and returns the |
| 81 | + * id of the document in play. |
| 82 | + * |
| 83 | + * @param {Object} event - API Gateway Lambda Proxy Input Format |
| 84 | + * @returns {Object} object - API Gateway Lambda Proxy Output Format |
| 85 | + */ |
| 86 | +export const lambdaHandler = async (event) => { |
| 87 | + const db = mongoClient.db('lambdaTest'); |
| 88 | + const collection = db.collection('test'); |
| 89 | + const { insertedId } = await collection.insertOne({ n: 1 }); |
| 90 | + await collection.deleteOne({ _id: insertedId }); |
| 91 | + // Create the response and then reset the numbers. |
| 92 | + const response = JSON.stringify(createResponse()); |
| 93 | + reset(); |
| 94 | + |
| 95 | + return { |
| 96 | + statusCode: 200, |
| 97 | + body: response |
| 98 | + }; |
| 99 | +}; |
0 commit comments