|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Text.Json; |
| 19 | +using System.Threading; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Amazon.Lambda.APIGatewayEvents; |
| 22 | +using Amazon.Lambda.Core; |
| 23 | +using Amazon.Lambda.Serialization.SystemTextJson; |
| 24 | +using MongoDB.Bson; |
| 25 | +using MongoDB.Driver.Core.Events; |
| 26 | + |
| 27 | +// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. |
| 28 | +[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))] |
| 29 | + |
| 30 | +namespace MongoDB.Driver.LambdaTest |
| 31 | +{ |
| 32 | + public class LambdaFunction |
| 33 | + { |
| 34 | + private MongoClient _mongoClient; |
| 35 | + private long _openConnections; |
| 36 | + private long _totalHeartbeatCount; |
| 37 | + private double _totalHeartbeatDurationMs; |
| 38 | + private long _totalCommandCount; |
| 39 | + private double _totalCommandDurationMs; |
| 40 | + private bool _serverHeartbeatWasAwaited; |
| 41 | + |
| 42 | + public LambdaFunction() |
| 43 | + { |
| 44 | + _openConnections = 0; |
| 45 | + _totalHeartbeatCount = 0; |
| 46 | + _totalHeartbeatDurationMs = 0; |
| 47 | + _totalCommandCount = 0; |
| 48 | + _totalCommandDurationMs = 0; |
| 49 | + _serverHeartbeatWasAwaited = false; |
| 50 | + |
| 51 | + var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI"); |
| 52 | + |
| 53 | + // register listeners for the to be monitored events |
| 54 | + var clientSettings = MongoClientSettings.FromConnectionString(connectionString); |
| 55 | + clientSettings.ClusterConfigurator = builder => |
| 56 | + { |
| 57 | + builder |
| 58 | + .Subscribe<ServerHeartbeatSucceededEvent>(Handle) |
| 59 | + .Subscribe<ServerHeartbeatFailedEvent>(Handle) |
| 60 | + .Subscribe<CommandSucceededEvent>(Handle) |
| 61 | + .Subscribe<CommandFailedEvent>(Handle) |
| 62 | + .Subscribe<ConnectionCreatedEvent>(Handle) |
| 63 | + .Subscribe<ConnectionClosedEvent>(Handle); |
| 64 | + }; |
| 65 | + |
| 66 | + // Client is used for all requests |
| 67 | + _mongoClient = new MongoClient(clientSettings); |
| 68 | + } |
| 69 | + |
| 70 | + public async Task<APIGatewayProxyResponse> LambdaFunctionHandlerAsync(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context) |
| 71 | + { |
| 72 | + var collection = _mongoClient.GetDatabase("lambdaTest").GetCollection<BsonDocument>("test"); |
| 73 | + var testDocument = new BsonDocument("n", 1); |
| 74 | + await collection.InsertOneAsync(testDocument); |
| 75 | + await collection.DeleteOneAsync(doc => doc["_id"] == testDocument["_id"]); |
| 76 | + |
| 77 | + Dictionary<string, string> responseBody; |
| 78 | + if (_serverHeartbeatWasAwaited) |
| 79 | + { |
| 80 | + // This is an error since the streaming protocol should be disabled in a FaaS environment thus no server heartbeat should be awaited |
| 81 | + responseBody = new Dictionary<string, string> |
| 82 | + { |
| 83 | + { "message", "ERROR: no ServerHeartBeat events should contain the awaited=true flag." }, |
| 84 | + }; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + responseBody = new Dictionary<string, string> |
| 89 | + { |
| 90 | + { "averageCommandDurationMS", $"{_totalCommandDurationMs / _totalCommandCount :F3}"}, |
| 91 | + { "averageServerHeartbeatDurationMS", $"{_totalHeartbeatDurationMs / _totalHeartbeatCount :F3}"}, |
| 92 | + { "openConnections", $"{_openConnections}" }, |
| 93 | + { "heartBeatCount", $"{_totalHeartbeatCount}" }, |
| 94 | + { "commandCount", $"{_totalCommandCount}" }, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + Reset(); |
| 99 | + |
| 100 | + return new APIGatewayProxyResponse |
| 101 | + { |
| 102 | + Body = JsonSerializer.Serialize(responseBody, new JsonSerializerOptions { WriteIndented = true}), |
| 103 | + StatusCode = _serverHeartbeatWasAwaited ? 502 : 200, |
| 104 | + Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + // actions for monitored events |
| 109 | + private void Handle(ServerHeartbeatSucceededEvent @event) |
| 110 | + { |
| 111 | + _totalHeartbeatCount++; |
| 112 | + _totalHeartbeatDurationMs += @event.Duration.TotalMilliseconds; |
| 113 | + _serverHeartbeatWasAwaited |= @event.Awaited; |
| 114 | + } |
| 115 | + |
| 116 | + private void Handle(ServerHeartbeatFailedEvent @event) |
| 117 | + { |
| 118 | + _totalHeartbeatCount++; |
| 119 | + _totalHeartbeatDurationMs += @event.Duration.TotalMilliseconds; |
| 120 | + _serverHeartbeatWasAwaited |= @event.Awaited; |
| 121 | + } |
| 122 | + |
| 123 | + private void Handle(CommandSucceededEvent @event) |
| 124 | + { |
| 125 | + _totalCommandCount++; |
| 126 | + _totalCommandDurationMs += @event.Duration.TotalMilliseconds; |
| 127 | + } |
| 128 | + |
| 129 | + private void Handle(CommandFailedEvent @event) |
| 130 | + { |
| 131 | + _totalCommandCount++; |
| 132 | + _totalCommandDurationMs += @event.Duration.TotalMilliseconds; |
| 133 | + } |
| 134 | + |
| 135 | + private void Handle(ConnectionCreatedEvent @event) |
| 136 | + { |
| 137 | + Interlocked.Increment(ref _openConnections); |
| 138 | + } |
| 139 | + |
| 140 | + private void Handle(ConnectionClosedEvent @event) |
| 141 | + { |
| 142 | + Interlocked.Decrement(ref _openConnections); |
| 143 | + } |
| 144 | + |
| 145 | + private void Reset() |
| 146 | + { |
| 147 | + _totalHeartbeatCount = 0; |
| 148 | + _totalHeartbeatDurationMs = 0; |
| 149 | + _totalCommandCount = 0; |
| 150 | + _totalCommandDurationMs = 0; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments