Skip to content

Commit

Permalink
Refactor FaaS support in ClientMetadataHelper (#1300)
Browse files Browse the repository at this point in the history
Move FaasEnvironment enum and related static methods to a
top-level class, in preparation for use in DefaultServerMonitor.

JAVA-4936
  • Loading branch information
jyemin authored Jan 26, 2024
1 parent baebf90 commit cb6692f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.internal.connection.FaasEnvironment.getFaasEnvironment;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.nio.file.Paths.get;
Expand Down Expand Up @@ -180,61 +179,6 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
}
private enum FaasEnvironment {
AWS_LAMBDA("aws.lambda"),
AZURE_FUNC("azure.func"),
GCP_FUNC("gcp.func"),
VERCEL("vercel"),
UNKNOWN(null);

@Nullable
private final String name;

FaasEnvironment(@Nullable final String name) {
this.name = name;
}

@Nullable
public String getName() {
return name;
}

@Nullable
public Integer getTimeoutSec() {
switch (this) {
case GCP_FUNC:
return getEnvInteger("FUNCTION_TIMEOUT_SEC");
default:
return null;
}
}

@Nullable
public Integer getMemoryMb() {
switch (this) {
case AWS_LAMBDA:
return getEnvInteger("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
case GCP_FUNC:
return getEnvInteger("FUNCTION_MEMORY_MB");
default:
return null;
}
}

@Nullable
public String getRegion() {
switch (this) {
case AWS_LAMBDA:
return System.getenv("AWS_REGION");
case GCP_FUNC:
return System.getenv("FUNCTION_REGION");
case VERCEL:
return System.getenv("VERCEL_REGION");
default:
return null;
}
}
}

public enum ContainerRuntime {
DOCKER("docker") {
Expand Down Expand Up @@ -311,43 +255,6 @@ static Orchestrator determineExecutionOrchestrator() {
}
}

@Nullable
private static Integer getEnvInteger(final String name) {
try {
String value = System.getenv(name);
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return null;
}
}

static FaasEnvironment getFaasEnvironment() {
List<FaasEnvironment> result = new ArrayList<>();
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV");

if (System.getenv("VERCEL") != null) {
result.add(FaasEnvironment.VERCEL);
}
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_"))
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) {
result.add(FaasEnvironment.AWS_LAMBDA);
}
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) {
result.add(FaasEnvironment.AZURE_FUNC);
}
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) {
result.add(FaasEnvironment.GCP_FUNC);
}
// vercel takes precedence over aws.lambda
if (result.equals(Arrays.asList(FaasEnvironment.VERCEL, FaasEnvironment.AWS_LAMBDA))) {
return FaasEnvironment.VERCEL;
}
if (result.size() != 1) {
return FaasEnvironment.UNKNOWN;
}
return result.get(0);
}

static MongoDriverInformation getDriverInformation(@Nullable final MongoDriverInformation mongoDriverInformation) {
MongoDriverInformation.Builder builder = mongoDriverInformation != null ? MongoDriverInformation.builder(mongoDriverInformation)
: MongoDriverInformation.builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal.connection;

import com.mongodb.lang.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

enum FaasEnvironment {
AWS_LAMBDA("aws.lambda"),
AZURE_FUNC("azure.func"),
GCP_FUNC("gcp.func"),
VERCEL("vercel"),
UNKNOWN(null);

static FaasEnvironment getFaasEnvironment() {
List<FaasEnvironment> result = new ArrayList<>();
String awsExecutionEnv = System.getenv("AWS_EXECUTION_ENV");

if (System.getenv("VERCEL") != null) {
result.add(FaasEnvironment.VERCEL);
}
if ((awsExecutionEnv != null && awsExecutionEnv.startsWith("AWS_Lambda_"))
|| System.getenv("AWS_LAMBDA_RUNTIME_API") != null) {
result.add(FaasEnvironment.AWS_LAMBDA);
}
if (System.getenv("FUNCTIONS_WORKER_RUNTIME") != null) {
result.add(FaasEnvironment.AZURE_FUNC);
}
if (System.getenv("K_SERVICE") != null || System.getenv("FUNCTION_NAME") != null) {
result.add(FaasEnvironment.GCP_FUNC);
}
// vercel takes precedence over aws.lambda
if (result.equals(Arrays.asList(FaasEnvironment.VERCEL, FaasEnvironment.AWS_LAMBDA))) {
return FaasEnvironment.VERCEL;
}
if (result.size() != 1) {
return FaasEnvironment.UNKNOWN;
}
return result.get(0);
}

@Nullable
private final String name;

FaasEnvironment(@Nullable final String name) {
this.name = name;
}

@Nullable
public String getName() {
return name;
}

@Nullable
public Integer getTimeoutSec() {
//noinspection SwitchStatementWithTooFewBranches
switch (this) {
case GCP_FUNC:
return getEnvInteger("FUNCTION_TIMEOUT_SEC");
default:
return null;
}
}

@Nullable
public Integer getMemoryMb() {
switch (this) {
case AWS_LAMBDA:
return getEnvInteger("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
case GCP_FUNC:
return getEnvInteger("FUNCTION_MEMORY_MB");
default:
return null;
}
}

@Nullable
public String getRegion() {
switch (this) {
case AWS_LAMBDA:
return System.getenv("AWS_REGION");
case GCP_FUNC:
return System.getenv("FUNCTION_REGION");
case VERCEL:
return System.getenv("VERCEL_REGION");
default:
return null;
}
}

@Nullable
private static Integer getEnvInteger(final String name) {
try {
String value = System.getenv(name);
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return null;
}
}
}

0 comments on commit cb6692f

Please sign in to comment.