Skip to content

Commit cb6692f

Browse files
authored
Refactor FaaS support in ClientMetadataHelper (#1300)
Move FaasEnvironment enum and related static methods to a top-level class, in preparation for use in DefaultServerMonitor. JAVA-4936
1 parent baebf90 commit cb6692f

File tree

2 files changed

+118
-94
lines changed

2 files changed

+118
-94
lines changed

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
import java.io.File;
3333
import java.nio.charset.StandardCharsets;
3434
import java.nio.file.Files;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
3735
import java.util.List;
3836
import java.util.function.Consumer;
3937

4038
import static com.mongodb.assertions.Assertions.isTrueArgument;
39+
import static com.mongodb.internal.connection.FaasEnvironment.getFaasEnvironment;
4140
import static java.lang.String.format;
4241
import static java.lang.System.getProperty;
4342
import static java.nio.file.Paths.get;
@@ -180,61 +179,6 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
180179
new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
181180
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
182181
}
183-
private enum FaasEnvironment {
184-
AWS_LAMBDA("aws.lambda"),
185-
AZURE_FUNC("azure.func"),
186-
GCP_FUNC("gcp.func"),
187-
VERCEL("vercel"),
188-
UNKNOWN(null);
189-
190-
@Nullable
191-
private final String name;
192-
193-
FaasEnvironment(@Nullable final String name) {
194-
this.name = name;
195-
}
196-
197-
@Nullable
198-
public String getName() {
199-
return name;
200-
}
201-
202-
@Nullable
203-
public Integer getTimeoutSec() {
204-
switch (this) {
205-
case GCP_FUNC:
206-
return getEnvInteger("FUNCTION_TIMEOUT_SEC");
207-
default:
208-
return null;
209-
}
210-
}
211-
212-
@Nullable
213-
public Integer getMemoryMb() {
214-
switch (this) {
215-
case AWS_LAMBDA:
216-
return getEnvInteger("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
217-
case GCP_FUNC:
218-
return getEnvInteger("FUNCTION_MEMORY_MB");
219-
default:
220-
return null;
221-
}
222-
}
223-
224-
@Nullable
225-
public String getRegion() {
226-
switch (this) {
227-
case AWS_LAMBDA:
228-
return System.getenv("AWS_REGION");
229-
case GCP_FUNC:
230-
return System.getenv("FUNCTION_REGION");
231-
case VERCEL:
232-
return System.getenv("VERCEL_REGION");
233-
default:
234-
return null;
235-
}
236-
}
237-
}
238182

239183
public enum ContainerRuntime {
240184
DOCKER("docker") {
@@ -311,43 +255,6 @@ static Orchestrator determineExecutionOrchestrator() {
311255
}
312256
}
313257

314-
@Nullable
315-
private static Integer getEnvInteger(final String name) {
316-
try {
317-
String value = System.getenv(name);
318-
return Integer.parseInt(value);
319-
} catch (NumberFormatException e) {
320-
return null;
321-
}
322-
}
323-
324-
static FaasEnvironment getFaasEnvironment() {
325-
List<FaasEnvironment> result = new ArrayList<>();
326-
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV");
327-
328-
if (System.getenv("VERCEL") != null) {
329-
result.add(FaasEnvironment.VERCEL);
330-
}
331-
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_"))
332-
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) {
333-
result.add(FaasEnvironment.AWS_LAMBDA);
334-
}
335-
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) {
336-
result.add(FaasEnvironment.AZURE_FUNC);
337-
}
338-
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) {
339-
result.add(FaasEnvironment.GCP_FUNC);
340-
}
341-
// vercel takes precedence over aws.lambda
342-
if (result.equals(Arrays.asList(FaasEnvironment.VERCEL, FaasEnvironment.AWS_LAMBDA))) {
343-
return FaasEnvironment.VERCEL;
344-
}
345-
if (result.size() != 1) {
346-
return FaasEnvironment.UNKNOWN;
347-
}
348-
return result.get(0);
349-
}
350-
351258
static MongoDriverInformation getDriverInformation(@Nullable final MongoDriverInformation mongoDriverInformation) {
352259
MongoDriverInformation.Builder builder = mongoDriverInformation != null ? MongoDriverInformation.builder(mongoDriverInformation)
353260
: MongoDriverInformation.builder();
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.internal.connection;
18+
19+
import com.mongodb.lang.Nullable;
20+
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
enum FaasEnvironment {
26+
AWS_LAMBDA("aws.lambda"),
27+
AZURE_FUNC("azure.func"),
28+
GCP_FUNC("gcp.func"),
29+
VERCEL("vercel"),
30+
UNKNOWN(null);
31+
32+
static FaasEnvironment getFaasEnvironment() {
33+
List<FaasEnvironment> result = new ArrayList<>();
34+
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV");
35+
36+
if (System.getenv("VERCEL") != null) {
37+
result.add(FaasEnvironment.VERCEL);
38+
}
39+
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_"))
40+
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) {
41+
result.add(FaasEnvironment.AWS_LAMBDA);
42+
}
43+
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) {
44+
result.add(FaasEnvironment.AZURE_FUNC);
45+
}
46+
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) {
47+
result.add(FaasEnvironment.GCP_FUNC);
48+
}
49+
// vercel takes precedence over aws.lambda
50+
if (result.equals(Arrays.asList(FaasEnvironment.VERCEL, FaasEnvironment.AWS_LAMBDA))) {
51+
return FaasEnvironment.VERCEL;
52+
}
53+
if (result.size() != 1) {
54+
return FaasEnvironment.UNKNOWN;
55+
}
56+
return result.get(0);
57+
}
58+
59+
@Nullable
60+
private final String name;
61+
62+
FaasEnvironment(@Nullable final String name) {
63+
this.name = name;
64+
}
65+
66+
@Nullable
67+
public String getName() {
68+
return name;
69+
}
70+
71+
@Nullable
72+
public Integer getTimeoutSec() {
73+
//noinspection SwitchStatementWithTooFewBranches
74+
switch (this) {
75+
case GCP_FUNC:
76+
return getEnvInteger("FUNCTION_TIMEOUT_SEC");
77+
default:
78+
return null;
79+
}
80+
}
81+
82+
@Nullable
83+
public Integer getMemoryMb() {
84+
switch (this) {
85+
case AWS_LAMBDA:
86+
return getEnvInteger("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
87+
case GCP_FUNC:
88+
return getEnvInteger("FUNCTION_MEMORY_MB");
89+
default:
90+
return null;
91+
}
92+
}
93+
94+
@Nullable
95+
public String getRegion() {
96+
switch (this) {
97+
case AWS_LAMBDA:
98+
return System.getenv("AWS_REGION");
99+
case GCP_FUNC:
100+
return System.getenv("FUNCTION_REGION");
101+
case VERCEL:
102+
return System.getenv("VERCEL_REGION");
103+
default:
104+
return null;
105+
}
106+
}
107+
108+
@Nullable
109+
private static Integer getEnvInteger(final String name) {
110+
try {
111+
String value = System.getenv(name);
112+
return Integer.parseInt(value);
113+
} catch (NumberFormatException e) {
114+
return null;
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)