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