|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.lambdatest; |
| 18 | + |
| 19 | +import com.amazonaws.services.lambda.runtime.Context; |
| 20 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 21 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; |
| 22 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; |
| 23 | +import com.mongodb.ConnectionString; |
| 24 | +import com.mongodb.MongoClientSettings; |
| 25 | +import com.mongodb.client.MongoClient; |
| 26 | +import com.mongodb.client.MongoClients; |
| 27 | +import com.mongodb.client.MongoCollection; |
| 28 | +import com.mongodb.event.CommandFailedEvent; |
| 29 | +import com.mongodb.event.CommandListener; |
| 30 | +import com.mongodb.event.CommandSucceededEvent; |
| 31 | +import com.mongodb.event.ConnectionClosedEvent; |
| 32 | +import com.mongodb.event.ConnectionCreatedEvent; |
| 33 | +import com.mongodb.event.ConnectionPoolListener; |
| 34 | +import com.mongodb.event.ServerHeartbeatFailedEvent; |
| 35 | +import com.mongodb.event.ServerHeartbeatSucceededEvent; |
| 36 | +import com.mongodb.event.ServerMonitorListener; |
| 37 | +import com.mongodb.lang.NonNull; |
| 38 | +import org.bson.BsonDocument; |
| 39 | +import org.bson.BsonInt64; |
| 40 | +import org.bson.BsonString; |
| 41 | +import org.bson.BsonValue; |
| 42 | +import org.bson.Document; |
| 43 | + |
| 44 | +import java.io.PrintWriter; |
| 45 | +import java.io.StringWriter; |
| 46 | +import java.util.HashMap; |
| 47 | +import java.util.Map; |
| 48 | + |
| 49 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 50 | + |
| 51 | +/** |
| 52 | + * Test App for AWS lambda functions |
| 53 | + */ |
| 54 | +public class LambdaTestApp implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 55 | + private final MongoClient mongoClient; |
| 56 | + private long openConnections = 0; |
| 57 | + private long totalHeartbeatCount = 0; |
| 58 | + private long totalHeartbeatDurationMs = 0; |
| 59 | + private long totalCommandCount = 0; |
| 60 | + private long totalCommandDurationMs = 0; |
| 61 | + |
| 62 | + public LambdaTestApp() { |
| 63 | + String connectionString = System.getenv("MONGODB_URI"); |
| 64 | + |
| 65 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 66 | + .applyConnectionString(new ConnectionString(connectionString)) |
| 67 | + .addCommandListener(new CommandListener() { |
| 68 | + @Override |
| 69 | + public void commandSucceeded(@NonNull final CommandSucceededEvent event) { |
| 70 | + totalCommandCount++; |
| 71 | + totalCommandDurationMs += event.getElapsedTime(MILLISECONDS); |
| 72 | + } |
| 73 | + @Override |
| 74 | + public void commandFailed(@NonNull final CommandFailedEvent event) { |
| 75 | + totalCommandCount++; |
| 76 | + totalCommandDurationMs += event.getElapsedTime(MILLISECONDS); |
| 77 | + } |
| 78 | + }) |
| 79 | + .applyToServerSettings(builder -> builder.addServerMonitorListener(new ServerMonitorListener() { |
| 80 | + @Override |
| 81 | + public void serverHeartbeatSucceeded(@NonNull final ServerHeartbeatSucceededEvent event) { |
| 82 | + totalHeartbeatCount++; |
| 83 | + totalHeartbeatDurationMs += event.getElapsedTime(MILLISECONDS); |
| 84 | + } |
| 85 | + @Override |
| 86 | + public void serverHeartbeatFailed(@NonNull final ServerHeartbeatFailedEvent event) { |
| 87 | + totalHeartbeatCount++; |
| 88 | + totalHeartbeatDurationMs += event.getElapsedTime(MILLISECONDS); |
| 89 | + } |
| 90 | + })) |
| 91 | + .applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new ConnectionPoolListener() { |
| 92 | + @Override |
| 93 | + public void connectionCreated(@NonNull final ConnectionCreatedEvent event) { |
| 94 | + openConnections++; |
| 95 | + } |
| 96 | + @Override |
| 97 | + public void connectionClosed(@NonNull final ConnectionClosedEvent event) { |
| 98 | + openConnections--; |
| 99 | + } |
| 100 | + })) |
| 101 | + .build(); |
| 102 | + mongoClient = MongoClients.create(settings); |
| 103 | + } |
| 104 | + |
| 105 | + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { |
| 106 | + try { |
| 107 | + MongoCollection<Document> collection = mongoClient |
| 108 | + .getDatabase("lambdaTest") |
| 109 | + .getCollection("test"); |
| 110 | + BsonValue id = collection.insertOne(new Document("n", 1)).getInsertedId(); |
| 111 | + collection.deleteOne(new Document("_id", id)); |
| 112 | + |
| 113 | + BsonDocument responseBody = getBsonDocument(); |
| 114 | + |
| 115 | + return templateResponse() |
| 116 | + .withStatusCode(200) |
| 117 | + .withBody(responseBody.toJson()); |
| 118 | + |
| 119 | + } catch (Throwable e) { |
| 120 | + StringWriter sw = new StringWriter(); |
| 121 | + e.printStackTrace(new PrintWriter(sw)); |
| 122 | + BsonDocument responseBody = new BsonDocument() |
| 123 | + .append("throwable", new BsonString(e.getMessage())) |
| 124 | + .append("stacktrace", new BsonString(sw.toString())); |
| 125 | + return templateResponse() |
| 126 | + .withBody(responseBody.toJson()) |
| 127 | + .withStatusCode(500); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private BsonDocument getBsonDocument() { |
| 132 | + BsonDocument responseBody = new BsonDocument() |
| 133 | + .append("totalCommandDurationMs", new BsonInt64(totalCommandDurationMs)) |
| 134 | + .append("totalCommandCount", new BsonInt64(totalCommandCount)) |
| 135 | + .append("totalHeartbeatDurationMs", new BsonInt64(totalHeartbeatDurationMs)) |
| 136 | + .append("totalHeartbeatCount", new BsonInt64(totalHeartbeatCount)) |
| 137 | + .append("openConnections", new BsonInt64(openConnections)); |
| 138 | + |
| 139 | + totalCommandDurationMs = 0; |
| 140 | + totalCommandCount = 0; |
| 141 | + totalHeartbeatCount = 0; |
| 142 | + totalHeartbeatDurationMs = 0; |
| 143 | + |
| 144 | + return responseBody; |
| 145 | + } |
| 146 | + |
| 147 | + private APIGatewayProxyResponseEvent templateResponse() { |
| 148 | + Map<String, String> headers = new HashMap<>(); |
| 149 | + headers.put("Content-Type", "application/json"); |
| 150 | + headers.put("X-Custom-Header", "application/json"); |
| 151 | + return new APIGatewayProxyResponseEvent() |
| 152 | + .withHeaders(headers); |
| 153 | + } |
| 154 | +} |
0 commit comments